Skip to content

Commit af8dcc1

Browse files
committed
Updated tests to support new model.validateScope method
1 parent 5f4508a commit af8dcc1

10 files changed

+59
-38
lines changed

test/integration/grant-types/authorization-code-grant-type_test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ describe('AuthorizationCodeGrantType integration', function() {
115115
var model = {
116116
getAuthorizationCode: function() { return { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() * 2), user: {} }; },
117117
revokeAuthorizationCode: function() { return { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() / 2), user: {} }; },
118-
saveToken: function() { return token; }
118+
saveToken: function() { return token; },
119+
validateScope: function() { return 'foo'; }
119120
};
120121
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
121122
var request = new Request({ body: { code: 12345 }, headers: {}, method: {}, query: {} });
@@ -464,7 +465,8 @@ describe('AuthorizationCodeGrantType integration', function() {
464465
var model = {
465466
getAuthorizationCode: function() {},
466467
revokeAuthorizationCode: function() {},
467-
saveToken: function() { return token; }
468+
saveToken: function() { return token; },
469+
validateScope: function() { return 'foo'; }
468470
};
469471
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
470472

test/integration/grant-types/client-credentials-grant-type_test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ describe('ClientCredentialsGrantType integration', function() {
9494
var token = {};
9595
var model = {
9696
getUserFromClient: function() { return {}; },
97-
saveToken: function() { return token; }
97+
saveToken: function() { return token; },
98+
validateScope: function() { return 'foo'; }
9899
};
99100
var grantType = new ClientCredentialsGrantType({ accessTokenLifetime: 120, model: model });
100101
var request = new Request({ body: {}, headers: {}, method: {}, query: {} });
@@ -194,7 +195,8 @@ describe('ClientCredentialsGrantType integration', function() {
194195
var token = {};
195196
var model = {
196197
getUserFromClient: function() {},
197-
saveToken: function() { return token; }
198+
saveToken: function() { return token; },
199+
validateScope: function() { return 'foo'; }
198200
};
199201
var grantType = new ClientCredentialsGrantType({ accessTokenLifetime: 123, model: model });
200202

test/integration/grant-types/password-grant-type_test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ describe('PasswordGrantType integration', function() {
9595
var token = {};
9696
var model = {
9797
getUser: function() { return {}; },
98-
saveToken: function() { return token; }
98+
saveToken: function() { return token; },
99+
validateScope: function() { return 'baz'; }
99100
};
100101
var grantType = new PasswordGrantType({ accessTokenLifetime: 123, model: model });
101-
var request = new Request({ body: { username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });
102+
var request = new Request({ body: { username: 'foo', password: 'bar', scope: 'baz' }, headers: {}, method: {}, query: {} });
102103

103104
return grantType.handle(request, client)
104105
.then(function(data) {
@@ -269,7 +270,8 @@ describe('PasswordGrantType integration', function() {
269270
var token = {};
270271
var model = {
271272
getUser: function() {},
272-
saveToken: function() { return token; }
273+
saveToken: function() { return token; },
274+
validateScope: function() { return 'foo'; }
273275
};
274276
var grantType = new PasswordGrantType({ accessTokenLifetime: 123, model: model });
275277

test/integration/handlers/authenticate-handler_test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ describe('AuthenticateHandler integration', function() {
6666
}
6767
});
6868

69-
it('should throw an error if `scope` was given and the model does not implement `validateScope()`', function() {
69+
it('should throw an error if `scope` was given and the model does not implement `verifyScope()`', function() {
7070
try {
7171
new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: true, model: { getAccessToken: function() {} }, scope: 'foobar' });
7272

7373
should.fail();
7474
} catch (e) {
7575
e.should.be.an.instanceOf(InvalidArgumentError);
76-
e.message.should.equal('Invalid argument: model does not implement `validateScope()`');
76+
e.message.should.equal('Invalid argument: model does not implement `verifyScope()`');
7777
}
7878
});
7979

@@ -87,7 +87,7 @@ describe('AuthenticateHandler integration', function() {
8787
it('should set the `scope`', function() {
8888
var model = {
8989
getAccessToken: function() {},
90-
validateScope: function() {}
90+
verifyScope: function() {}
9191
};
9292
var grantType = new AuthenticateHandler({
9393
addAcceptedScopesHeader: true,
@@ -173,7 +173,7 @@ describe('AuthenticateHandler integration', function() {
173173
getAccessToken: function() {
174174
return accessToken;
175175
},
176-
validateScope: function() {
176+
verifyScope: function() {
177177
return true;
178178
}
179179
};
@@ -434,17 +434,17 @@ describe('AuthenticateHandler integration', function() {
434434
});
435435
});
436436

437-
describe('validateScope()', function() {
437+
describe('verifyScope()', function() {
438438
it('should throw an error if `scope` is invalid', function() {
439439
var model = {
440440
getAccessToken: function() {},
441-
validateScope: function() {
441+
verifyScope: function() {
442442
return false;
443443
}
444444
};
445445
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: true, model: model, scope: 'foo' });
446446

447-
return handler.validateScope('foo')
447+
return handler.verifyScope('foo')
448448
.then(should.fail)
449449
.catch(function(e) {
450450
e.should.be.an.instanceOf(InvalidScopeError);
@@ -455,33 +455,33 @@ describe('AuthenticateHandler integration', function() {
455455
it('should support promises', function() {
456456
var model = {
457457
getAccessToken: function() {},
458-
validateScope: function() {
458+
verifyScope: function() {
459459
return true;
460460
}
461461
};
462462
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: true, model: model, scope: 'foo' });
463463

464-
handler.validateScope('foo').should.be.an.instanceOf(Promise);
464+
handler.verifyScope('foo').should.be.an.instanceOf(Promise);
465465
});
466466

467467
it('should support non-promises', function() {
468468
var model = {
469469
getAccessToken: function() {},
470-
validateScope: function() {
470+
verifyScope: function() {
471471
return true;
472472
}
473473
};
474474
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: true, model: model, scope: 'foo' });
475475

476-
handler.validateScope('foo').should.be.an.instanceOf(Promise);
476+
handler.verifyScope('foo').should.be.an.instanceOf(Promise);
477477
});
478478
});
479479

480480
describe('updateResponse()', function() {
481481
it('should not set the `X-Accepted-OAuth-Scopes` header if `scope` is not specified', function() {
482482
var model = {
483483
getAccessToken: function() {},
484-
validateScope: function() {}
484+
verifyScope: function() {}
485485
};
486486
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: false, model: model });
487487
var response = new Response({ body: {}, headers: {} });
@@ -494,7 +494,7 @@ describe('AuthenticateHandler integration', function() {
494494
it('should set the `X-Accepted-OAuth-Scopes` header if `scope` is specified', function() {
495495
var model = {
496496
getAccessToken: function() {},
497-
validateScope: function() {}
497+
verifyScope: function() {}
498498
};
499499
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: false, model: model, scope: 'foo bar' });
500500
var response = new Response({ body: {}, headers: {} });
@@ -507,7 +507,7 @@ describe('AuthenticateHandler integration', function() {
507507
it('should not set the `X-Authorized-OAuth-Scopes` header if `scope` is not specified', function() {
508508
var model = {
509509
getAccessToken: function() {},
510-
validateScope: function() {}
510+
verifyScope: function() {}
511511
};
512512
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: false, addAuthorizedScopesHeader: true, model: model });
513513
var response = new Response({ body: {}, headers: {} });
@@ -520,7 +520,7 @@ describe('AuthenticateHandler integration', function() {
520520
it('should set the `X-Authorized-OAuth-Scopes` header', function() {
521521
var model = {
522522
getAccessToken: function() {},
523-
validateScope: function() {}
523+
verifyScope: function() {}
524524
};
525525
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: false, addAuthorizedScopesHeader: true, model: model, scope: 'foo bar' });
526526
var response = new Response({ body: {}, headers: {} });

test/integration/handlers/token-handler_test.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ describe('TokenHandler integration', function() {
269269
var model = {
270270
getClient: function() { return { grants: ['password'] }; },
271271
getUser: function() { return {}; },
272-
saveToken: function() { return token; }
272+
saveToken: function() { return token; },
273+
validateScope: function() { return 'baz'; }
273274
};
274275
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 });
275276
var request = new Request({
@@ -278,7 +279,8 @@ describe('TokenHandler integration', function() {
278279
client_secret: 'secret',
279280
username: 'foo',
280281
password: 'bar',
281-
grant_type: 'password'
282+
grant_type: 'password',
283+
scope: 'baz'
282284
},
283285
headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' },
284286
method: 'POST',
@@ -616,6 +618,7 @@ describe('TokenHandler integration', function() {
616618
getAuthorizationCode: function() { return { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() * 2), user: {} }; },
617619
getClient: function() {},
618620
saveToken: function() { return token; },
621+
validateScope: function() { return 'foo'; },
619622
revokeAuthorizationCode: function() { return { authorizationCode: 12345, client: { id: 'foobar' }, expiresAt: new Date(new Date() / 2), user: {} }; }
620623
};
621624
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 });
@@ -644,12 +647,14 @@ describe('TokenHandler integration', function() {
644647
var model = {
645648
getClient: function() {},
646649
getUserFromClient: function() { return {}; },
647-
saveToken: function() { return token; }
650+
saveToken: function() { return token; },
651+
validateScope: function() { return 'foo'; }
648652
};
649653
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 });
650654
var request = new Request({
651655
body: {
652-
grant_type: 'client_credentials'
656+
grant_type: 'client_credentials',
657+
scope: 'foo'
653658
},
654659
headers: {},
655660
method: {},
@@ -671,7 +676,8 @@ describe('TokenHandler integration', function() {
671676
var model = {
672677
getClient: function() {},
673678
getUser: function() { return {}; },
674-
saveToken: function() { return token; }
679+
saveToken: function() { return token; },
680+
validateScope: function() { return 'baz'; }
675681
};
676682
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120 });
677683
var request = new Request({
@@ -680,7 +686,8 @@ describe('TokenHandler integration', function() {
680686
client_secret: 'secret',
681687
grant_type: 'password',
682688
password: 'bar',
683-
username: 'foo'
689+
username: 'foo',
690+
scope: 'baz'
684691
},
685692
headers: {},
686693
method: {},
@@ -731,7 +738,8 @@ describe('TokenHandler integration', function() {
731738
var model = {
732739
getClient: function() {},
733740
getUser: function() { return {}; },
734-
saveToken: function() { return token; }
741+
saveToken: function() { return token; },
742+
validateScope: function() { return 'foo'; }
735743
};
736744
var handler = new TokenHandler({ accessTokenLifetime: 120, model: model, refreshTokenLifetime: 120, extendedGrantTypes: { 'urn:ietf:params:oauth:grant-type:saml2-bearer': PasswordGrantType } });
737745
var request = new Request({ body: { grant_type: 'urn:ietf:params:oauth:grant-type:saml2-bearer', username: 'foo', password: 'bar' }, headers: {}, method: {}, query: {} });

test/integration/server_test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ describe('Server integration', function() {
159159
},
160160
saveToken: function() {
161161
return { accessToken: 1234, client: {}, user: {} };
162-
}
162+
},
163+
validateScope: function() { return 'foo'; }
163164
};
164165
var server = new Server({ model: model });
165-
var request = new Request({ body: { client_id: 1234, client_secret: 'secret', grant_type: 'password', username: 'foo', password: 'pass' }, headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' }, method: 'POST', query: {} });
166+
var request = new Request({ body: { client_id: 1234, client_secret: 'secret', grant_type: 'password', username: 'foo', password: 'pass', scope: 'foo' }, headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' }, method: 'POST', query: {} });
166167
var response = new Response({ body: {}, headers: {} });
167168

168169
return server.token(request, response)
@@ -203,10 +204,13 @@ describe('Server integration', function() {
203204
},
204205
saveToken: function() {
205206
return { accessToken: 1234, client: {}, user: {} };
207+
},
208+
validateScope: function() {
209+
return 'foo';
206210
}
207211
};
208212
var server = new Server({ model: model });
209-
var request = new Request({ body: { client_id: 1234, client_secret: 'secret', grant_type: 'password', username: 'foo', password: 'pass' }, headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' }, method: 'POST', query: {} });
213+
var request = new Request({ body: { client_id: 1234, client_secret: 'secret', grant_type: 'password', username: 'foo', password: 'pass', scope: 'foo' }, headers: { 'content-type': 'application/x-www-form-urlencoded', 'transfer-encoding': 'chunked' }, method: 'POST', query: {} });
210214
var response = new Response({ body: {}, headers: {} });
211215

212216
server.token(request, response, null, next);

test/unit/grant-types/authorization-code-grant-type_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('AuthorizationCodeGrantType', function() {
6666
};
6767
var handler = new AuthorizationCodeGrantType({ accessTokenLifetime: 120, model: model });
6868

69+
sinon.stub(handler, 'validateScope').returns('foobiz');
6970
sinon.stub(handler, 'generateAccessToken').returns(Promise.resolve('foo'));
7071
sinon.stub(handler, 'generateRefreshToken').returns(Promise.resolve('bar'));
7172

test/unit/grant-types/client-credentials-grant-type_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('ClientCredentialsGrantType', function() {
4141
};
4242
var handler = new ClientCredentialsGrantType({ accessTokenLifetime: 120, model: model });
4343

44+
sinon.stub(handler, 'validateScope').returns('foobar');
4445
sinon.stub(handler, 'generateAccessToken').returns('foo');
4546
sinon.stub(handler, 'getAccessTokenExpiresAt').returns('biz');
4647

test/unit/grant-types/password-grant-type_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('PasswordGrantType', function() {
4343
};
4444
var handler = new PasswordGrantType({ accessTokenLifetime: 120, model: model });
4545

46+
sinon.stub(handler, 'validateScope').returns('foobar');
4647
sinon.stub(handler, 'generateAccessToken').returns('foo');
4748
sinon.stub(handler, 'generateRefreshToken').returns('bar');
4849
sinon.stub(handler, 'getAccessTokenExpiresAt').returns('biz');

test/unit/handlers/authenticate-handler_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ describe('AuthenticateHandler', function() {
9292
});
9393
});
9494

95-
describe('validateScope()', function() {
95+
describe('verifyScope()', function() {
9696
it('should call `model.getAccessToken()` if scope is defined', function() {
9797
var model = {
9898
getAccessToken: function() {},
99-
validateScope: sinon.stub().returns(true)
99+
verifyScope: sinon.stub().returns(true)
100100
};
101101
var handler = new AuthenticateHandler({ addAcceptedScopesHeader: true, addAuthorizedScopesHeader: true, model: model, scope: 'bar' });
102102

103-
return handler.validateScope('foo')
103+
return handler.verifyScope('foo')
104104
.then(function() {
105-
model.validateScope.callCount.should.equal(1);
106-
model.validateScope.firstCall.args.should.have.length(2);
107-
model.validateScope.firstCall.args[0].should.equal('foo', 'bar');
105+
model.verifyScope.callCount.should.equal(1);
106+
model.verifyScope.firstCall.args.should.have.length(2);
107+
model.verifyScope.firstCall.args[0].should.equal('foo', 'bar');
108108
})
109109
.catch(should.fail);
110110
});

0 commit comments

Comments
 (0)