From 209317faec2184e3032e08781976728626009cf2 Mon Sep 17 00:00:00 2001 From: David KEITA Date: Wed, 11 May 2016 15:22:40 +0200 Subject: [PATCH 1/2] #506 fix: Deleting a file does not delete from fs.files --- src/Adapters/Files/GridStoreAdapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Files/GridStoreAdapter.js b/src/Adapters/Files/GridStoreAdapter.js index 1d469ee07c..d7844a0b5f 100644 --- a/src/Adapters/Files/GridStoreAdapter.js +++ b/src/Adapters/Files/GridStoreAdapter.js @@ -43,7 +43,7 @@ export class GridStoreAdapter extends FilesAdapter { deleteFile(filename: string) { return this._connect().then(database => { - let gridStore = new GridStore(database, filename, 'w'); + let gridStore = new GridStore(database, filename, 'r'); return gridStore.open(); }).then((gridStore) => { return gridStore.unlink(); From cb58e4685f11f8c3d404569d333356f733c7a478 Mon Sep 17 00:00:00 2001 From: David KEITA Date: Thu, 26 May 2016 18:13:21 +0200 Subject: [PATCH 2/2] test added to check that GridStoreAdapter deleteFile removes from fs.files and fs.chunks --- spec/GridStoreAdapter.js | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 spec/GridStoreAdapter.js diff --git a/spec/GridStoreAdapter.js b/spec/GridStoreAdapter.js new file mode 100644 index 0000000000..78c848f33b --- /dev/null +++ b/spec/GridStoreAdapter.js @@ -0,0 +1,87 @@ +var MongoClient = require("mongodb").MongoClient; +var GridStore = require("mongodb").GridStore; + +var GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter; +var Config = require("../src/Config"); +var FilesController = require('../src/Controllers/FilesController').default; + + +// Small additional tests to improve overall coverage +describe("GridStoreAdapter",() =>{ + it("should properly instanciate the GridStore when deleting a file", (done) => { + + var databaseURI = 'mongodb://localhost:27017/parse'; + var config = new Config(Parse.applicationId); + var gridStoreAdapter = new GridStoreAdapter(databaseURI); + var filesController = new FilesController(gridStoreAdapter); + + // save original unlink before redefinition + var originalUnlink = GridStore.prototype.unlink; + + var gridStoreMode; + + // new unlink method that will capture the mode in which GridStore was opened + GridStore.prototype.unlink = function() { + + // restore original unlink during first call + GridStore.prototype.unlink = originalUnlink; + + gridStoreMode = this.mode; + + return originalUnlink.call(this); + }; + + + filesController.createFile(config, 'myFilename.txt', 'my file content', 'text/plain') + .then(myFile => { + + return MongoClient.connect(databaseURI) + .then(database => { + + // Verify the existance of the fs.files document + return database.collection('fs.files').count().then(count => { + expect(count).toEqual(1); + return database; + }); + }) + .then(database => { + + // Verify the existance of the fs.files document + return database.collection('fs.chunks').count().then(count => { + expect(count).toEqual(1); + return database.close(); + }); + }) + .then(() => { + return filesController.deleteFile(config, myFile.name); + }); + }) + .then(() => { + return MongoClient.connect(databaseURI) + .then(database => { + + // Verify the existance of the fs.files document + return database.collection('fs.files').count().then(count => { + expect(count).toEqual(0); + return database; + }); + }) + .then(database => { + + // Verify the existance of the fs.files document + return database.collection('fs.chunks').count().then(count => { + expect(count).toEqual(0); + return database.close(); + }); + }); + }) + .then(() => { + // Verify that gridStore was opened in read only mode + expect(gridStoreMode).toEqual('r'); + + done(); + }) + .catch(fail); + + }) +});