@@ -886,4 +886,108 @@ describe.onServer('Remote Methods', function() {
886
886
// fails on time-out when not implemented correctly
887
887
} ) ;
888
888
} ) ;
889
+
890
+ describe ( 'Model.createOptionsFromRemotingContext' , function ( ) {
891
+ var app , TestModel , accessToken , userId , actualOptions ;
892
+
893
+ before ( setupAppAndRequest ) ;
894
+ before ( createUserAndAccessToken ) ;
895
+
896
+ it ( 'sets empty options.accessToken for anonymous requests' , function ( done ) {
897
+ request ( app ) . get ( '/TestModels/saveOptions' )
898
+ . expect ( 204 , function ( err ) {
899
+ if ( err ) return done ( err ) ;
900
+ expect ( actualOptions ) . to . eql ( { accessToken : null } ) ;
901
+ done ( ) ;
902
+ } ) ;
903
+ } ) ;
904
+
905
+ it ( 'sets options.accessToken for authorized requests' , function ( done ) {
906
+ request ( app ) . get ( '/TestModels/saveOptions' )
907
+ . set ( 'Authorization' , accessToken . id )
908
+ . expect ( 204 , function ( err ) {
909
+ if ( err ) return done ( err ) ;
910
+ expect ( actualOptions ) . to . have . property ( 'accessToken' ) ;
911
+ expect ( actualOptions . accessToken . toObject ( ) )
912
+ . to . eql ( accessToken . toObject ( ) ) ;
913
+ done ( ) ;
914
+ } ) ;
915
+ } ) ;
916
+
917
+ it ( 'allows "beforeRemote" hooks to contribute options' , function ( done ) {
918
+ TestModel . beforeRemote ( 'saveOptions' , function ( ctx , unused , next ) {
919
+ ctx . args . options . hooked = true ;
920
+ next ( ) ;
921
+ } ) ;
922
+
923
+ request ( app ) . get ( '/TestModels/saveOptions' )
924
+ . expect ( 204 , function ( err ) {
925
+ if ( err ) return done ( err ) ;
926
+ expect ( actualOptions ) . to . have . property ( 'hooked' , true ) ;
927
+ done ( ) ;
928
+ } ) ;
929
+ } ) ;
930
+
931
+ it ( 'allows apps to add options before remoting hooks' , function ( done ) {
932
+ TestModel . createOptionsFromRemotingContext = function ( ctx ) {
933
+ return { hooks : [ ] } ;
934
+ } ;
935
+
936
+ TestModel . beforeRemote ( 'saveOptions' , function ( ctx , unused , next ) {
937
+ ctx . args . options . hooks . push ( 'beforeRemote' ) ;
938
+ next ( ) ;
939
+ } ) ;
940
+
941
+ // In real apps, this code can live in a component or in a boot script
942
+ app . remotes ( ) . phases
943
+ . addBefore ( 'invoke' , 'options-from-request' )
944
+ . use ( function ( ctx , next ) {
945
+ ctx . args . options . hooks . push ( 'custom' ) ;
946
+ next ( ) ;
947
+ } ) ;
948
+
949
+ request ( app ) . get ( '/TestModels/saveOptions' )
950
+ . expect ( 204 , function ( err ) {
951
+ if ( err ) return done ( err ) ;
952
+ expect ( actualOptions . hooks ) . to . eql ( [ 'custom' , 'beforeRemote' ] ) ;
953
+ done ( ) ;
954
+ } ) ;
955
+ } ) ;
956
+
957
+ function setupAppAndRequest ( ) {
958
+ app = loopback ( { localRegistry : true , loadBuiltinModels : true } ) ;
959
+
960
+ app . dataSource ( 'db' , { connector : 'memory' } ) ;
961
+
962
+ TestModel = app . registry . createModel ( 'TestModel' , { base : 'Model' } ) ;
963
+ TestModel . saveOptions = function ( options , cb ) {
964
+ actualOptions = options ;
965
+ cb ( ) ;
966
+ } ;
967
+
968
+ TestModel . remoteMethod ( 'saveOptions' , {
969
+ accepts : { arg : 'options' , type : 'object' , http : 'optionsFromRequest' } ,
970
+ http : { verb : 'GET' , path : '/saveOptions' } ,
971
+ } ) ;
972
+
973
+ app . model ( TestModel , { dataSource : null } ) ;
974
+
975
+ app . enableAuth ( { dataSource : 'db' } ) ;
976
+
977
+ app . use ( loopback . token ( ) ) ;
978
+ app . use ( loopback . rest ( ) ) ;
979
+ }
980
+
981
+ function createUserAndAccessToken ( ) {
982
+ var CREDENTIALS = { email :
'[email protected] ' , password :
'pass' } ;
983
+ var User = app . registry . getModel ( 'User' ) ;
984
+ return User . create ( CREDENTIALS )
985
+ . then ( function ( u ) {
986
+ return User . login ( CREDENTIALS ) ;
987
+ } ) . then ( function ( token ) {
988
+ accessToken = token ;
989
+ userId = token . userId ;
990
+ } ) ;
991
+ }
992
+ } ) ;
889
993
} ) ;
0 commit comments