@@ -25,15 +25,34 @@ describe('unit tests on fetch functions', function() {
25
25
var db = { } ;
26
26
db . admin = function ( ) {
27
27
return {
28
- // add more db methods here as needed.
29
-
30
28
// buildInfo is a separate function on the admin object
31
29
buildInfo : function ( callback ) {
32
30
return callback ( err , res ) ;
33
31
} ,
34
32
// all other commands return the global err/res results
35
33
command : function ( command , options , callback ) {
36
34
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
+ } ;
37
56
}
38
57
} ;
39
58
} ;
@@ -108,7 +127,7 @@ describe('unit tests on fetch functions', function() {
108
127
var results = { } ;
109
128
110
129
beforeEach ( function ( ) {
111
- results . userInfo = fixtures . USER_INFO ;
130
+ results . userInfo = fixtures . USER_INFO_JOHN ;
112
131
} ) ;
113
132
114
133
it ( 'should ignore auth errors gracefully' , function ( done ) {
@@ -136,20 +155,152 @@ describe('unit tests on fetch functions', function() {
136
155
} ) ;
137
156
138
157
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
+ } ) ;
141
180
} ) ;
181
+
142
182
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
+ } ) ;
145
211
} ) ;
212
+
146
213
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
+ } ) ;
150
236
} ) ;
237
+
151
238
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
+ } ) ;
154
305
} ) ;
155
306
} ) ;
0 commit comments