@@ -14,25 +14,107 @@ var extend = require('util')._extend;
14
14
var session = require ( 'express-session' ) ;
15
15
var request = require ( 'supertest' ) ;
16
16
17
- var Token , ACL ;
17
+ var Token , ACL , User , TestModel ;
18
18
19
19
describe ( 'loopback.token(options)' , function ( ) {
20
20
var app ;
21
21
beforeEach ( function ( done ) {
22
22
app = loopback ( { localRegistry : true , loadBuiltinModels : true } ) ;
23
23
app . dataSource ( 'db' , { connector : 'memory' } ) ;
24
24
25
+ ACL = app . registry . getModel ( 'ACL' ) ;
26
+ app . model ( ACL , { dataSource : 'db' } ) ;
27
+
28
+ User = app . registry . getModel ( 'User' ) ;
29
+ app . model ( User , { dataSource : 'db' } ) ;
30
+
25
31
Token = app . registry . createModel ( {
26
32
name : 'MyToken' ,
27
33
base : 'AccessToken' ,
28
34
} ) ;
29
35
app . model ( Token , { dataSource : 'db' } ) ;
30
36
31
- ACL = app . registry . getModel ( 'ACL' ) ;
37
+ TestModel = app . registry . createModel ( {
38
+ name : 'TestModel' ,
39
+ base : 'Model' ,
40
+ } ) ;
41
+ TestModel . getToken = function ( options , cb ) {
42
+ cb ( null , options && options . accessToken || null ) ;
43
+ } ;
44
+ TestModel . remoteMethod ( 'getToken' , {
45
+ accepts : { arg : 'options' , type : 'object' , http : 'optionsFromRequest' } ,
46
+ returns : { arg : 'token' , type : 'object' } ,
47
+ http : { verb : 'GET' , path : '/token' } ,
48
+ } ) ;
49
+ app . model ( TestModel , { dataSource : 'db' } ) ;
32
50
33
51
createTestingToken . call ( this , done ) ;
34
52
} ) ;
35
53
54
+ it ( 'defaults to built-in AccessToken model' , function ( ) {
55
+ var BuiltInToken = app . registry . getModel ( 'AccessToken' ) ;
56
+ app . model ( BuiltInToken , { dataSource : 'db' } ) ;
57
+
58
+ app . enableAuth ( { dataSource : 'db' } ) ;
59
+ app . use ( loopback . token ( ) ) ;
60
+ app . use ( loopback . rest ( ) ) ;
61
+
62
+ return BuiltInToken . create ( { userId : 123 } ) . then ( function ( token ) {
63
+ return request ( app )
64
+ . get ( '/TestModels/token?_format=json' )
65
+ . set ( 'authorization' , token . id )
66
+ . expect ( 200 )
67
+ . expect ( 'Content-Type' , / j s o n / )
68
+ . then ( res => {
69
+ expect ( res . body . token . id ) . to . eql ( token . id ) ;
70
+ } ) ;
71
+ } ) ;
72
+ } ) ;
73
+
74
+ it ( 'uses correct custom AccessToken model from model class param' , function ( ) {
75
+ User . hasMany ( Token , {
76
+ as : 'accessTokens' ,
77
+ options : { disableInclude : true } ,
78
+ } ) ;
79
+
80
+ app . enableAuth ( ) ;
81
+ app . use ( loopback . token ( { model : Token } ) ) ;
82
+ app . use ( loopback . rest ( ) ) ;
83
+
84
+ return Token . create ( { userId : 123 } ) . then ( function ( token ) {
85
+ return request ( app )
86
+ . get ( '/TestModels/token?_format=json' )
87
+ . set ( 'authorization' , token . id )
88
+ . expect ( 200 )
89
+ . expect ( 'Content-Type' , / j s o n / )
90
+ . then ( res => {
91
+ expect ( res . body . token . id ) . to . eql ( token . id ) ;
92
+ } ) ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ it ( 'uses correct custom AccessToken model from string param' , function ( ) {
97
+ User . hasMany ( Token , {
98
+ as : 'accessTokens' ,
99
+ options : { disableInclude : true } ,
100
+ } ) ;
101
+
102
+ app . enableAuth ( ) ;
103
+ app . use ( loopback . token ( { model : Token . modelName } ) ) ;
104
+ app . use ( loopback . rest ( ) ) ;
105
+
106
+ return Token . create ( { userId : 123 } ) . then ( function ( token ) {
107
+ return request ( app )
108
+ . get ( '/TestModels/token?_format=json' )
109
+ . set ( 'authorization' , token . id )
110
+ . expect ( 200 )
111
+ . expect ( 'Content-Type' , / j s o n / )
112
+ . then ( res => {
113
+ expect ( res . body . token . id ) . to . eql ( token . id ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
117
+
36
118
it ( 'should populate req.token from the query string' , function ( done ) {
37
119
createTestAppAndRequest ( this . token , done )
38
120
. get ( '/?access_token=' + this . token . id )
@@ -287,7 +369,7 @@ describe('loopback.token(options)', function() {
287
369
} ) ;
288
370
289
371
it ( 'should overwrite invalid existing token (is !== undefined and has no "id" property) ' +
290
- ' when enableDoubkecheck is true' ,
372
+ ' when enableDoublecheck is true' ,
291
373
function ( done ) {
292
374
var token = this . token ;
293
375
app . use ( function ( req , res , next ) {
@@ -607,9 +689,10 @@ function createTestAppAndRequest(testToken, settings, done) {
607
689
}
608
690
609
691
function createTestApp ( testToken , settings , done ) {
610
- done = arguments [ arguments . length - 1 ] ;
611
- if ( settings == done ) settings = { } ;
612
- settings = settings || { } ;
692
+ if ( ! done && typeof settings === 'function' ) {
693
+ done = settings ;
694
+ settings = { } ;
695
+ }
613
696
614
697
var appSettings = settings . app || { } ;
615
698
var modelSettings = settings . model || { } ;
0 commit comments