@@ -99,4 +99,80 @@ describe('AuthorizeHandler', function() {
99
99
. catch ( should . fail ) ;
100
100
} ) ;
101
101
} ) ;
102
+
103
+ describe ( 'validateRedirectUri()' , function ( ) {
104
+ it ( 'should call `model.validateRedirectUri()`' , function ( ) {
105
+ const client = { grants : [ 'authorization_code' ] , redirectUris : [ 'http://example.com/cb' ] } ;
106
+ const redirect_uri = 'http://example.com/cb/2' ;
107
+ const model = {
108
+ getAccessToken : function ( ) { } ,
109
+ getClient : sinon . stub ( ) . returns ( client ) ,
110
+ saveAuthorizationCode : function ( ) { } ,
111
+ validateRedirectUri : sinon . stub ( ) . returns ( true )
112
+ } ;
113
+ const handler = new AuthorizeHandler ( { authorizationCodeLifetime : 120 , model : model } ) ;
114
+ const request = new Request ( { body : { client_id : 12345 , client_secret : 'secret' , redirect_uri } , headers : { } , method : { } , query : { } } ) ;
115
+
116
+ return handler . getClient ( request )
117
+ . then ( function ( ) {
118
+ model . getClient . callCount . should . equal ( 1 ) ;
119
+ model . getClient . firstCall . args . should . have . length ( 2 ) ;
120
+ model . getClient . firstCall . args [ 0 ] . should . equal ( 12345 ) ;
121
+ model . getClient . firstCall . thisValue . should . equal ( model ) ;
122
+
123
+ model . validateRedirectUri . callCount . should . equal ( 1 ) ;
124
+ model . validateRedirectUri . firstCall . args . should . have . length ( 2 ) ;
125
+ model . validateRedirectUri . firstCall . args [ 0 ] . should . equal ( redirect_uri ) ;
126
+ model . validateRedirectUri . firstCall . args [ 1 ] . should . equal ( client ) ;
127
+ model . validateRedirectUri . firstCall . thisValue . should . equal ( model ) ;
128
+ } )
129
+ . catch ( should . fail ) ;
130
+ } ) ;
131
+
132
+ it ( 'should be successful validation' , function ( ) {
133
+ const client = { grants : [ 'authorization_code' ] , redirectUris : [ 'http://example.com/cb' ] } ;
134
+ const redirect_uri = 'http://example.com/cb' ;
135
+ const model = {
136
+ getAccessToken : function ( ) { } ,
137
+ getClient : sinon . stub ( ) . returns ( client ) ,
138
+ saveAuthorizationCode : function ( ) { } ,
139
+ validateRedirectUri : function ( redirectUri , client ) {
140
+ return client . redirectUris . includes ( redirectUri ) ;
141
+ }
142
+ } ;
143
+
144
+ const handler = new AuthorizeHandler ( { authorizationCodeLifetime : 120 , model : model } ) ;
145
+ const request = new Request ( { body : { client_id : 12345 , client_secret : 'secret' , redirect_uri } , headers : { } , method : { } , query : { } } ) ;
146
+
147
+ return handler . getClient ( request )
148
+ . then ( ( client ) => {
149
+ client . should . equal ( client ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ it ( 'should be unsuccessful validation' , function ( ) {
154
+ const client = { grants : [ 'authorization_code' ] , redirectUris : [ 'http://example.com/cb' ] } ;
155
+ const redirect_uri = 'http://example.com/callback' ;
156
+ const model = {
157
+ getAccessToken : function ( ) { } ,
158
+ getClient : sinon . stub ( ) . returns ( client ) ,
159
+ saveAuthorizationCode : function ( ) { } ,
160
+ validateRedirectUri : function ( redirectUri , client ) {
161
+ return client . redirectUris . includes ( redirectUri ) ;
162
+ }
163
+ } ;
164
+
165
+ const handler = new AuthorizeHandler ( { authorizationCodeLifetime : 120 , model : model } ) ;
166
+ const request = new Request ( { body : { client_id : 12345 , client_secret : 'secret' , redirect_uri } , headers : { } , method : { } , query : { } } ) ;
167
+
168
+ return handler . getClient ( request )
169
+ . then ( ( ) => {
170
+ throw Error ( 'should not resolve' ) ;
171
+ } )
172
+ . catch ( ( err ) => {
173
+ err . name . should . equal ( 'invalid_client' ) ;
174
+ err . message . should . equal ( 'Invalid client: `redirect_uri` does not match client value' ) ;
175
+ } ) ;
176
+ } ) ;
177
+ } ) ;
102
178
} ) ;
0 commit comments