Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit d555904

Browse files
committed
Merge pull request #21 from Machyne/INT-962-tests
INT-962 add tests for list collection fix
2 parents 4725aa5 + 0e600b2 commit d555904

File tree

2 files changed

+616
-444
lines changed

2 files changed

+616
-444
lines changed

test/fetch-mocked.test.js

Lines changed: 163 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,34 @@ describe('unit tests on fetch functions', function() {
2525
var db = {};
2626
db.admin = function() {
2727
return {
28-
// add more db methods here as needed.
29-
3028
// buildInfo is a separate function on the admin object
3129
buildInfo: function(callback) {
3230
return callback(err, res);
3331
},
3432
// all other commands return the global err/res results
3533
command: function(command, options, callback) {
3634
return callback(err, res);
35+
},
36+
databaseName: 'admin',
37+
// listCollections is a separate function on the admin object
38+
listCollections: function() {
39+
return {
40+
toArray: function(callback) {
41+
return callback(err, res);
42+
}
43+
};
44+
}
45+
};
46+
};
47+
db.db = function(databaseName) {
48+
return {
49+
databaseName: databaseName,
50+
listCollections: function() {
51+
return {
52+
toArray: function(callback) {
53+
return callback(err, res);
54+
}
55+
};
3756
}
3857
};
3958
};
@@ -108,7 +127,7 @@ describe('unit tests on fetch functions', function() {
108127
var results = {};
109128

110129
beforeEach(function() {
111-
results.userInfo = fixtures.USER_INFO;
130+
results.userInfo = fixtures.USER_INFO_JOHN;
112131
});
113132

114133
it('should ignore auth errors gracefully', function(done) {
@@ -136,20 +155,152 @@ describe('unit tests on fetch functions', function() {
136155
});
137156

138157
describe('getAllowedDatabases', function() {
139-
it('should return the correct results');
140-
// ...
158+
var results = {};
159+
160+
it('should return all databases for which the user can list collections', function(done) {
161+
results.userInfo = fixtures.USER_INFO_JOHN;
162+
163+
fetch.getAllowedDatabases(function(err, res) {
164+
assert.equal(err, null);
165+
res.sort();
166+
assert.deepEqual(res, ['accounts', 'products', 'reporting', 'sales']);
167+
done();
168+
}, results);
169+
});
170+
171+
it('should return empty list for users with no list collections', function(done) {
172+
results.userInfo = fixtures.USER_INFO_LISTDB_ONLY;
173+
174+
fetch.getAllowedDatabases(function(err, res) {
175+
assert.equal(err, null);
176+
assert.deepEqual(res, []);
177+
done();
178+
}, results);
179+
});
141180
});
181+
142182
describe('getAllowedCollections', function() {
143-
it('should return the correct results');
144-
// ...
183+
var results = {};
184+
185+
it('should return all collections the user info says it can access', function(done) {
186+
results.userInfo = fixtures.USER_INFO_JOHN;
187+
188+
fetch.getAllowedCollections(function(err, res) {
189+
assert.equal(err, null);
190+
var expected = [
191+
{
192+
'_id': 'tenants.mongodb',
193+
'database': 'tenants',
194+
'name': 'mongodb'
195+
}
196+
];
197+
assert.deepEqual(res, expected);
198+
done();
199+
}, results);
200+
});
201+
202+
it('should return empty list for users with no collections', function(done) {
203+
results.userInfo = fixtures.USER_INFO_LISTDB_ONLY;
204+
205+
fetch.getAllowedCollections(function(err, res) {
206+
assert.equal(err, null);
207+
assert.deepEqual(res, []);
208+
done();
209+
}, results);
210+
});
145211
});
212+
146213
describe('getDatabaseCollections', function() {
147-
it('should ignore auth errors gracefully');
148-
it('should pass on other errors from the listCollections command');
149-
// ...
214+
var results = {};
215+
it('should ignore auth errors gracefully', function(done) {
216+
results.db = makeMockDB(new Error('not authorized on fooBarDatabase to execute command '
217+
+ '{listCollections: true, filter: {}, cursor: {}'), null);
218+
219+
fetch.getDatabaseCollections(results.db.admin(), function(err, res) {
220+
assert.equal(err, null);
221+
assert.deepEqual(res, []);
222+
done();
223+
});
224+
});
225+
226+
it('should pass on other errors from the listCollections command', function(done) {
227+
results.db = makeMockDB(new Error('some other error from list collections'), null);
228+
229+
fetch.getDatabaseCollections(results.db.admin(), function(err, res) {
230+
assert.ok(err);
231+
assert.equal(err.command, 'listCollections');
232+
assert.deepEqual(res, null);
233+
done();
234+
});
235+
});
150236
});
237+
151238
describe('listCollections', function() {
152-
it('should merge the two collection lists correctly');
153-
// ...
239+
var results = {};
240+
241+
beforeEach(function() {
242+
results.databases = [
243+
{
244+
'name': 'accounts'
245+
},
246+
{
247+
'name': 'products'
248+
},
249+
{
250+
'name': 'reporting'
251+
},
252+
{
253+
'name': 'sales'
254+
}
255+
];
256+
});
257+
258+
it('should lists all collections for each listable db', function(done) {
259+
results.userInfo = fixtures.USER_INFO_JOHN;
260+
results.db = makeMockDB(null, [{
261+
'name': 'testCol'
262+
}]);
263+
264+
fetch.listCollections(function(err, res) {
265+
assert.equal(err, null);
266+
res.sort();
267+
var expected = [
268+
{
269+
'_id': 'accounts.testCol',
270+
'database': 'accounts',
271+
'name': 'testCol'
272+
},
273+
{
274+
'_id': 'products.testCol',
275+
'database': 'products',
276+
'name': 'testCol'
277+
},
278+
{
279+
'_id': 'reporting.testCol',
280+
'database': 'reporting',
281+
'name': 'testCol'
282+
},
283+
{
284+
'_id': 'sales.testCol',
285+
'database': 'sales',
286+
'name': 'testCol'
287+
}
288+
];
289+
expected.sort();
290+
assert.deepEqual(res, expected);
291+
done();
292+
}, results);
293+
});
294+
295+
it('should be empty for no privileges', function(done) {
296+
results.userInfo = fixtures.USER_INFO_LISTDB_ONLY;
297+
results.db = makeMockDB(null, []);
298+
299+
fetch.listCollections(function(err, res) {
300+
assert.equal(err, null);
301+
assert.deepEqual(res, []);
302+
done();
303+
}, results);
304+
});
154305
});
155306
});

0 commit comments

Comments
 (0)