77const AbstractGrantType = require ( '../../../lib/grant-types/abstract-grant-type' ) ;
88const InvalidArgumentError = require ( '../../../lib/errors/invalid-argument-error' ) ;
99const Request = require ( '../../../lib/request' ) ;
10+ const InvalidScopeError = require ( '../../../lib/errors/invalid-scope-error' ) ;
1011const should = require ( 'chai' ) . should ( ) ;
1112
1213/**
@@ -44,7 +45,7 @@ describe('AbstractGrantType integration', function() {
4445 } ) ;
4546
4647 it ( 'should set the `model`' , function ( ) {
47- const model = { } ;
48+ const model = { async generateAccessToken ( ) { } } ;
4849 const grantType = new AbstractGrantType ( { accessTokenLifetime : 123 , model : model } ) ;
4950
5051 grantType . model . should . equal ( model ) ;
@@ -58,70 +59,62 @@ describe('AbstractGrantType integration', function() {
5859 } ) ;
5960
6061 describe ( 'generateAccessToken()' , function ( ) {
61- it ( 'should return an access token' , function ( ) {
62+ it ( 'should return an access token' , async function ( ) {
6263 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 ( ) ;
6966 } ) ;
7067
71- it ( 'should support promises' , function ( ) {
68+ it ( 'should support promises' , async function ( ) {
7269 const model = {
7370 generateAccessToken : async function ( ) {
74- return { } ;
71+ return 'long-hash-foo-bar' ;
7572 }
7673 } ;
7774 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' ) ;
8077 } ) ;
8178
82- it ( 'should support non-promises' , function ( ) {
79+ it ( 'should support non-promises' , async function ( ) {
8380 const model = {
8481 generateAccessToken : function ( ) {
85- return { } ;
82+ return 'long-hash-foo-bar' ;
8683 }
8784 } ;
8885 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' ) ;
9188 } ) ;
9289 } ) ;
9390
9491 describe ( 'generateRefreshToken()' , function ( ) {
95- it ( 'should return a refresh token' , function ( ) {
92+ it ( 'should return a refresh token' , async function ( ) {
9693 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 ( ) ;
10396 } ) ;
10497
105- it ( 'should support promises' , function ( ) {
98+ it ( 'should support promises' , async function ( ) {
10699 const model = {
107100 generateRefreshToken : async function ( ) {
108- return { } ;
101+ return 'long-hash-foo-bar' ;
109102 }
110103 } ;
111104 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' ) ;
114107 } ) ;
115108
116- it ( 'should support non-promises' , function ( ) {
109+ it ( 'should support non-promises' , async function ( ) {
117110 const model = {
118111 generateRefreshToken : function ( ) {
119- return { } ;
112+ return 'long-hash-foo-bar' ;
120113 }
121114 } ;
122115 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' ) ;
125118 } ) ;
126119 } ) ;
127120
@@ -170,4 +163,64 @@ describe('AbstractGrantType integration', function() {
170163 handler . getScope ( request ) . should . equal ( 'foo' ) ;
171164 } ) ;
172165 } ) ;
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+ } ) ;
173226} ) ;
0 commit comments