Skip to content

Commit f074b01

Browse files
committed
Test case for AuthorizationError.
1 parent 689f45a commit f074b01

5 files changed

+107
-3
lines changed

lib/errors/authorizationerror.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
/**
22
* `AuthorizationError` error.
33
*
4+
* AuthorizationError represents an error in response to an authorization
5+
* request. For details, refer to RFC 6749, section 4.1.2.1.
6+
*
7+
* References:
8+
* - [The OAuth 2.0 Authorization Framework](http://tools.ietf.org/html/rfc6749)
9+
*
10+
* @constructor
11+
* @param {String} [message]
12+
* @param {String} [code]
13+
* @param {String} [uri]
14+
* @param {Number} [status]
415
* @api public
516
*/
617
function AuthorizationError(message, code, uri, status) {
@@ -15,7 +26,7 @@ function AuthorizationError(message, code, uri, status) {
1526
Error.call(this);
1627
Error.captureStackTrace(this, arguments.callee);
1728
this.name = 'AuthorizationError';
18-
this.message = message || 'OAuth 2.0 authorization request failed';
29+
this.message = message;
1930
this.code = code || 'server_error';
2031
this.uri = uri;
2132
this.status = status || 500;

lib/errors/internaloautherror.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ InternalOAuthError.prototype.__proto__ = Error.prototype;
2727
* @api public
2828
*/
2929
InternalOAuthError.prototype.toString = function() {
30-
var m = this.name + ': ' + this.message;
30+
var m = this.name;
31+
if (this.message) { m += ': ' + this.message; }
3132
if (this.oauthError) {
3233
if (this.oauthError instanceof Error) {
3334
m = this.oauthError.toString();
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var AuthorizationError = require('../../lib/errors/authorizationerror');
2+
3+
4+
describe('AuthorizationError', function() {
5+
6+
describe('constructed without a message', function() {
7+
var err = new AuthorizationError();
8+
9+
it('should have default properties', function() {
10+
expect(err.message).to.be.undefined;
11+
expect(err.code).to.equal('server_error');
12+
expect(err.uri).to.be.undefined;
13+
expect(err.status).to.equal(500);
14+
});
15+
16+
it('should format correctly', function() {
17+
expect(err.toString()).to.equal('AuthorizationError');
18+
});
19+
});
20+
21+
describe('constructed with a message', function() {
22+
var err = new AuthorizationError('Invalid return URI');
23+
24+
it('should have default properties', function() {
25+
expect(err.message).to.equal('Invalid return URI');
26+
expect(err.code).to.equal('server_error');
27+
expect(err.uri).to.be.undefined;
28+
expect(err.status).to.equal(500);
29+
});
30+
31+
it('should format correctly', function() {
32+
expect(err.toString()).to.equal('AuthorizationError: Invalid return URI');
33+
});
34+
});
35+
36+
describe('constructed with a message and access_denied code', function() {
37+
var err = new AuthorizationError('Access denied', 'access_denied');
38+
39+
it('should have default properties', function() {
40+
expect(err.message).to.equal('Access denied');
41+
expect(err.code).to.equal('access_denied');
42+
expect(err.uri).to.be.undefined;
43+
expect(err.status).to.equal(403);
44+
});
45+
});
46+
47+
describe('constructed with a message and server_error code', function() {
48+
var err = new AuthorizationError('Server error', 'server_error');
49+
50+
it('should have default properties', function() {
51+
expect(err.message).to.equal('Server error');
52+
expect(err.code).to.equal('server_error');
53+
expect(err.uri).to.be.undefined;
54+
expect(err.status).to.equal(502);
55+
});
56+
});
57+
58+
describe('constructed with a message and temporarily_unavailable code', function() {
59+
var err = new AuthorizationError('Temporarily unavailable', 'temporarily_unavailable');
60+
61+
it('should have default properties', function() {
62+
expect(err.message).to.equal('Temporarily unavailable');
63+
expect(err.code).to.equal('temporarily_unavailable');
64+
expect(err.uri).to.be.undefined;
65+
expect(err.status).to.equal(503);
66+
});
67+
});
68+
69+
describe('constructed with a message, code, uri and status', function() {
70+
var err = new AuthorizationError('Unsupported response type: foo', 'unsupported_response_type', 'http://www.example.com/oauth/help', 501);
71+
72+
it('should have default properties', function() {
73+
expect(err.message).to.equal('Unsupported response type: foo');
74+
expect(err.code).to.equal('unsupported_response_type');
75+
expect(err.uri).to.equal('http://www.example.com/oauth/help');
76+
expect(err.status).to.equal(501);
77+
});
78+
79+
it('should format correctly', function() {
80+
expect(err.toString()).to.equal('AuthorizationError: Unsupported response type: foo');
81+
});
82+
});
83+
84+
});

test/errors/internaloautherror.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ var InternalOAuthError = require('../../lib/errors/internaloautherror');
33

44
describe('InternalOAuthError', function() {
55

6+
describe('constructed without a message', function() {
7+
var err = new InternalOAuthError();
8+
9+
it('should format correctly', function() {
10+
expect(err.toString()).to.equal('InternalOAuthError');
11+
});
12+
});
13+
614
describe('constructed with a message', function() {
715
var err = new InternalOAuthError('oops');
816

test/strategies/oauth2.normal.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ describe('OAuth2Strategy', function() {
291291

292292
it('should error', function() {
293293
expect(err).to.be.an.instanceof(AuthorizationError)
294-
expect(err.message).to.equal('OAuth 2.0 authorization request failed');
294+
expect(err.message).to.be.undefined;
295295
expect(err.code).to.equal('invalid_scope');
296296
expect(err.uri).to.be.undefined;
297297
expect(err.status).to.equal(500);

0 commit comments

Comments
 (0)