forked from koajs/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
186 lines (145 loc) · 4.68 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var koa = require('koa');
var request = require('supertest');
var jwt = require('jsonwebtoken');
var assert = require('assert');
var koajwt = require('.');
describe('failure tests', function () {
it('should throw if options not sent', function() {
try {
koajwt();
}
catch(e) {
assert.ok(e);
assert.equal(e.message, '"secret" option is required');
}
});
it('should throw 401 if no authorization header', function(done) {
var app = koa();
app.use(koajwt({ secret: 'shhhh' }));
request(app.listen())
.get('/')
.expect(401)
.end(done);
});
it('should return 401 if authorization header is malformed', function(done) {
var app = koa();
app.use(koajwt({ secret: 'shhhh' }));
request(app.listen())
.get('/')
.set('Authorization', 'wrong')
.expect(401)
.expect('Bad Authorization header format. Format is "Authorization: Bearer <token>"\n')
.end(done);
});
it('should throw if authorization header is not well-formatted jwt', function(done) {
var app = koa();
app.use(koajwt({ secret: 'shhhh' }));
request(app.listen())
.get('/')
.set('Authorization', 'Bearer wrongjwt')
.expect(401)
.expect('Invalid token\n')
.end(done);
});
it('should throw if authorization header is not valid jwt', function(done) {
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar'}, secret);
var app = koa();
app.use(koajwt({ secret: 'different-shhhh', debug: true }));
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(401)
.expect('Invalid token - invalid signature\n')
.end(done);
// assert.equal(err.message, 'invalid signature');
});
it('should throw if audience is not expected', function(done) {
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar', aud: 'expected-audience'}, secret);
var app = koa();
app.use(koajwt({ secret: 'shhhhhh', audience: 'not-expected-audience', debug: true }));
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(401)
.expect('Invalid token - jwt audience invalid. expected: expected-audience\n')
.end(done);
});
it('should throw if token is expired', function(done) {
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar', exp: 1382412921 }, secret);
var app = koa();
app.use(koajwt({ secret: 'shhhhhh', debug: true }));
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(401)
.expect('Invalid token - jwt expired\n')
.end(done);
});
it('should throw if token issuer is wrong', function(done) {
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar', iss: 'http://foo' }, secret);
var app = koa();
app.use(koajwt({ secret: 'shhhhhh', issuer: 'http://wrong', debug: true }));
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(401)
.expect('Invalid token - jwt issuer invalid. expected: http://foo\n')
.end(done);
});
});
describe('passthrough tests', function () {
it('should continue if `passthrough` is true', function(done) {
var app = koa();
app.use(koajwt({ secret: 'shhhhhh', passthrough: true, debug: true }));
app.use(function* (next) {
this.body = this.user;
});
request(app.listen())
.get('/')
.expect(204) // No content
.expect('')
.end(done);
});
});
describe('success tests', function () {
it('should work if authorization header is valid jwt', function(done) {
var validUserResponse = function(res) {
if (!(res.body.foo === 'bar')) return "Wrong user";
}
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar'}, secret);
var app = koa();
app.use(koajwt({ secret: secret }));
app.use(function* (next) {
this.body = this.user;
});
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(200)
.expect(validUserResponse)
.end(done);
});
it('should use provided key for decoded data', function(done) {
var validUserResponse = function(res) {
if (!(res.body.foo === 'bar')) return "Key param not used properly";
}
var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar'}, secret);
var app = koa();
app.use(koajwt({ secret: secret, key: 'jwtdata' }));
app.use(function* (next) {
this.body = this.jwtdata;
});
request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(200)
.expect(validUserResponse)
.end(done);
});
});