Skip to content

Commit 802546a

Browse files
committed
fix(tests): add missing tests for missing and invalid codes in AuthorizationCodeGrantType
1 parent e51fc27 commit 802546a

1 file changed

Lines changed: 100 additions & 53 deletions

File tree

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

Lines changed: 100 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,63 @@ describe('AuthorizationCodeGrantType integration', function() {
112112
}
113113
});
114114

115-
it('should throw an error if `client` is missing', function() {
115+
it('should throw an error if `client` is missing', async function () {
116116
const model = Model.from({
117117
getAuthorizationCode: () => should.fail(),
118118
revokeAuthorizationCode: () => should.fail(),
119119
saveToken: () => should.fail()
120120
});
121121
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
122122
const request = new Request({ body: { code: '12345' }, headers: {}, method: {}, query: {} });
123+
const values = [null, undefined, '', 0, false];
124+
for (const client of values) {
125+
try {
126+
await grantType.handle(request, client);
127+
should.fail();
128+
} catch (e) {
129+
e.should.be.an.instanceOf(InvalidArgumentError);
130+
e.message.should.equal('Missing parameter: `client`');
131+
}
132+
}
133+
});
123134

124-
try {
125-
grantType.handle(request, null);
135+
it('should throw an error if `code` is missing', async function() {
136+
const model = Model.from({
137+
getAuthorizationCode: () => should.fail(),
138+
revokeAuthorizationCode: () => should.fail(),
139+
saveToken: () => should.fail()
140+
});
141+
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
142+
const values = [null, undefined, '', 0, false];
143+
for (const code of values) {
144+
const request = new Request({ body: { code }, headers: {}, method: {}, query: {} });
145+
try {
146+
await grantType.handle(request, { id: 'foobar' });
147+
should.fail();
148+
} catch (e) {
149+
e.should.be.an.instanceOf(InvalidRequestError);
150+
e.message.should.equal('Missing parameter: `code`');
151+
}
126152
}
127-
catch (e) {
128-
e.should.be.an.instanceOf(InvalidArgumentError);
129-
e.message.should.equal('Missing parameter: `client`');
153+
});
154+
155+
it('should throw an error if `code` is of invalid format', async function() {
156+
const model = Model.from({
157+
getAuthorizationCode: () => should.fail(),
158+
revokeAuthorizationCode: () => should.fail(),
159+
saveToken: () => should.fail()
160+
});
161+
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
162+
const values = ['øå€£‰', {}, () => {}, [], Symbol('foo')];
163+
for (const code of values) {
164+
const request = new Request({ body: { code }, headers: {}, method: {}, query: {} });
165+
try {
166+
await grantType.handle(request, { id: 'foobar' });
167+
should.fail();
168+
} catch (e) {
169+
e.should.be.an.instanceOf(InvalidRequestError);
170+
e.message.should.equal('Invalid parameter: `code`');
171+
}
130172
}
131173
});
132174

@@ -141,55 +183,60 @@ describe('AuthorizationCodeGrantType integration', function() {
141183
user,
142184
scope
143185
};
144-
const model = Model.from({
145-
getAuthorizationCode: async function (code) {
146-
code.should.equal('code-1234');
147186

148-
return codeDoc;
149-
},
150-
revokeAuthorizationCode: async function (_codeDoc) {
151-
_codeDoc.should.deep.equal(codeDoc);
152-
return true;
153-
},
154-
validateScope: async function (_user, _client, _scope) {
155-
_user.should.deep.equal(user);
156-
_client.should.deep.equal(client);
157-
_scope.should.eql(scope);
158-
return scope;
159-
},
160-
generateAccessToken: async function (_client, _user, _scope) {
161-
_user.should.deep.equal(user);
162-
_client.should.deep.equal(client);
163-
_scope.should.eql(scope);
164-
return 'long-access-token-hash';
165-
},
166-
generateRefreshToken: async function (_client, _user, _scope) {
167-
_user.should.deep.equal(user);
168-
_client.should.deep.equal(client);
169-
_scope.should.eql(scope);
170-
return 'long-refresh-token-hash';
171-
},
172-
saveToken: async function (_token, _client, _user) {
173-
_user.should.deep.equal(user);
174-
_client.should.deep.equal(client);
175-
_token.accessToken.should.equal('long-access-token-hash');
176-
_token.refreshToken.should.equal('long-refresh-token-hash');
177-
_token.authorizationCode.should.equal(codeDoc.authorizationCode);
178-
_token.accessTokenExpiresAt.should.be.instanceOf(Date);
179-
_token.refreshTokenExpiresAt.should.be.instanceOf(Date);
180-
return _token;
181-
},
182-
});
187+
const values = ['1234', 1234];
188+
for (const code of values) {
183189

184-
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
185-
const request = new Request({ body: { code: 'code-1234' }, headers: {}, method: {}, query: {} });
186-
187-
const token = await grantType.handle(request, client);
188-
token.accessToken.should.equal('long-access-token-hash');
189-
token.refreshToken.should.equal('long-refresh-token-hash');
190-
token.authorizationCode.should.equal(codeDoc.authorizationCode);
191-
token.accessTokenExpiresAt.should.be.instanceOf(Date);
192-
token.refreshTokenExpiresAt.should.be.instanceOf(Date);
190+
const model = Model.from({
191+
getAuthorizationCode: async function (_code) {
192+
_code.should.equal(code);
193+
194+
return codeDoc;
195+
},
196+
revokeAuthorizationCode: async function (_codeDoc) {
197+
_codeDoc.should.deep.equal(codeDoc);
198+
return true;
199+
},
200+
validateScope: async function (_user, _client, _scope) {
201+
_user.should.deep.equal(user);
202+
_client.should.deep.equal(client);
203+
_scope.should.eql(scope);
204+
return scope;
205+
},
206+
generateAccessToken: async function (_client, _user, _scope) {
207+
_user.should.deep.equal(user);
208+
_client.should.deep.equal(client);
209+
_scope.should.eql(scope);
210+
return 'long-access-token-hash';
211+
},
212+
generateRefreshToken: async function (_client, _user, _scope) {
213+
_user.should.deep.equal(user);
214+
_client.should.deep.equal(client);
215+
_scope.should.eql(scope);
216+
return 'long-refresh-token-hash';
217+
},
218+
saveToken: async function (_token, _client, _user) {
219+
_user.should.deep.equal(user);
220+
_client.should.deep.equal(client);
221+
_token.accessToken.should.equal('long-access-token-hash');
222+
_token.refreshToken.should.equal('long-refresh-token-hash');
223+
_token.authorizationCode.should.equal(codeDoc.authorizationCode);
224+
_token.accessTokenExpiresAt.should.be.instanceOf(Date);
225+
_token.refreshTokenExpiresAt.should.be.instanceOf(Date);
226+
return _token;
227+
},
228+
});
229+
230+
const grantType = new AuthorizationCodeGrantType({ accessTokenLifetime: 123, model: model });
231+
const request = new Request({ body: { code }, headers: {}, method: {}, query: {} });
232+
233+
const token = await grantType.handle(request, client);
234+
token.accessToken.should.equal('long-access-token-hash');
235+
token.refreshToken.should.equal('long-refresh-token-hash');
236+
token.authorizationCode.should.equal(codeDoc.authorizationCode);
237+
token.accessTokenExpiresAt.should.be.instanceOf(Date);
238+
token.refreshTokenExpiresAt.should.be.instanceOf(Date);
239+
}
193240
});
194241

195242
it('should support promises', function() {

0 commit comments

Comments
 (0)