Skip to content

Commit 32d13c0

Browse files
committed
tests: coverage 100%
1 parent 1b88ef0 commit 32d13c0

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

test/integration/index_test.js

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const ExpressOAuthServer = require('../../');
66
const InvalidArgumentError = require('@node-oauth/oauth2-server/lib/errors/invalid-argument-error');
7+
const UnauthorizedRequestError = require('@node-oauth/oauth2-server/lib/errors/unauthorized-request-error');
78
const NodeOAuthServer = require('@node-oauth/oauth2-server');
89
const bodyparser = require('body-parser');
910
const express = require('express');
@@ -28,7 +29,7 @@ describe('ExpressOAuthServer', function() {
2829
describe('constructor()', function() {
2930
it('should throw an error if `model` is missing', function() {
3031
try {
31-
new ExpressOAuthServer({});
32+
new ExpressOAuthServer();
3233

3334
should.fail();
3435
} catch (e) {
@@ -63,7 +64,7 @@ describe('ExpressOAuthServer', function() {
6364
return token;
6465
}
6566
};
66-
const oauth = new ExpressOAuthServer({ model: model });
67+
const oauth = new ExpressOAuthServer({ model });
6768

6869
app.use(oauth.authenticate());
6970

@@ -80,6 +81,25 @@ describe('ExpressOAuthServer', function() {
8081
.end(done);
8182
});
8283

84+
it('should return opaque error if the request lacks proper authentication', function(done) {
85+
const model = {
86+
getAccessToken: function() {
87+
throw new UnauthorizedRequestError();
88+
}
89+
};
90+
const oauth = new ExpressOAuthServer({ model });
91+
app.use(oauth.authenticate());
92+
93+
request(app.listen())
94+
.get('/')
95+
.set('Authorization', 'Bearer foobar')
96+
.expect(401, function (err, res) {
97+
(err === null).should.eql(true);
98+
(res.body.error === undefined).should.eql(true);
99+
done();
100+
});
101+
});
102+
83103
it('should cache the authorization token', function(done) {
84104
const tokenExpires = new Date();
85105
tokenExpires.setDate(tokenExpires.getDate() + 1);
@@ -89,7 +109,7 @@ describe('ExpressOAuthServer', function() {
89109
return token;
90110
}
91111
};
92-
const oauth = new ExpressOAuthServer({ model: model });
112+
const oauth = new ExpressOAuthServer({ model });
93113

94114
app.use(oauth.authenticate());
95115

@@ -127,7 +147,7 @@ describe('ExpressOAuthServer', function() {
127147
return code;
128148
}
129149
};
130-
const oauth = new ExpressOAuthServer({ model: model, continueMiddleware: true });
150+
const oauth = new ExpressOAuthServer({ model, continueMiddleware: true });
131151

132152
app.use(oauth.authorize());
133153

@@ -159,7 +179,7 @@ describe('ExpressOAuthServer', function() {
159179
return {};
160180
}
161181
};
162-
const oauth = new ExpressOAuthServer({ model: model });
182+
const oauth = new ExpressOAuthServer({ model });
163183

164184
app.use(oauth.authorize());
165185

@@ -186,7 +206,7 @@ describe('ExpressOAuthServer', function() {
186206
return { authorizationCode: 123 };
187207
}
188208
};
189-
const oauth = new ExpressOAuthServer({ model: model });
209+
const oauth = new ExpressOAuthServer({ model });
190210

191211
app.use(oauth.authorize());
192212

@@ -198,6 +218,39 @@ describe('ExpressOAuthServer', function() {
198218
.end(done);
199219
});
200220

221+
it('should use error handler', function(done) {
222+
const model = {
223+
getAccessToken: function() {
224+
return { user: {}, accessTokenExpiresAt: new Date() };
225+
},
226+
getClient: function() {
227+
return { grants: ['authorization_code'], redirectUris: ['http://example.com'] };
228+
},
229+
saveAuthorizationCode: function() {
230+
return {};
231+
}
232+
};
233+
const oauth = new ExpressOAuthServer({ model, useErrorHandler: true });
234+
235+
app.use(oauth.authorize());
236+
app.use(function (err, req, res, next) {
237+
err.status.should.eql(400);
238+
err.name.should.eql('invalid_request');
239+
err.message.should.eql('Missing parameter: `response_type`');
240+
(typeof next === 'function').should.eql(true);
241+
done();
242+
});
243+
244+
request(app.listen())
245+
.post('/?state=foobiz')
246+
.set('Authorization', 'Bearer foobar')
247+
.send({ client_id: 12345 })
248+
.expect(500, function(err, res) {
249+
(err === null).should.eql(true);
250+
(res.body.error === undefined).should.eql(true);
251+
});
252+
});
253+
201254
it('should return an error if `model` is empty', function(done) {
202255
const oauth = new ExpressOAuthServer({ model: {} });
203256

@@ -224,7 +277,7 @@ describe('ExpressOAuthServer', function() {
224277
return token;
225278
}
226279
};
227-
const oauth = new ExpressOAuthServer({ model: model, continueMiddleware: true });
280+
const oauth = new ExpressOAuthServer({ model, continueMiddleware: true });
228281

229282
app.use(oauth.token());
230283
const spy = sinon.spy(function(req, res, next) {
@@ -257,7 +310,7 @@ describe('ExpressOAuthServer', function() {
257310
}
258311
};
259312
sinon.spy();
260-
const oauth = new ExpressOAuthServer({ model: model, continueMiddleware: true });
313+
const oauth = new ExpressOAuthServer({ model, continueMiddleware: true });
261314

262315
app.use(oauth.token());
263316
request(app.listen())
@@ -279,7 +332,7 @@ describe('ExpressOAuthServer', function() {
279332
return { accessToken: 'foobar', client: {}, refreshToken: 'foobiz', user: {} };
280333
}
281334
};
282-
const oauth = new ExpressOAuthServer({ model: model });
335+
const oauth = new ExpressOAuthServer({ model });
283336

284337
app.use(oauth.token());
285338

0 commit comments

Comments
 (0)