Skip to content

Commit 434cb69

Browse files
author
Andrew Maillet
committed
added error handling tests
1 parent 78a17a6 commit 434cb69

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe('AuthorizationCodeGrantType integration', function() {
9191
e.message.should.equal('Missing parameter: `request`');
9292
}
9393
});
94-
95-
it('should throw an error if `client` is missing', function() {
94+
95+
it('should throw an error if `client` is invalid', function() {
9696
var client = {};
9797
var model = {
9898
getAuthorizationCode: function() { return { authorizationCode: 12345, expiresAt: new Date(new Date() * 2), user: {} }; },
@@ -110,6 +110,25 @@ describe('AuthorizationCodeGrantType integration', function() {
110110
});
111111
});
112112

113+
it('should throw an error if `client` is missing', function() {
114+
115+
var model = {
116+
getAuthorizationCode: function() { return { authorizationCode: 12345, expiresAt: new Date(new Date() * 2), user: {} }; },
117+
revokeAuthorizationCode: function() {},
118+
saveToken: function() {}
119+
};
120+
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
121+
var request = new Request({ body: { code: 12345 }, headers: {}, method: {}, query: {} });
122+
123+
try {
124+
grantType.handle(request, null);
125+
}
126+
catch (e) {
127+
e.should.be.an.instanceOf(InvalidArgumentError);
128+
e.message.should.equal('Missing parameter: `client`');
129+
}
130+
});
131+
113132
it('should return a token', function() {
114133
var client = { id: 'foobar' };
115134
var token = {};
@@ -462,6 +481,25 @@ describe('AuthorizationCodeGrantType integration', function() {
462481
.catch(should.fail);
463482
});
464483

484+
it('should throw an error when the auth code is invalid', function() {
485+
var authorizationCode = { authorizationCode: 12345, client: {}, expiresAt: new Date(new Date() / 2), user: {} };
486+
var model = {
487+
getAuthorizationCode: function() {},
488+
revokeAuthorizationCode: function() { return false; },
489+
saveToken: function() {}
490+
};
491+
var grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
492+
493+
return grantType.revokeAuthorizationCode(authorizationCode)
494+
.then(function(data) {
495+
data.should.equal(authorizationCode);
496+
})
497+
.catch(function(e) {
498+
e.should.be.an.instanceOf(InvalidGrantError);
499+
e.message.should.equal('Invalid grant: authorization code is invalid');
500+
});
501+
});
502+
465503
it('should support promises', function() {
466504
var authorizationCode = { authorizationCode: 12345, client: {}, expiresAt: new Date(new Date() / 2), user: {} };
467505
var model = {

test/integration/grant-types/refresh-token-grant-type_test.js

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('RefreshTokenGrantType integration', function() {
170170
});
171171

172172
describe('getRefreshToken()', function() {
173-
it('should throw an error if the requested `refreshToken` is missing', function() {
173+
it('should throw an error if the `refreshToken` parameter is missing from the request body', function() {
174174
var client = {};
175175
var model = {
176176
getRefreshToken: function() {},
@@ -190,35 +190,15 @@ describe('RefreshTokenGrantType integration', function() {
190190
}
191191
});
192192

193-
it('should throw an error if the requested `refreshToken` is invalid', function() {
194-
var client = {};
195-
var model = {
196-
getRefreshToken: function() {},
197-
revokeToken: function() {},
198-
saveToken: function() {}
199-
};
200-
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
201-
var request = new Request({ body: { refresh_token: [] }, headers: {}, method: {}, query: {} });
202-
203-
try {
204-
grantType.getRefreshToken(request, client);
205-
206-
should.fail();
207-
} catch (e) {
208-
e.should.be.an.instanceOf(InvalidRequestError);
209-
e.message.should.equal('Invalid parameter: `refresh_token`');
210-
}
211-
});
212-
213-
it('should throw an error if `refreshToken` is missing', function() {
214-
var client = {};
193+
it('should throw an error if `refreshToken` is not found', function() {
194+
var client = { id: 123 };
215195
var model = {
216-
getRefreshToken: function() { return { accessToken: 'foo', client: { id: 123 }, user: {} }; },
196+
getRefreshToken: function() { return; },
217197
revokeToken: function() {},
218198
saveToken: function() {}
219199
};
220200
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
221-
var request = new Request({ body: { refresh_token: 12345 }, headers: {}, method: {}, query: {} });
201+
var request = new Request({ body: { refresh_token: '12345' }, headers: {}, method: {}, query: {} });
222202

223203
return grantType.getRefreshToken(request, client)
224204
.then(should.fail)
@@ -286,29 +266,7 @@ describe('RefreshTokenGrantType integration', function() {
286266
});
287267
});
288268

289-
it('should throw an error if the request body does not contain `refresh_token`', function() {
290-
var client = {};
291-
var model = {
292-
getRefreshToken: function() {
293-
return { client: { id: 456 }, user: {} };
294-
},
295-
revokeToken: function() {},
296-
saveToken: function() {}
297-
};
298-
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
299-
var request = new Request({ body: {}, headers: {}, method: {}, query: {} });
300-
301-
try {
302-
grantType.getRefreshToken(request, client);
303-
304-
should.fail();
305-
} catch (e) {
306-
e.should.be.an.instanceOf(InvalidRequestError);
307-
e.message.should.equal('Missing parameter: `refresh_token`');
308-
}
309-
});
310-
311-
it('should throw an error if `refresh_token` is invalid', function() {
269+
it('should throw an error if `refresh_token` contains invalid characters', function() {
312270
var client = {};
313271
var model = {
314272
getRefreshToken: function() {
@@ -371,6 +329,26 @@ describe('RefreshTokenGrantType integration', function() {
371329
});
372330
});
373331

332+
it('should throw an error if `refreshTokenExpiresAt` is not a date value', function() {
333+
var client = { id: 123 };
334+
var model = {
335+
getRefreshToken: function() {
336+
return { accessToken: 'foo', client: { id: 123 }, refreshTokenExpiresAt: 'stringvalue', user: {} };
337+
},
338+
revokeToken: function() {},
339+
saveToken: function() {}
340+
};
341+
var grantType = new RefreshTokenGrantType({ accessTokenLifetime: 120, model: model });
342+
var request = new Request({ body: { refresh_token: 12345 }, headers: {}, method: {}, query: {} });
343+
344+
return grantType.getRefreshToken(request, client)
345+
.then(should.fail)
346+
.catch(function(e) {
347+
e.should.be.an.instanceOf(ServerError);
348+
e.message.should.equal('Server error: `refreshTokenExpiresAt` must be a Date instance');
349+
});
350+
});
351+
374352
it('should return a token', function() {
375353
var client = { id: 123 };
376354
var token = { accessToken: 'foo', client: { id: 123 }, user: {} };

0 commit comments

Comments
 (0)