Skip to content

Commit 6e006bf

Browse files
committed
test: add integration test for implicit grant type
1 parent a8375bc commit 6e006bf

File tree

1 file changed

+260
-0
lines changed

1 file changed

+260
-0
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var ImplicitGrantType = require('../../../lib/grant-types/implicit-grant-type');
8+
var InvalidArgumentError = require('../../../lib/errors/invalid-argument-error');
9+
var Promise = require('bluebird');
10+
var Request = require('../../../lib/request');
11+
var should = require('should');
12+
13+
/**
14+
* Test `ImplicitGrantType` integration.
15+
*/
16+
17+
describe.only('ImplicitGrantType integration', function() {
18+
describe('constructor()', function() {
19+
it('should throw an error if `model` is missing', function() {
20+
try {
21+
new ImplicitGrantType();
22+
23+
should.fail();
24+
} catch (e) {
25+
e.should.be.an.instanceOf(InvalidArgumentError);
26+
e.message.should.equal('Missing parameter: `model`');
27+
}
28+
});
29+
30+
it('should throw an error if the model does not implement `saveToken()`', function() {
31+
try {
32+
var model = {};
33+
34+
new ImplicitGrantType({ model: model });
35+
36+
should.fail();
37+
} catch (e) {
38+
e.should.be.an.instanceOf(InvalidArgumentError);
39+
e.message.should.equal('Invalid argument: model does not implement `saveToken()`');
40+
}
41+
});
42+
43+
it('should throw an error if the `user` parameter is missing', function() {
44+
try {
45+
var model = {
46+
saveToken: function() {}
47+
};
48+
49+
new ImplicitGrantType({ model: model });
50+
51+
should.fail();
52+
} catch (e) {
53+
e.should.be.an.instanceOf(InvalidArgumentError);
54+
e.message.should.equal('Missing parameter: `user`');
55+
}
56+
});
57+
});
58+
59+
describe('handle()', function() {
60+
it('should throw an error if `request` is missing', function() {
61+
var model = {
62+
saveToken: function() {}
63+
};
64+
var grantType = new ImplicitGrantType({
65+
accessTokenLifetime: 123,
66+
model: model,
67+
user: {}
68+
});
69+
70+
try {
71+
grantType.handle();
72+
73+
should.fail();
74+
} catch (e) {
75+
e.should.be.an.instanceOf(InvalidArgumentError);
76+
e.message.should.equal('Missing parameter: `request`');
77+
}
78+
});
79+
80+
it('should throw an error if `client` is missing', function() {
81+
82+
var model = {
83+
saveToken: function() {}
84+
};
85+
var grantType = new ImplicitGrantType({
86+
accessTokenLifetime: 123,
87+
model: model,
88+
user: {}
89+
});
90+
var request = new Request({
91+
body: { code: 12345 },
92+
headers: {},
93+
method: {},
94+
query: {}
95+
});
96+
97+
try {
98+
grantType.handle(request, null);
99+
}
100+
catch (e) {
101+
e.should.be.an.instanceOf(InvalidArgumentError);
102+
e.message.should.equal('Missing parameter: `client`');
103+
}
104+
});
105+
106+
it('should return a token', function() {
107+
var client = { id: 'foobar' };
108+
var token = { accessToken: 'foobar-token' };
109+
var model = {
110+
saveToken: function() { return token; },
111+
validateScope: function() { return 'foo'; }
112+
};
113+
var grantType = new ImplicitGrantType({
114+
accessTokenLifetime: 123,
115+
model: model,
116+
user: {}
117+
});
118+
var request = new Request({
119+
body: { code: 12345 },
120+
headers: {},
121+
method: {},
122+
query: {}
123+
});
124+
125+
return grantType.handle(request, client)
126+
.then(function(data) {
127+
data.should.equal(token);
128+
})
129+
.catch(should.fail);
130+
});
131+
132+
it('should support promises', function() {
133+
var client = { id: 'foobar' };
134+
var model = {
135+
saveToken: function() {}
136+
};
137+
var grantType = new ImplicitGrantType({
138+
accessTokenLifetime: 123,
139+
model: model,
140+
user: {}
141+
});
142+
var request = new Request({
143+
body: { code: 12345 },
144+
headers: {},
145+
method: {},
146+
query: {}
147+
});
148+
149+
grantType.handle(request, client).should.be.an.instanceOf(Promise);
150+
});
151+
152+
it('should support non-promises', function() {
153+
var client = { id: 'foobar' };
154+
var model = {
155+
saveToken: function() {}
156+
};
157+
var grantType = new ImplicitGrantType({
158+
accessTokenLifetime: 123,
159+
model: model,
160+
user: {}
161+
});
162+
var request = new Request({
163+
body: { code: 12345 },
164+
headers: {},
165+
method: {},
166+
query: {}
167+
});
168+
169+
grantType.handle(request, client).should.be.an.instanceOf(Promise);
170+
});
171+
172+
it('should support callbacks', function() {
173+
var client = { id: 'foobar' };
174+
var model = {
175+
saveToken: function(tokenToSave, client, user, callback) { callback(null, tokenToSave); }
176+
};
177+
var grantType = new ImplicitGrantType({
178+
accessTokenLifetime: 123,
179+
model: model,
180+
user: {}
181+
});
182+
var request = new Request({
183+
body: { code: 12345 },
184+
headers: {},
185+
method: {},
186+
query: {}
187+
});
188+
189+
grantType.handle(request, client).should.be.an.instanceOf(Promise);
190+
grantType.handle(request, client).then(function(data) {
191+
data.should.have.keys('accessToken', 'accessTokenExpiresAt');
192+
data.accessToken.should.be.type('string');
193+
});
194+
195+
});
196+
});
197+
198+
describe('saveToken()', function() {
199+
it('should save the token', function() {
200+
var token = {};
201+
var model = {
202+
saveToken: function() { return token; },
203+
validateScope: function() { return 'foo'; }
204+
};
205+
var grantType = new ImplicitGrantType({
206+
accessTokenLifetime: 123,
207+
model: model,
208+
user: {}
209+
});
210+
211+
return grantType.saveToken(token)
212+
.then(function(data) {
213+
data.should.equal(token);
214+
})
215+
.catch(should.fail);
216+
});
217+
218+
it('should support promises', function() {
219+
var token = {};
220+
var model = {
221+
saveToken: function() { return Promise.resolve(token); }
222+
};
223+
var grantType = new ImplicitGrantType({
224+
accessTokenLifetime: 123,
225+
model: model,
226+
user: {}
227+
});
228+
229+
grantType.saveToken(token).should.be.an.instanceOf(Promise);
230+
});
231+
232+
it('should support non-promises', function() {
233+
var token = {};
234+
var model = {
235+
saveToken: function() { return token; }
236+
};
237+
var grantType = new ImplicitGrantType({
238+
accessTokenLifetime: 123,
239+
model: model,
240+
user: {}
241+
});
242+
243+
grantType.saveToken(token).should.be.an.instanceOf(Promise);
244+
});
245+
246+
it('should support callbacks', function() {
247+
var token = {};
248+
var model = {
249+
saveToken: function(tokenToSave, client, user, callback) { callback(null, token); }
250+
};
251+
var grantType = new ImplicitGrantType({
252+
accessTokenLifetime: 123,
253+
model: model,
254+
user: {}
255+
});
256+
257+
grantType.saveToken(token).should.be.an.instanceOf(Promise);
258+
});
259+
});
260+
});

0 commit comments

Comments
 (0)