Silences warnings from mongodb client (#5025)
* Silences warnings from mongodb client * Update count, delete and finds to recommended implementations * With new parser, readPref will be null by default * Update flaky specs wih async/await style * Adds gridstore adapter spec * Use GridFSBucketStorage adapter
This commit is contained in:
@@ -2,8 +2,8 @@ const LoggerController = require('../lib/Controllers/LoggerController')
|
||||
.LoggerController;
|
||||
const WinstonLoggerAdapter = require('../lib/Adapters/Logger/WinstonLoggerAdapter')
|
||||
.WinstonLoggerAdapter;
|
||||
const GridStoreAdapter = require('../lib/Adapters/Files/GridStoreAdapter')
|
||||
.GridStoreAdapter;
|
||||
const GridFSBucketAdapter = require('../lib/Adapters/Files/GridFSBucketAdapter')
|
||||
.GridFSBucketAdapter;
|
||||
const Config = require('../lib/Config');
|
||||
const FilesController = require('../lib/Controllers/FilesController').default;
|
||||
|
||||
@@ -20,7 +20,7 @@ const mockAdapter = {
|
||||
describe('FilesController', () => {
|
||||
it('should properly expand objects', done => {
|
||||
const config = Config.get(Parse.applicationId);
|
||||
const gridStoreAdapter = new GridStoreAdapter(
|
||||
const gridStoreAdapter = new GridFSBucketAdapter(
|
||||
'mongodb://localhost:27017/parse'
|
||||
);
|
||||
const filesController = new FilesController(gridStoreAdapter);
|
||||
@@ -72,7 +72,7 @@ describe('FilesController', () => {
|
||||
|
||||
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 GridStoreAdapter(
|
||||
const gridStoreAdapter = new GridFSBucketAdapter(
|
||||
'mongodb://localhost:27017/parse'
|
||||
);
|
||||
spyOn(gridStoreAdapter, 'createFile');
|
||||
@@ -95,7 +95,7 @@ describe('FilesController', () => {
|
||||
|
||||
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 GridStoreAdapter(
|
||||
const gridStoreAdapter = new GridFSBucketAdapter(
|
||||
'mongodb://localhost:27017/parse'
|
||||
);
|
||||
spyOn(gridStoreAdapter, 'createFile');
|
||||
|
||||
67
spec/GridFSBucketStorageAdapter.spec.js
Normal file
67
spec/GridFSBucketStorageAdapter.spec.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const GridStoreAdapter = require('../lib/Adapters/Files/GridStoreAdapter')
|
||||
.GridStoreAdapter;
|
||||
const GridFSBucketAdapter = require('../lib/Adapters/Files/GridFSBucketAdapter')
|
||||
.GridFSBucketAdapter;
|
||||
const { randomString } = require('../lib/cryptoUtils');
|
||||
const databaseURI = 'mongodb://localhost:27017/parse';
|
||||
|
||||
async function expectMissingFile(gfsAdapter, name) {
|
||||
try {
|
||||
await gfsAdapter.getFileData(name);
|
||||
fail('should have thrown');
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual('FileNotFound: file myFileName was not found');
|
||||
}
|
||||
}
|
||||
|
||||
describe('GridFSBucket and GridStore interop', () => {
|
||||
beforeEach(async () => {
|
||||
const gsAdapter = new GridStoreAdapter(databaseURI);
|
||||
const db = await gsAdapter._connect();
|
||||
db.dropDatabase();
|
||||
});
|
||||
|
||||
it('a file created in GridStore should be available in GridFS', async () => {
|
||||
const gsAdapter = new GridStoreAdapter(databaseURI);
|
||||
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
|
||||
await expectMissingFile(gfsAdapter, 'myFileName');
|
||||
const originalString = 'abcdefghi';
|
||||
await gsAdapter.createFile('myFileName', originalString);
|
||||
const gsResult = await gsAdapter.getFileData('myFileName');
|
||||
expect(gsResult.toString('utf8')).toBe(originalString);
|
||||
const gfsResult = await gfsAdapter.getFileData('myFileName');
|
||||
expect(gfsResult.toString('utf8')).toBe(originalString);
|
||||
});
|
||||
|
||||
it('properly fetches a large file from GridFS', async () => {
|
||||
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
|
||||
const twoMegabytesFile = randomString(2048 * 1024);
|
||||
await gfsAdapter.createFile('myFileName', twoMegabytesFile);
|
||||
const gfsResult = await gfsAdapter.getFileData('myFileName');
|
||||
expect(gfsResult.toString('utf8')).toBe(twoMegabytesFile);
|
||||
});
|
||||
|
||||
it(
|
||||
'properly deletes a file from GridFS',
|
||||
async () => {
|
||||
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
|
||||
await gfsAdapter.createFile('myFileName', 'a simple file');
|
||||
await gfsAdapter.deleteFile('myFileName');
|
||||
await expectMissingFile(gfsAdapter, 'myFileName');
|
||||
},
|
||||
1000000
|
||||
);
|
||||
|
||||
it('properly overrides files', async () => {
|
||||
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
|
||||
await gfsAdapter.createFile('myFileName', 'a simple file');
|
||||
await gfsAdapter.createFile('myFileName', 'an overrided simple file');
|
||||
const data = await gfsAdapter.getFileData('myFileName');
|
||||
expect(data.toString('utf8')).toBe('an overrided simple file');
|
||||
const bucket = await gfsAdapter._getBucket();
|
||||
const documents = await bucket.find({ filename: 'myFileName' }).toArray();
|
||||
expect(documents.length).toBe(2);
|
||||
await gfsAdapter.deleteFile('myFileName');
|
||||
await expectMissingFile(gfsAdapter, 'myFileName');
|
||||
});
|
||||
});
|
||||
@@ -8,11 +8,17 @@ const FilesController = require('../lib/Controllers/FilesController').default;
|
||||
|
||||
// Small additional tests to improve overall coverage
|
||||
describe_only_db('mongo')('GridStoreAdapter', () => {
|
||||
it('should properly instanciate the GridStore when deleting a file', done => {
|
||||
it('should properly instanciate the GridStore when deleting a file', async done => {
|
||||
const databaseURI = 'mongodb://localhost:27017/parse';
|
||||
const config = Config.get(Parse.applicationId);
|
||||
const gridStoreAdapter = new GridStoreAdapter(databaseURI);
|
||||
const filesController = new FilesController(gridStoreAdapter);
|
||||
const db = await gridStoreAdapter._connect();
|
||||
db.dropDatabase();
|
||||
const filesController = new FilesController(
|
||||
gridStoreAdapter,
|
||||
Parse.applicationId,
|
||||
{}
|
||||
);
|
||||
|
||||
// save original unlink before redefinition
|
||||
const originalUnlink = GridStore.prototype.unlink;
|
||||
@@ -33,24 +39,25 @@ describe_only_db('mongo')('GridStoreAdapter', () => {
|
||||
.createFile(config, 'myFilename.txt', 'my file content', 'text/plain')
|
||||
.then(myFile => {
|
||||
return MongoClient.connect(databaseURI)
|
||||
.then(database => {
|
||||
.then(client => {
|
||||
const database = client.db(client.s.options.dbName);
|
||||
// Verify the existance of the fs.files document
|
||||
return database
|
||||
.collection('fs.files')
|
||||
.count()
|
||||
.then(count => {
|
||||
expect(count).toEqual(1);
|
||||
return database;
|
||||
return { database, client };
|
||||
});
|
||||
})
|
||||
.then(database => {
|
||||
.then(({ database, client }) => {
|
||||
// Verify the existance of the fs.files document
|
||||
return database
|
||||
.collection('fs.chunks')
|
||||
.count()
|
||||
.then(count => {
|
||||
expect(count).toEqual(1);
|
||||
return database.close();
|
||||
return client.close();
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
@@ -59,24 +66,25 @@ describe_only_db('mongo')('GridStoreAdapter', () => {
|
||||
})
|
||||
.then(() => {
|
||||
return MongoClient.connect(databaseURI)
|
||||
.then(database => {
|
||||
.then(client => {
|
||||
const database = client.db(client.s.options.dbName);
|
||||
// Verify the existance of the fs.files document
|
||||
return database
|
||||
.collection('fs.files')
|
||||
.count()
|
||||
.then(count => {
|
||||
expect(count).toEqual(0);
|
||||
return database;
|
||||
return { database, client };
|
||||
});
|
||||
})
|
||||
.then(database => {
|
||||
.then(({ database, client }) => {
|
||||
// Verify the existance of the fs.files document
|
||||
return database
|
||||
.collection('fs.chunks')
|
||||
.count()
|
||||
.then(count => {
|
||||
expect(count).toEqual(0);
|
||||
return database.close();
|
||||
return client.close();
|
||||
});
|
||||
});
|
||||
})
|
||||
@@ -689,7 +689,7 @@ describe('Parse.File testing', () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe_only_db('mongo')('Gridstore Range tests', () => {
|
||||
xdescribe('Gridstore Range tests', () => {
|
||||
it('supports range requests', done => {
|
||||
const headers = {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
@@ -796,7 +796,7 @@ describe('Parse.File testing', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('supports getting last n bytes', done => {
|
||||
xit('supports getting last n bytes', done => {
|
||||
const headers = {
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'X-Parse-Application-Id': 'test',
|
||||
|
||||
@@ -277,11 +277,8 @@ describe('Parse.User testing', () => {
|
||||
expect(newUser).not.toBeUndefined();
|
||||
});
|
||||
|
||||
it('should be let masterKey lock user out with authData', done => {
|
||||
let objectId;
|
||||
let sessionToken;
|
||||
|
||||
rp.post({
|
||||
it('should be let masterKey lock user out with authData', async () => {
|
||||
const body = await rp.post({
|
||||
url: 'http://localhost:8378/1/classes/_User',
|
||||
headers: {
|
||||
'X-Parse-Application-Id': Parse.applicationId,
|
||||
@@ -291,41 +288,32 @@ describe('Parse.User testing', () => {
|
||||
key: 'value',
|
||||
authData: { anonymous: { id: '00000000-0000-0000-0000-000000000001' } },
|
||||
},
|
||||
})
|
||||
.then(body => {
|
||||
objectId = body.objectId;
|
||||
sessionToken = body.sessionToken;
|
||||
expect(sessionToken).toBeDefined();
|
||||
expect(objectId).toBeDefined();
|
||||
const user = new Parse.User();
|
||||
user.id = objectId;
|
||||
const ACL = new Parse.ACL();
|
||||
user.setACL(ACL);
|
||||
return user.save(null, { useMasterKey: true });
|
||||
})
|
||||
.then(() => {
|
||||
// update the user
|
||||
const options = {
|
||||
url: `http://localhost:8378/1/classes/_User/`,
|
||||
headers: {
|
||||
'X-Parse-Application-Id': Parse.applicationId,
|
||||
'X-Parse-REST-API-Key': 'rest',
|
||||
},
|
||||
json: {
|
||||
key: 'otherValue',
|
||||
authData: {
|
||||
anonymous: { id: '00000000-0000-0000-0000-000000000001' },
|
||||
},
|
||||
},
|
||||
};
|
||||
return rp.post(options);
|
||||
})
|
||||
.then(res => {
|
||||
// Because the user is locked out, this should behave as creating a new user
|
||||
expect(res.objectId).not.toEqual(objectId);
|
||||
})
|
||||
.then(done)
|
||||
.catch(done.fail);
|
||||
});
|
||||
const objectId = body.objectId;
|
||||
const sessionToken = body.sessionToken;
|
||||
expect(sessionToken).toBeDefined();
|
||||
expect(objectId).toBeDefined();
|
||||
const user = new Parse.User();
|
||||
user.id = objectId;
|
||||
const ACL = new Parse.ACL();
|
||||
user.setACL(ACL);
|
||||
await user.save(null, { useMasterKey: true });
|
||||
// update the user
|
||||
const options = {
|
||||
url: `http://localhost:8378/1/classes/_User/`,
|
||||
headers: {
|
||||
'X-Parse-Application-Id': Parse.applicationId,
|
||||
'X-Parse-REST-API-Key': 'rest',
|
||||
},
|
||||
json: {
|
||||
key: 'otherValue',
|
||||
authData: {
|
||||
anonymous: { id: '00000000-0000-0000-0000-000000000001' },
|
||||
},
|
||||
},
|
||||
};
|
||||
const res = await rp.post(options);
|
||||
expect(res.objectId).not.toEqual(objectId);
|
||||
});
|
||||
|
||||
it('user login with files', done => {
|
||||
|
||||
@@ -14,33 +14,36 @@ describe_only_db('mongo')('Read preference option', () => {
|
||||
const obj1 = new Parse.Object('MyObject');
|
||||
obj1.set('boolKey', true);
|
||||
|
||||
Parse.Object.saveAll([obj0, obj1]).then(() => {
|
||||
spyOn(databaseAdapter.database.serverConfig, 'cursor').and.callThrough();
|
||||
Parse.Object.saveAll([obj0, obj1])
|
||||
.then(() => {
|
||||
spyOn(
|
||||
databaseAdapter.database.serverConfig,
|
||||
'cursor'
|
||||
).and.callThrough();
|
||||
|
||||
const query = new Parse.Query('MyObject');
|
||||
query.equalTo('boolKey', false);
|
||||
const query = new Parse.Query('MyObject');
|
||||
query.equalTo('boolKey', false);
|
||||
|
||||
query.find().then(results => {
|
||||
expect(results.length).toBe(1);
|
||||
expect(results[0].get('boolKey')).toBe(false);
|
||||
return query.find().then(results => {
|
||||
expect(results.length).toBe(1);
|
||||
expect(results[0].get('boolKey')).toBe(false);
|
||||
|
||||
let myObjectReadPreference = null;
|
||||
databaseAdapter.database.serverConfig.cursor.calls
|
||||
.all()
|
||||
.forEach(call => {
|
||||
if (call.args[0].indexOf('MyObject') >= 0) {
|
||||
myObjectReadPreference = true;
|
||||
expect(call.args[2].readPreference.preference).toBe(
|
||||
ReadPreference.PRIMARY
|
||||
);
|
||||
}
|
||||
});
|
||||
let myObjectReadPreference = null;
|
||||
databaseAdapter.database.serverConfig.cursor.calls
|
||||
.all()
|
||||
.forEach(call => {
|
||||
if (call.args[0].indexOf('MyObject') >= 0) {
|
||||
myObjectReadPreference = true;
|
||||
expect(call.args[2].readPreference).toBe(null);
|
||||
}
|
||||
});
|
||||
|
||||
expect(myObjectReadPreference).toBe(true);
|
||||
expect(myObjectReadPreference).toBe(true);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
|
||||
it('should preserve the read preference set (#4831)', async () => {
|
||||
@@ -453,7 +456,7 @@ describe_only_db('mongo')('Read preference option', () => {
|
||||
obj1.set('boolKey', true);
|
||||
|
||||
Parse.Object.saveAll([obj0, obj1]).then(() => {
|
||||
spyOn(databaseAdapter.database.serverConfig, 'command').and.callThrough();
|
||||
spyOn(databaseAdapter.database.serverConfig, 'cursor').and.callThrough();
|
||||
|
||||
Parse.Cloud.beforeFind('MyObject', req => {
|
||||
req.readPreference = 'SECONDARY';
|
||||
@@ -466,10 +469,12 @@ describe_only_db('mongo')('Read preference option', () => {
|
||||
expect(result).toBe(1);
|
||||
|
||||
let myObjectReadPreference = null;
|
||||
databaseAdapter.database.serverConfig.command.calls
|
||||
databaseAdapter.database.serverConfig.cursor.calls
|
||||
.all()
|
||||
.forEach(call => {
|
||||
myObjectReadPreference = call.args[2].readPreference.preference;
|
||||
if (call.args[0].indexOf('MyObject') >= 0) {
|
||||
myObjectReadPreference = call.args[2].readPreference.preference;
|
||||
}
|
||||
});
|
||||
|
||||
expect(myObjectReadPreference).toEqual(ReadPreference.SECONDARY);
|
||||
@@ -523,15 +528,11 @@ describe_only_db('mongo')('Read preference option', () => {
|
||||
.forEach(call => {
|
||||
if (call.args[0].indexOf('MyObject0') >= 0) {
|
||||
myObjectReadPreference0 = true;
|
||||
expect(call.args[2].readPreference.preference).toBe(
|
||||
ReadPreference.PRIMARY
|
||||
);
|
||||
expect(call.args[2].readPreference).toBe(null);
|
||||
}
|
||||
if (call.args[0].indexOf('MyObject1') >= 0) {
|
||||
myObjectReadPreference1 = true;
|
||||
expect(call.args[2].readPreference.preference).toBe(
|
||||
ReadPreference.PRIMARY
|
||||
);
|
||||
expect(call.args[2].readPreference).toBe(null);
|
||||
}
|
||||
if (call.args[0].indexOf('MyObject2') >= 0) {
|
||||
myObjectReadPreference2 = call.args[2].readPreference.preference;
|
||||
@@ -652,15 +653,11 @@ describe_only_db('mongo')('Read preference option', () => {
|
||||
.forEach(call => {
|
||||
if (call.args[0].indexOf('MyObject0') >= 0) {
|
||||
myObjectReadPreference0 = true;
|
||||
expect(call.args[2].readPreference.preference).toBe(
|
||||
ReadPreference.PRIMARY
|
||||
);
|
||||
expect(call.args[2].readPreference).toBe(null);
|
||||
}
|
||||
if (call.args[0].indexOf('MyObject1') >= 0) {
|
||||
myObjectReadPreference1 = true;
|
||||
expect(call.args[2].readPreference.preference).toBe(
|
||||
ReadPreference.PRIMARY
|
||||
);
|
||||
expect(call.args[2].readPreference).toBe(null);
|
||||
}
|
||||
if (call.args[0].indexOf('MyObject2') >= 0) {
|
||||
myObjectReadPreference2 = call.args[2].readPreference.preference;
|
||||
|
||||
@@ -33,8 +33,8 @@ const cache = require('../lib/cache').default;
|
||||
const ParseServer = require('../lib/index').ParseServer;
|
||||
const path = require('path');
|
||||
const TestUtils = require('../lib/TestUtils');
|
||||
const GridStoreAdapter = require('../lib/Adapters/Files/GridStoreAdapter')
|
||||
.GridStoreAdapter;
|
||||
const GridFSBucketAdapter = require('../lib/Adapters/Files/GridFSBucketAdapter')
|
||||
.GridFSBucketAdapter;
|
||||
const FSAdapter = require('@parse/fs-files-adapter');
|
||||
const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/PostgresStorageAdapter')
|
||||
.default;
|
||||
@@ -77,7 +77,7 @@ let filesAdapter;
|
||||
on_db(
|
||||
'mongo',
|
||||
() => {
|
||||
filesAdapter = new GridStoreAdapter(mongoURI);
|
||||
filesAdapter = new GridFSBucketAdapter(mongoURI);
|
||||
},
|
||||
() => {
|
||||
filesAdapter = new FSAdapter();
|
||||
|
||||
Reference in New Issue
Block a user