@@ -130,6 +130,107 @@ describe('loopback.rest', function() {
130130 } ) ;
131131 } ) ;
132132
133+ describe ( 'context propagation' , function ( ) {
134+ var User ;
135+
136+ beforeEach ( function ( ) {
137+ User = givenUserModelWithAuth ( ) ;
138+ User . getToken = function ( cb ) {
139+ var context = loopback . getCurrentContext ( ) ;
140+ var req = context . get ( 'http' ) . req ;
141+ expect ( req ) . to . have . property ( 'accessToken' ) ;
142+
143+ var juggler = require ( 'loopback-datasource-juggler' ) ;
144+ expect ( juggler . getCurrentContext ( ) . get ( 'http' ) . req )
145+ . to . have . property ( 'accessToken' ) ;
146+
147+ var remoting = require ( 'strong-remoting' ) ;
148+ expect ( remoting . getCurrentContext ( ) . get ( 'http' ) . req )
149+ . to . have . property ( 'accessToken' ) ;
150+
151+ cb ( null , req && req . accessToken ? req . accessToken . id : null ) ;
152+ } ;
153+ // Set up the ACL
154+ User . settings . acls . push ( { principalType : 'ROLE' ,
155+ principalId : '$authenticated' , permission : 'ALLOW' ,
156+ property : 'getToken' } ) ;
157+
158+ loopback . remoteMethod ( User . getToken , {
159+ accepts : [ ] ,
160+ returns : [
161+ { type : 'object' , name : 'id' }
162+ ]
163+ } ) ;
164+ } ) ;
165+
166+ function invokeGetToken ( done ) {
167+ givenLoggedInUser ( function ( err , token ) {
168+ if ( err ) return done ( err ) ;
169+ request ( app ) . get ( '/users/getToken' )
170+ . set ( 'Authorization' , token . id )
171+ . expect ( 200 )
172+ . end ( function ( err , res ) {
173+ if ( err ) return done ( err ) ;
174+ expect ( res . body . id ) . to . equal ( token . id ) ;
175+ done ( ) ;
176+ } ) ;
177+ } ) ;
178+ }
179+
180+ it ( 'should enable context using loopback.context' , function ( done ) {
181+ app . use ( loopback . context ( { enableHttpContext : true } ) ) ;
182+ app . enableAuth ( ) ;
183+ app . use ( loopback . rest ( ) ) ;
184+
185+ invokeGetToken ( done ) ;
186+ } ) ;
187+
188+ it ( 'should enable context with loopback.rest' , function ( done ) {
189+ app . enableAuth ( ) ;
190+ app . set ( 'remoting' , { context : { enableHttpContext : true } } ) ;
191+ app . use ( loopback . rest ( ) ) ;
192+
193+ invokeGetToken ( done ) ;
194+ } ) ;
195+
196+ it ( 'should support explicit context' , function ( done ) {
197+ app . enableAuth ( ) ;
198+ app . use ( loopback . context ( ) ) ;
199+ app . use ( loopback . token (
200+ { model : loopback . getModelByType ( loopback . AccessToken ) } ) ) ;
201+ app . use ( function ( req , res , next ) {
202+ loopback . getCurrentContext ( ) . set ( 'accessToken' , req . accessToken ) ;
203+ next ( ) ;
204+ } ) ;
205+ app . use ( loopback . rest ( ) ) ;
206+
207+ User . getToken = function ( cb ) {
208+ var context = loopback . getCurrentContext ( ) ;
209+ var accessToken = context . get ( 'accessToken' ) ;
210+ expect ( context . get ( 'accessToken' ) ) . to . have . property ( 'id' ) ;
211+
212+ var juggler = require ( 'loopback-datasource-juggler' ) ;
213+ context = juggler . getCurrentContext ( ) ;
214+ expect ( context . get ( 'accessToken' ) ) . to . have . property ( 'id' ) ;
215+
216+ var remoting = require ( 'strong-remoting' ) ;
217+ context = remoting . getCurrentContext ( ) ;
218+ expect ( context . get ( 'accessToken' ) ) . to . have . property ( 'id' ) ;
219+
220+ cb ( null , accessToken ? accessToken . id : null ) ;
221+ } ;
222+
223+ loopback . remoteMethod ( User . getToken , {
224+ accepts : [ ] ,
225+ returns : [
226+ { type : 'object' , name : 'id' }
227+ ]
228+ } ) ;
229+
230+ invokeGetToken ( done ) ;
231+ } ) ;
232+ } ) ;
233+
133234 function givenUserModelWithAuth ( ) {
134235 // NOTE(bajtos) It is important to create a custom AccessToken model here,
135236 // in order to overwrite the entry created by previous tests in
0 commit comments