Skip to content

Commit a7a6a0a

Browse files
committed
Port handler tests in pull oauthjs#451 from dev branch.
1 parent ad8b498 commit a7a6a0a

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

test/integration/handlers/authorize-handler.spec.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,95 @@ describe('AuthorizeHandler integration', () => {
424424
});
425425
});
426426

427+
it('should redirect to a successful response if `model.validateScope` is not defined', function() {
428+
var client = { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
429+
var model = {
430+
getAccessToken: function() {
431+
return {
432+
client: client,
433+
user: {},
434+
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
435+
};
436+
},
437+
getClient: function() {
438+
return client;
439+
},
440+
saveAuthorizationCode: function() {
441+
return { authorizationCode: 12345, client: client };
442+
}
443+
};
444+
var handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
445+
var request = new Request({
446+
body: {
447+
client_id: 12345,
448+
response_type: 'code'
449+
},
450+
headers: {
451+
'Authorization': 'Bearer foo'
452+
},
453+
method: 'POST',
454+
query: {
455+
scope: 'read',
456+
state: 'foobar'
457+
}
458+
});
459+
var response = new Response({ body: {}, headers: {} });
460+
461+
return handler.handle(request, response)
462+
.then(function(data) {
463+
data.should.eql({
464+
authorizationCode: 12345,
465+
client: client
466+
});
467+
});
468+
});
469+
470+
it('should redirect to an error response if `scope` is insufficient', function() {
471+
var client = { grants: ['authorization_code'], redirectUris: ['http://example.com/cb'] };
472+
var model = {
473+
getAccessToken: function() {
474+
return {
475+
client: client,
476+
user: {},
477+
accessTokenExpiresAt: new Date(new Date().getTime() + 10000)
478+
};
479+
},
480+
getClient: function() {
481+
return client;
482+
},
483+
saveAuthorizationCode: function() {
484+
return { authorizationCode: 12345, client: client };
485+
},
486+
validateScope: function() {
487+
return false;
488+
}
489+
};
490+
var handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
491+
var request = new Request({
492+
body: {
493+
client_id: 12345,
494+
response_type: 'code'
495+
},
496+
headers: {
497+
'Authorization': 'Bearer foo'
498+
},
499+
method: 'POST',
500+
query: {
501+
scope: 'read',
502+
state: 'foobar'
503+
}
504+
});
505+
var response = new Response({ body: {}, headers: {} });
506+
507+
return handler.handle(request, response)
508+
.then(() => {
509+
should.fail('should.fail', '');
510+
})
511+
.catch(function() {
512+
response.get('location').should.equal('http://example.com/cb?error=invalid_scope&error_description=Invalid%20scope%3A%20Requested%20scope%20is%20invalid');
513+
});
514+
});
515+
427516
it('should redirect to an error response if `state` is missing', () => {
428517
const model = {
429518
getAccessToken: () => {

0 commit comments

Comments
 (0)