@@ -166,7 +166,120 @@ describe('API tests', function() {
166166
167167 describe ( 'API' , function ( ) {
168168
169- describe ( 'data read' , function ( ) {
169+ describe . only ( 'document read' , function ( ) {
170+
171+ var bData , status ;
172+
173+ function getItem ( id , query , cb ) {
174+ var mockReq = {
175+ url : '/b_using_options/' + id ,
176+ params : { resourceName : 'b_using_options' , id : id } ,
177+ query : query
178+ } ;
179+ var mockRes = {
180+ status : function ( data ) {
181+ status = data ;
182+ return this ;
183+ } ,
184+ send : function ( data ) {
185+ cb ( null , data ) ;
186+ }
187+ } ;
188+ fng . entityGet ( ) ( mockReq , mockRes ) ;
189+ }
190+
191+ describe ( 'simple' , function ( ) {
192+
193+ before ( function ( done ) {
194+ getItem ( '519a6075b320153869b175e0' , { } , function ( err , result ) {
195+ if ( err ) { throw err ; }
196+ bData = result ;
197+ done ( ) ;
198+ } ) ;
199+ } ) ;
200+
201+ it ( 'should send a record' , function ( ) {
202+ assert ( status === 200 ) ;
203+ assert ( bData ) ;
204+ } ) ;
205+
206+ it ( 'should not send secure fields of a modified schema' , function ( ) {
207+ assert ( bData . surname , 'Must send surname' ) ;
208+ assert ( bData . forename , 'Must send forename' ) ;
209+ assert ( ! bData . login , 'Must not send secure login field' ) ;
210+ assert ( ! bData . passwordHash , 'Must not send secure password hash field' ) ;
211+ assert ( bData . email , 'Must send email' ) ;
212+ assert ( bData . weight , 'Must send weight' ) ;
213+ assert ( bData . accepted , 'Must send accepted' ) ;
214+ assert ( bData . interviewScore , 'Must send interview score' ) ;
215+ assert ( bData . freeText , 'Must send freetext' ) ;
216+ } ) ;
217+
218+ it ( 'should not send secure fields of a modified subschema' , function ( ) {
219+ assert ( bData . address . line1 , 'Must send line1' ) ;
220+ assert ( bData . address . town , 'Must send town' ) ;
221+ assert ( bData . address . postcode , 'Must send postcode' ) ;
222+ assert ( ! bData . address . surveillance , 'Must not send secure surveillance field' ) ;
223+ } ) ;
224+
225+ } ) ;
226+
227+ describe ( 'projection' , function ( ) {
228+
229+ before ( function ( done ) {
230+ getItem ( '519a6075b320153869b175e0' , { p : { surname :1 , forename :1 , login :1 , 'address.line1' :1 , 'address.surveillance' : 1 } } , function ( err , result ) {
231+ if ( err ) { throw err ; }
232+ bData = result ;
233+ done ( ) ;
234+ } ) ;
235+ } ) ;
236+
237+ it ( 'should send a record' , function ( ) {
238+ assert ( bData ) ;
239+ assert ( status === 200 ) ;
240+ } ) ;
241+
242+ it ( 'should not send secure fields of a modified schema' , function ( ) {
243+ assert ( bData . surname , 'Must send surname' ) ;
244+ assert ( bData . forename , 'Must send forename' ) ;
245+ assert ( ! bData . login , 'Must not send secure login field' ) ;
246+ assert ( ! bData . passwordHash , 'Must not send secure password hash field' ) ;
247+ assert ( ! bData . email , 'Must not send email' ) ;
248+ assert ( ! bData . weight , 'Must not send weight' ) ;
249+ assert ( ! bData . accepted , 'Must not send accepted' ) ;
250+ assert ( ! bData . interviewScore , 'Must not send interview score' ) ;
251+ assert ( ! bData . freeText , 'Must not send freetext' ) ;
252+ } ) ;
253+
254+ it ( 'should not send secure fields of a modified subschema' , function ( ) {
255+ assert ( bData . address . line1 , 'Must send line1' ) ;
256+ assert ( ! bData . address . town , 'Must not send town' ) ;
257+ assert ( ! bData . address . postcode , 'Must not send postcode' ) ;
258+ assert ( ! bData . address . surveillance , 'Must not send secure surveillance field' ) ;
259+ } ) ;
260+
261+ } ) ;
262+
263+ describe ( 'findFunc filter' , function ( ) {
264+
265+ before ( function ( done ) {
266+ getItem ( '519a6075b440153869b155e0' , { } , function ( err , result ) {
267+ if ( err ) { throw err ; }
268+ bData = result ;
269+ done ( ) ;
270+ } ) ;
271+ } ) ;
272+
273+ it ( 'should not send a record' , function ( ) {
274+ assert ( ! bData || ! bData . success ) ;
275+ assert ( status === 404 ) ;
276+ } ) ;
277+
278+ } ) ;
279+
280+ } ) ;
281+
282+ describe ( 'collection read' , function ( ) {
170283
171284 var aData , aPtr , bData , bPtr ;
172285
@@ -248,6 +361,85 @@ describe('API tests', function() {
248361
249362 } ) ;
250363
364+ describe ( 'collection projection' , function ( ) {
365+
366+ var aData , aPtr , bData , bPtr ;
367+
368+ function getCollectionProjection ( model , proj , cb ) {
369+ var mockReq = {
370+ url : '/' + model ,
371+ query : { p : JSON . stringify ( proj ) } ,
372+ params : { resourceName : model }
373+ } ;
374+ var mockRes = {
375+ send : function ( data ) {
376+ cb ( null , data ) ;
377+ }
378+ } ;
379+ fng . collectionGet ( ) ( mockReq , mockRes ) ;
380+ }
381+
382+ before ( function ( done ) {
383+ async . auto (
384+ {
385+ aData : function ( cb ) {
386+ getCollectionProjection ( 'a_unadorned_mongoose' , { forename :0 , weight :0 } , cb ) ;
387+ } ,
388+ bData : function ( cb ) {
389+ getCollectionProjection ( 'b_using_options' , { surname : 1 , weight : 1 , login : 1 , 'address.surveillance' : 1 , 'address.line1' : 1 } , cb ) ;
390+ }
391+ } ,
392+ function ( err , results ) {
393+ if ( err ) {
394+ throw err ;
395+ }
396+ aData = results [ 'aData' ] ;
397+ aPtr = aData . find ( function ( obj ) {
398+ return obj . surname === 'TestPerson1'
399+ } ) ;
400+ bData = results [ 'bData' ] ;
401+ bPtr = bData . find ( function ( obj ) {
402+ return obj . surname === 'IsAccepted1'
403+ } ) ;
404+ done ( ) ;
405+ }
406+ ) ;
407+ } ) ;
408+
409+ it ( 'should send the right number of records' , function ( ) {
410+ assert . strictEqual ( aData . length , 2 ) ;
411+ } ) ;
412+
413+ it ( 'should suppress unselected fields' , function ( ) {
414+ assert ( aPtr . surname , 'must send surname' ) ;
415+ assert ( ! aPtr . forename , 'must not send forename' ) ;
416+ assert ( ! aPtr . weight , 'must not send weight' ) ;
417+ assert ( aPtr . eyeColour , 'must send eyeColour' ) ;
418+ assert ( aPtr . dateOfBirth , 'must send dob' ) ;
419+ assert . strictEqual ( aPtr . accepted , false , 'must send accepted' ) ;
420+ } ) ;
421+
422+ it ( 'should send select fields unless they are secure' , function ( ) {
423+ assert ( bPtr . surname , 'Must send surname' ) ;
424+ assert ( ! bPtr . forename , 'Must not send forename' ) ;
425+ assert ( ! bPtr . login , 'Must not send secure login field' ) ;
426+ assert ( ! bPtr . passwordHash , 'Must not send secure password hash field' ) ;
427+ assert ( ! bPtr . email , 'Must not send email' ) ;
428+ assert ( bPtr . weight , 'Must send weight' ) ;
429+ assert ( ! bPtr . accepted , 'Must not send accepted' ) ;
430+ assert ( ! bPtr . interviewScore , 'Must not send interview score' ) ;
431+ assert ( ! bPtr . freeText , 'Must not send freetext' ) ;
432+ } ) ;
433+
434+ it ( 'should not send secure fields of a modified subschema' , function ( ) {
435+ assert ( bPtr . address . line1 , 'Must send line1' ) ;
436+ assert ( ! bPtr . address . town , 'Must not send town' ) ;
437+ assert ( ! bPtr . address . postcode , 'Must not send postcode' ) ;
438+ assert ( ! bPtr . address . surveillance , 'Must not send secure surveillance field' ) ;
439+ } ) ;
440+
441+ } ) ;
442+
251443 describe ( 'data update' , function ( ) {
252444
253445 var id ;
0 commit comments