@@ -236,6 +236,57 @@ describe('DatabaseController', function () {
236236 done ( ) ;
237237 } ) ;
238238
239+ it ( 'should not return a $or operation if the query involves one of the two fields also used as array/pointer permissions' , done => {
240+ const clp = buildCLP ( [ 'users' , 'user' ] ) ;
241+ const query = { a : 'b' , user : createUserPointer ( USER_ID ) } ;
242+ schemaController . testPermissionsForClassName
243+ . withArgs ( CLASS_NAME , ACL_GROUP , OPERATION )
244+ . and . returnValue ( false ) ;
245+ schemaController . getClassLevelPermissions . withArgs ( CLASS_NAME ) . and . returnValue ( clp ) ;
246+ schemaController . getExpectedType
247+ . withArgs ( CLASS_NAME , 'user' )
248+ . and . returnValue ( { type : 'Pointer' } ) ;
249+ schemaController . getExpectedType
250+ . withArgs ( CLASS_NAME , 'users' )
251+ . and . returnValue ( { type : 'Array' } ) ;
252+ const output = databaseController . addPointerPermissions (
253+ schemaController ,
254+ CLASS_NAME ,
255+ OPERATION ,
256+ query ,
257+ ACL_GROUP
258+ ) ;
259+ expect ( output ) . toEqual ( { ...query , user : createUserPointer ( USER_ID ) } ) ;
260+ done ( ) ;
261+ } ) ;
262+
263+ it ( 'should not return a $or operation if the query involves one of the fields also used as array/pointer permissions' , done => {
264+ const clp = buildCLP ( [ 'user' , 'users' , 'userObject' ] ) ;
265+ const query = { a : 'b' , user : createUserPointer ( USER_ID ) } ;
266+ schemaController . testPermissionsForClassName
267+ . withArgs ( CLASS_NAME , ACL_GROUP , OPERATION )
268+ . and . returnValue ( false ) ;
269+ schemaController . getClassLevelPermissions . withArgs ( CLASS_NAME ) . and . returnValue ( clp ) ;
270+ schemaController . getExpectedType
271+ . withArgs ( CLASS_NAME , 'user' )
272+ . and . returnValue ( { type : 'Pointer' } ) ;
273+ schemaController . getExpectedType
274+ . withArgs ( CLASS_NAME , 'users' )
275+ . and . returnValue ( { type : 'Array' } ) ;
276+ schemaController . getExpectedType
277+ . withArgs ( CLASS_NAME , 'userObject' )
278+ . and . returnValue ( { type : 'Object' } ) ;
279+ const output = databaseController . addPointerPermissions (
280+ schemaController ,
281+ CLASS_NAME ,
282+ OPERATION ,
283+ query ,
284+ ACL_GROUP
285+ ) ;
286+ expect ( output ) . toEqual ( { ...query , user : createUserPointer ( USER_ID ) } ) ;
287+ done ( ) ;
288+ } ) ;
289+
239290 it ( 'should throw an error if for some unexpected reason the property specified in the CLP is neither a pointer nor an array' , done => {
240291 const clp = buildCLP ( [ 'user' ] ) ;
241292 const query = { a : 'b' } ;
@@ -265,6 +316,51 @@ describe('DatabaseController', function () {
265316 done ( ) ;
266317 } ) ;
267318 } ) ;
319+
320+ describe ( 'reduceOperations' , function ( ) {
321+ const databaseController = new DatabaseController ( ) ;
322+
323+ it ( 'objectToEntriesStrings' , done => {
324+ const output = databaseController . objectToEntriesStrings ( { a : 1 , b : 2 , c : 3 } ) ;
325+ expect ( output ) . toEqual ( [ '"a":1' , '"b":2' , '"c":3' ] ) ;
326+ done ( ) ;
327+ } ) ;
328+
329+ it ( 'reduceOrOperation' , done => {
330+ expect ( databaseController . reduceOrOperation ( { a : 1 } ) ) . toEqual ( { a : 1 } ) ;
331+ expect ( databaseController . reduceOrOperation ( { $or : [ { a : 1 } , { b : 2 } ] } ) ) . toEqual ( {
332+ $or : [ { a : 1 } , { b : 2 } ] ,
333+ } ) ;
334+ expect ( databaseController . reduceOrOperation ( { $or : [ { a : 1 } , { a : 2 } ] } ) ) . toEqual ( {
335+ $or : [ { a : 1 } , { a : 2 } ] ,
336+ } ) ;
337+ expect ( databaseController . reduceOrOperation ( { $or : [ { a : 1 } , { a : 1 } ] } ) ) . toEqual ( { a : 1 } ) ;
338+ expect (
339+ databaseController . reduceOrOperation ( { $or : [ { a : 1 , b : 2 , c : 3 } , { a : 1 } ] } )
340+ ) . toEqual ( { a : 1 } ) ;
341+ expect (
342+ databaseController . reduceOrOperation ( { $or : [ { b : 2 } , { a : 1 , b : 2 , c : 3 } ] } )
343+ ) . toEqual ( { b : 2 } ) ;
344+ done ( ) ;
345+ } ) ;
346+
347+ it ( 'reduceAndOperation' , done => {
348+ expect ( databaseController . reduceAndOperation ( { a : 1 } ) ) . toEqual ( { a : 1 } ) ;
349+ expect ( databaseController . reduceAndOperation ( { $and : [ { a : 1 } , { b : 2 } ] } ) ) . toEqual ( {
350+ $and : [ { a : 1 } , { b : 2 } ] ,
351+ } ) ;
352+ expect ( databaseController . reduceAndOperation ( { $and : [ { a : 1 } , { a : 2 } ] } ) ) . toEqual ( {
353+ $and : [ { a : 1 } , { a : 2 } ] ,
354+ } ) ;
355+ expect ( databaseController . reduceAndOperation ( { $and : [ { a : 1 } , { a : 1 } ] } ) ) . toEqual ( {
356+ a : 1 ,
357+ } ) ;
358+ expect (
359+ databaseController . reduceAndOperation ( { $and : [ { a : 1 , b : 2 , c : 3 } , { b : 2 } ] } )
360+ ) . toEqual ( { a : 1 , b : 2 , c : 3 } ) ;
361+ done ( ) ;
362+ } ) ;
363+ } ) ;
268364} ) ;
269365
270366function buildCLP ( pointerNames ) {
0 commit comments