Support Metadata in GridFSAdapter (#6660)

* Support Metadata in GridFSAdapter

* Useful for testing in the JS SDK
* Adds new endpoint to be used with `Parse.File.getData`
* Allows file adapters to return tags as well as future data.

* fix tests

* Make getMetadata optional

* Revert "fix tests"

This reverts commit 7706da13c688027483974e854b5b24321fb070cd.

* improve coverage
This commit is contained in:
Diamond Lewis
2020-05-08 15:32:20 -05:00
committed by GitHub
parent c32ff20f4f
commit 370215a39b
6 changed files with 162 additions and 50 deletions

View File

@@ -8,6 +8,7 @@ const GridStoreAdapter = require('../lib/Adapters/Files/GridStoreAdapter')
.GridStoreAdapter;
const Config = require('../lib/Config');
const FilesController = require('../lib/Controllers/FilesController').default;
const databaseURI = 'mongodb://localhost:27017/parse';
const mockAdapter = {
createFile: () => {
@@ -23,13 +24,13 @@ const mockAdapter = {
// Small additional tests to improve overall coverage
describe('FilesController', () => {
it('should properly expand objects', done => {
it('should properly expand objects', (done) => {
const config = Config.get(Parse.applicationId);
const gridStoreAdapter = new GridFSBucketAdapter(
'mongodb://localhost:27017/parse'
);
const filesController = new FilesController(gridStoreAdapter);
const result = filesController.expandFilesInObject(config, function() {});
const result = filesController.expandFilesInObject(config, function () {});
expect(result).toBeUndefined();
@@ -47,7 +48,7 @@ describe('FilesController', () => {
done();
});
it('should create a server log on failure', done => {
it('should create a server log on failure', (done) => {
const logController = new LoggerController(new WinstonLoggerAdapter());
reconfigureServer({ filesAdapter: mockAdapter })
@@ -56,22 +57,20 @@ describe('FilesController', () => {
() => done.fail('should not succeed'),
() => setImmediate(() => Promise.resolve('done'))
)
.then(() => new Promise(resolve => setTimeout(resolve, 200)))
.then(() => new Promise((resolve) => setTimeout(resolve, 200)))
.then(() =>
logController.getLogs({ from: Date.now() - 1000, size: 1000 })
)
.then(logs => {
.then((logs) => {
// we get two logs here: 1. the source of the failure to save the file
// and 2 the message that will be sent back to the client.
const log1 = logs.find(
x => x.message === 'Error creating a file: it failed with xyz'
(x) => x.message === 'Error creating a file: it failed with xyz'
);
expect(log1.level).toBe('error');
const log2 = logs.find(
x => x.message === 'it failed with xyz'
);
const log2 = logs.find((x) => x.message === 'it failed with xyz');
expect(log2.level).toBe('error');
expect(log2.code).toBe(130);
@@ -79,7 +78,7 @@ describe('FilesController', () => {
});
});
it('should create a parse error when a string is returned', done => {
it('should create a parse error when a string is returned', (done) => {
const mock2 = mockAdapter;
mock2.validateFilename = () => {
return 'Bad file! No biscuit!';
@@ -92,7 +91,7 @@ describe('FilesController', () => {
done();
});
it('should add a unique hash to the file name when the preserveFileName option is false', done => {
it('should add a unique hash to the file name when the preserveFileName option is false', (done) => {
const config = Config.get(Parse.applicationId);
const gridStoreAdapter = new GridFSBucketAdapter(
'mongodb://localhost:27017/parse'
@@ -115,7 +114,7 @@ describe('FilesController', () => {
done();
});
it('should not add a unique hash to the file name when the preserveFileName option is true', done => {
it('should not add a unique hash to the file name when the preserveFileName option is true', (done) => {
const config = Config.get(Parse.applicationId);
const gridStoreAdapter = new GridFSBucketAdapter(
'mongodb://localhost:27017/parse'
@@ -137,7 +136,16 @@ describe('FilesController', () => {
done();
});
it('should reject slashes in file names', done => {
it('should handle adapter without getMetadata', async () => {
const gridStoreAdapter = new GridFSBucketAdapter(databaseURI);
gridStoreAdapter.getMetadata = null;
const filesController = new FilesController(gridStoreAdapter);
const result = await filesController.getMetadata();
expect(result).toEqual({});
});
it('should reject slashes in file names', (done) => {
const gridStoreAdapter = new GridFSBucketAdapter(
'mongodb://localhost:27017/parse'
);
@@ -146,7 +154,7 @@ describe('FilesController', () => {
done();
});
it('should also reject slashes in file names', done => {
it('should also reject slashes in file names', (done) => {
const gridStoreAdapter = new GridStoreAdapter(
'mongodb://localhost:27017/parse'
);