1 | var chai = require('chai')
|
2 | , Strategy = require('../lib/strategy')
|
3 | , test_data = require('./testdata')
|
4 | , sinon = require('sinon');
|
5 |
|
6 |
|
7 | describe('Strategy', function() {
|
8 |
|
9 | before(function() {
|
10 | Strategy.JwtVerifier = sinon.stub();
|
11 | Strategy.JwtVerifier.callsArgWith(3, null, test_data.valid_jwt.payload);
|
12 | });
|
13 |
|
14 | describe('Handling a request with a valid JWT and succesful verification', function() {
|
15 |
|
16 | var strategy, user, info;
|
17 |
|
18 | before(function(done) {
|
19 | strategy = new Strategy({secretOrKey: 'secret'}, function(jwt_paylod, next) {
|
20 | return next(null, {user_id: 1234567890}, {foo:'bar'});
|
21 | });
|
22 |
|
23 | chai.passport.use(strategy)
|
24 | .success(function(u, i) {
|
25 | user = u;
|
26 | info = i;
|
27 | done();
|
28 | })
|
29 | .req(function(req) {
|
30 | req.headers['authorization'] = "JWT " + test_data.valid_jwt.token;
|
31 | })
|
32 | .authenticate();
|
33 | });
|
34 |
|
35 |
|
36 | it('should provide a user', function() {
|
37 | expect(user).to.be.an.object;
|
38 | expect(user.user_id).to.equal(1234567890);
|
39 | });
|
40 |
|
41 |
|
42 | it('should forward info', function() {
|
43 | expect(info).to.be.an.object;
|
44 | expect(info.foo).to.equal('bar');
|
45 | });
|
46 |
|
47 | });
|
48 |
|
49 |
|
50 |
|
51 | describe('handling a request with valid jwt and failed verification', function() {
|
52 |
|
53 | var strategy, info;
|
54 |
|
55 | before(function(done) {
|
56 | strategy = new Strategy({secretOrKey: 'secret'}, function(jwt_payload, next) {
|
57 | return next(null, false, {message: 'invalid user'});
|
58 | });
|
59 |
|
60 | chai.passport.use(strategy)
|
61 | .fail(function(i) {
|
62 | info = i;
|
63 | done();
|
64 | })
|
65 | .req(function(req) {
|
66 | req.headers['authorization'] = "JWT " + test_data.valid_jwt.token;
|
67 | })
|
68 | .authenticate();
|
69 | });
|
70 |
|
71 |
|
72 | it('should fail with info', function() {
|
73 | expect(info).to.be.an.object;
|
74 | expect(info.message).to.equal('invalid user');
|
75 | });
|
76 |
|
77 | });
|
78 |
|
79 |
|
80 |
|
81 | describe('handling a request with a valid jwt and an error during verification', function() {
|
82 |
|
83 | var strategy, err;
|
84 |
|
85 | before(function(done) {
|
86 | strategy = new Strategy({secretOrKey: 'secrety'}, function(jwt_payload, next) {
|
87 | return next(new Error("ERROR"), false, {message: 'invalid user'});
|
88 | });
|
89 |
|
90 | chai.passport.use(strategy)
|
91 | .error(function(e) {
|
92 | err = e;
|
93 | done();
|
94 | })
|
95 | .req(function(req) {
|
96 | req.headers['authorization'] = "JWT " + test_data.valid_jwt.token;
|
97 | })
|
98 | .authenticate();
|
99 | });
|
100 |
|
101 |
|
102 | it('should error', function() {
|
103 | expect(err).to.be.an.instanceof(Error);
|
104 | expect(err.message).to.equal('ERROR');
|
105 | });
|
106 |
|
107 | });
|
108 |
|
109 |
|
110 |
|
111 | describe('hanlding a request with a valid jwt and an exception during verification', function() {
|
112 | var strategy, err;
|
113 |
|
114 | before(function(done) {
|
115 | strategy = new Strategy({secretOrKey: 'secret'}, function(jwt_payload, next) {
|
116 | throw new Error("EXCEPTION");
|
117 | });
|
118 |
|
119 | chai.passport.use(strategy)
|
120 | .error(function(e) {
|
121 | err = e;
|
122 | done();
|
123 | })
|
124 | .req(function(req) {
|
125 | req.headers['authorization'] = "JWT " + test_data.valid_jwt.token;
|
126 | })
|
127 | .authenticate();
|
128 | });
|
129 |
|
130 |
|
131 | it('should error', function() {
|
132 | expect(err).to.be.an.instanceof(Error);
|
133 | expect(err.message).to.equal('EXCEPTION');
|
134 | });
|
135 |
|
136 | });
|
137 |
|
138 |
|
139 |
|
140 | describe('handing a request with a valid jwt and option passReqToCallback is true', function() {
|
141 |
|
142 | var strategy, expected_request, request_arg;
|
143 |
|
144 | before(function(done) {
|
145 | opts = { passReqToCallback: true };
|
146 | opts.secretOrKey = 'secret';
|
147 | strategy = new Strategy(opts, function(request, jwt_payload, next) {
|
148 |
|
149 | request_arg = request;
|
150 | return next(null, {user_id: 1234567890}, {foo:'bar'});
|
151 | });
|
152 |
|
153 | chai.passport.use(strategy)
|
154 | .success(function(u, i) {
|
155 | done();
|
156 | })
|
157 | .req(function(req) {
|
158 | req.headers['authorization'] = "JWT " + test_data.valid_jwt.token;
|
159 | expected_request = req;
|
160 | })
|
161 | .authenticate();
|
162 | });
|
163 |
|
164 | it('will call verify with request as the first argument', function() {
|
165 | expect(expected_request).to.equal(request_arg);
|
166 | });
|
167 |
|
168 | });
|
169 |
|
170 | });
|