Skip to content

Commit 19080ed

Browse files
maningaflovilmart
authored andcommitted
#506 fix: Deleting a file does not delete from fs.files (#1758)
* #506 fix: Deleting a file does not delete from fs.files * test added to check that GridStoreAdapter deleteFile removes from fs.files and fs.chunks
1 parent b7d15cc commit 19080ed

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

spec/GridStoreAdapter.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var MongoClient = require("mongodb").MongoClient;
2+
var GridStore = require("mongodb").GridStore;
3+
4+
var GridStoreAdapter = require("../src/Adapters/Files/GridStoreAdapter").GridStoreAdapter;
5+
var Config = require("../src/Config");
6+
var FilesController = require('../src/Controllers/FilesController').default;
7+
8+
9+
// Small additional tests to improve overall coverage
10+
describe("GridStoreAdapter",() =>{
11+
it("should properly instanciate the GridStore when deleting a file", (done) => {
12+
13+
var databaseURI = 'mongodb://localhost:27017/parse';
14+
var config = new Config(Parse.applicationId);
15+
var gridStoreAdapter = new GridStoreAdapter(databaseURI);
16+
var filesController = new FilesController(gridStoreAdapter);
17+
18+
// save original unlink before redefinition
19+
var originalUnlink = GridStore.prototype.unlink;
20+
21+
var gridStoreMode;
22+
23+
// new unlink method that will capture the mode in which GridStore was opened
24+
GridStore.prototype.unlink = function() {
25+
26+
// restore original unlink during first call
27+
GridStore.prototype.unlink = originalUnlink;
28+
29+
gridStoreMode = this.mode;
30+
31+
return originalUnlink.call(this);
32+
};
33+
34+
35+
filesController.createFile(config, 'myFilename.txt', 'my file content', 'text/plain')
36+
.then(myFile => {
37+
38+
return MongoClient.connect(databaseURI)
39+
.then(database => {
40+
41+
// Verify the existance of the fs.files document
42+
return database.collection('fs.files').count().then(count => {
43+
expect(count).toEqual(1);
44+
return database;
45+
});
46+
})
47+
.then(database => {
48+
49+
// Verify the existance of the fs.files document
50+
return database.collection('fs.chunks').count().then(count => {
51+
expect(count).toEqual(1);
52+
return database.close();
53+
});
54+
})
55+
.then(() => {
56+
return filesController.deleteFile(config, myFile.name);
57+
});
58+
})
59+
.then(() => {
60+
return MongoClient.connect(databaseURI)
61+
.then(database => {
62+
63+
// Verify the existance of the fs.files document
64+
return database.collection('fs.files').count().then(count => {
65+
expect(count).toEqual(0);
66+
return database;
67+
});
68+
})
69+
.then(database => {
70+
71+
// Verify the existance of the fs.files document
72+
return database.collection('fs.chunks').count().then(count => {
73+
expect(count).toEqual(0);
74+
return database.close();
75+
});
76+
});
77+
})
78+
.then(() => {
79+
// Verify that gridStore was opened in read only mode
80+
expect(gridStoreMode).toEqual('r');
81+
82+
done();
83+
})
84+
.catch(fail);
85+
86+
})
87+
});

src/Adapters/Files/GridStoreAdapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class GridStoreAdapter extends FilesAdapter {
4343

4444
deleteFile(filename: string) {
4545
return this._connect().then(database => {
46-
let gridStore = new GridStore(database, filename, 'w');
46+
let gridStore = new GridStore(database, filename, 'r');
4747
return gridStore.open();
4848
}).then((gridStore) => {
4949
return gridStore.unlink();

0 commit comments

Comments
 (0)