7
7
const AbstractGrantType = require ( '../../../lib/grant-types/abstract-grant-type' ) ;
8
8
const InvalidArgumentError = require ( '../../../lib/errors/invalid-argument-error' ) ;
9
9
const Request = require ( '../../../lib/request' ) ;
10
+ const InvalidScopeError = require ( '../../../lib/errors/invalid-scope-error' ) ;
10
11
const should = require ( 'chai' ) . should ( ) ;
11
12
12
13
/**
@@ -44,7 +45,7 @@ describe('AbstractGrantType integration', function() {
44
45
} ) ;
45
46
46
47
it ( 'should set the `model`' , function ( ) {
47
- const model = { } ;
48
+ const model = { async generateAccessToken ( ) { } } ;
48
49
const grantType = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model } ) ;
49
50
50
51
grantType . model . should . equal ( model ) ;
@@ -58,70 +59,62 @@ describe('AbstractGrantType integration', function() {
58
59
} ) ;
59
60
60
61
describe ( 'generateAccessToken()' , function ( ) {
61
- it ( 'should return an access token' , function ( ) {
62
+ it ( 'should return an access token' , async function ( ) {
62
63
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : { } , refreshTokenLifetime : 456 } ) ;
63
-
64
- return handler . generateAccessToken ( )
65
- . then ( function ( data ) {
66
- data . should . be . a . sha256 ( ) ;
67
- } )
68
- . catch ( should . fail ) ;
64
+ const accessToken = await handler . generateAccessToken ( ) ;
65
+ accessToken . should . be . a . sha256 ( ) ;
69
66
} ) ;
70
67
71
- it ( 'should support promises' , function ( ) {
68
+ it ( 'should support promises' , async function ( ) {
72
69
const model = {
73
70
generateAccessToken : async function ( ) {
74
- return { } ;
71
+ return 'long-hash-foo-bar' ;
75
72
}
76
73
} ;
77
74
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model , refreshTokenLifetime : 456 } ) ;
78
-
79
- handler . generateAccessToken ( ) . should . be . an . instanceOf ( Promise ) ;
75
+ const accessToken = await handler . generateAccessToken ( ) ;
76
+ accessToken . should . equal ( 'long-hash-foo-bar' ) ;
80
77
} ) ;
81
78
82
- it ( 'should support non-promises' , function ( ) {
79
+ it ( 'should support non-promises' , async function ( ) {
83
80
const model = {
84
81
generateAccessToken : function ( ) {
85
- return { } ;
82
+ return 'long-hash-foo-bar' ;
86
83
}
87
84
} ;
88
85
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model , refreshTokenLifetime : 456 } ) ;
89
-
90
- handler . generateAccessToken ( ) . should . be . an . instanceOf ( Promise ) ;
86
+ const accessToken = await handler . generateAccessToken ( ) ;
87
+ accessToken . should . equal ( 'long-hash-foo-bar' ) ;
91
88
} ) ;
92
89
} ) ;
93
90
94
91
describe ( 'generateRefreshToken()' , function ( ) {
95
- it ( 'should return a refresh token' , function ( ) {
92
+ it ( 'should return a refresh token' , async function ( ) {
96
93
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : { } , refreshTokenLifetime : 456 } ) ;
97
-
98
- return handler . generateRefreshToken ( )
99
- . then ( function ( data ) {
100
- data . should . be . a . sha256 ( ) ;
101
- } )
102
- . catch ( should . fail ) ;
94
+ const refreshToken = await handler . generateRefreshToken ( ) ;
95
+ refreshToken . should . be . a . sha256 ( ) ;
103
96
} ) ;
104
97
105
- it ( 'should support promises' , function ( ) {
98
+ it ( 'should support promises' , async function ( ) {
106
99
const model = {
107
100
generateRefreshToken : async function ( ) {
108
- return { } ;
101
+ return 'long-hash-foo-bar' ;
109
102
}
110
103
} ;
111
104
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model , refreshTokenLifetime : 456 } ) ;
112
-
113
- handler . generateRefreshToken ( ) . should . be . an . instanceOf ( Promise ) ;
105
+ const refreshToken = await handler . generateRefreshToken ( ) ;
106
+ refreshToken . should . equal ( 'long-hash-foo-bar' ) ;
114
107
} ) ;
115
108
116
- it ( 'should support non-promises' , function ( ) {
109
+ it ( 'should support non-promises' , async function ( ) {
117
110
const model = {
118
111
generateRefreshToken : function ( ) {
119
- return { } ;
112
+ return 'long-hash-foo-bar' ;
120
113
}
121
114
} ;
122
115
const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model , refreshTokenLifetime : 456 } ) ;
123
-
124
- handler . generateRefreshToken ( ) . should . be . an . instanceOf ( Promise ) ;
116
+ const refreshToken = await handler . generateRefreshToken ( ) ;
117
+ refreshToken . should . equal ( 'long-hash-foo-bar' ) ;
125
118
} ) ;
126
119
} ) ;
127
120
@@ -170,4 +163,64 @@ describe('AbstractGrantType integration', function() {
170
163
handler . getScope ( request ) . should . equal ( 'foo' ) ;
171
164
} ) ;
172
165
} ) ;
166
+
167
+ describe ( 'validateScope()' , function ( ) {
168
+ it ( 'accepts the scope, if the model does not implement it' , async function ( ) {
169
+ const scope = 'some,scope,this,that' ;
170
+ const user = { id : 123 } ;
171
+ const client = { id : 456 } ;
172
+ const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model : { } , refreshTokenLifetime : 456 } ) ;
173
+ const validated = await handler . validateScope ( user , client , scope ) ;
174
+ validated . should . equal ( scope ) ;
175
+ } ) ;
176
+
177
+ it ( 'accepts the scope, if the model accepts it' , async function ( ) {
178
+ const scope = 'some,scope,this,that' ;
179
+ const user = { id : 123 } ;
180
+ const client = { id : 456 } ;
181
+
182
+ const model = {
183
+ async validateScope ( _user , _client , _scope ) {
184
+ // make sure the model received the correct args
185
+ _user . should . deep . equal ( user ) ;
186
+ _client . should . deep . equal ( _client ) ;
187
+ _scope . should . equal ( scope ) ;
188
+
189
+ return scope ;
190
+ }
191
+ } ;
192
+ const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model, refreshTokenLifetime : 456 } ) ;
193
+ const validated = await handler . validateScope ( user , client , scope ) ;
194
+ validated . should . equal ( scope ) ;
195
+ } ) ;
196
+
197
+ it ( 'throws if the model rejects the scope' , async function ( ) {
198
+ const scope = 'some,scope,this,that' ;
199
+ const user = { id : 123 } ;
200
+ const client = { id : 456 } ;
201
+ const returnTypes = [ undefined , null , false , 0 , '' ] ;
202
+
203
+ for ( const type of returnTypes ) {
204
+ const model = {
205
+ async validateScope ( _user , _client , _scope ) {
206
+ // make sure the model received the correct args
207
+ _user . should . deep . equal ( user ) ;
208
+ _client . should . deep . equal ( _client ) ;
209
+ _scope . should . equal ( scope ) ;
210
+
211
+ return type ;
212
+ }
213
+ } ;
214
+ const handler = new AbstractGrantType ( { accessTokenLifetime : 123 , model, refreshTokenLifetime : 456 } ) ;
215
+
216
+ try {
217
+ await handler . validateScope ( user , client , scope ) ;
218
+ should . fail ( ) ;
219
+ } catch ( e ) {
220
+ e . should . be . an . instanceOf ( InvalidScopeError ) ;
221
+ e . message . should . equal ( 'Invalid scope: Requested scope is invalid' ) ;
222
+ }
223
+ }
224
+ } ) ;
225
+ } ) ;
173
226
} ) ;
0 commit comments