|
| 1 | +global.DATABASE_URL = 'mongodb://localhost/jwt-auth-demo-test'; |
| 2 | +const chai = require('chai'); |
| 3 | +const chaiHttp = require('chai-http'); |
| 4 | +const jwt = require('jsonwebtoken'); |
| 5 | + |
| 6 | +const {app, runServer, closeServer} = require('../server'); |
| 7 | +const {User} = require('../users'); |
| 8 | +const {JWT_SECRET} = require('../config'); |
| 9 | + |
| 10 | +const expect = chai.expect; |
| 11 | + |
| 12 | + |
| 13 | +// This let's us make HTTP requests |
| 14 | +// in our tests. |
| 15 | +// see: https://github.com/chaijs/chai-http |
| 16 | +chai.use(chaiHttp); |
| 17 | + |
| 18 | + |
| 19 | +describe('Auth endpoints', function() { |
| 20 | + const username = 'exampleUser'; |
| 21 | + const password = 'examplePass'; |
| 22 | + const firstName = 'Example'; |
| 23 | + const lastName = 'User'; |
| 24 | + |
| 25 | + before(function() { |
| 26 | + return runServer(); |
| 27 | + }); |
| 28 | + |
| 29 | + after(function() { |
| 30 | + return closeServer(); |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(function() { |
| 34 | + return User.hashPassword(password).then(password => |
| 35 | + User.create({ |
| 36 | + username, |
| 37 | + password, |
| 38 | + firstName, |
| 39 | + lastName |
| 40 | + }) |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(function() { |
| 45 | + return User.remove({}); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('/api/auth/login', function() { |
| 49 | + it('Should reject requests with no credentials', function() { |
| 50 | + return chai.request(app) |
| 51 | + .post('/api/auth/login') |
| 52 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 53 | + .catch(err => { |
| 54 | + if (err instanceof chai.AssertionError) { |
| 55 | + throw err; |
| 56 | + } |
| 57 | + |
| 58 | + const res = err.response; |
| 59 | + expect(res).to.have.status(401); |
| 60 | + }); |
| 61 | + }); |
| 62 | + it('Should reject requests with incorrect usernames', function() { |
| 63 | + return chai.request(app) |
| 64 | + .post('/api/auth/login') |
| 65 | + .auth('wrongUsername', password) |
| 66 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 67 | + .catch(err => { |
| 68 | + if (err instanceof chai.AssertionError) { |
| 69 | + throw err; |
| 70 | + } |
| 71 | + |
| 72 | + const res = err.response; |
| 73 | + expect(res).to.have.status(401); |
| 74 | + }); |
| 75 | + }); |
| 76 | + it('Should reject requests with incorrect passwords', function() { |
| 77 | + return chai.request(app) |
| 78 | + .post('/api/auth/login') |
| 79 | + .auth(username, 'wrongPassword') |
| 80 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 81 | + .catch(err => { |
| 82 | + if (err instanceof chai.AssertionError) { |
| 83 | + throw err; |
| 84 | + } |
| 85 | + |
| 86 | + const res = err.response; |
| 87 | + expect(res).to.have.status(401); |
| 88 | + }); |
| 89 | + }); |
| 90 | + it('Should return a valid auth token', function() { |
| 91 | + return chai.request(app) |
| 92 | + .post('/api/auth/login') |
| 93 | + .auth(username, password) |
| 94 | + .then(res => { |
| 95 | + expect(res).to.have.status(200); |
| 96 | + expect(res.body).to.be.an('object'); |
| 97 | + const token = res.body.authToken; |
| 98 | + expect(token).to.be.a('string'); |
| 99 | + const payload = jwt.verify(token, JWT_SECRET, { |
| 100 | + algorithm: ["HS256"] |
| 101 | + }); |
| 102 | + expect(payload.user).to.deep.equal({ |
| 103 | + username, |
| 104 | + firstName, |
| 105 | + lastName |
| 106 | + }); |
| 107 | + }) |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('/api/auth/refresh', function() { |
| 112 | + it('Should reject requests with no credentials', function() { |
| 113 | + return chai.request(app) |
| 114 | + .post('/api/auth/refresh') |
| 115 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 116 | + .catch(err => { |
| 117 | + if (err instanceof chai.AssertionError) { |
| 118 | + throw err; |
| 119 | + } |
| 120 | + |
| 121 | + const res = err.response; |
| 122 | + expect(res).to.have.status(401); |
| 123 | + }); |
| 124 | + }); |
| 125 | + it('Should reject requests with an invalid token', function() { |
| 126 | + const token = jwt.sign({ |
| 127 | + username, |
| 128 | + firstName, |
| 129 | + lastName |
| 130 | + }, 'wrongSecret', { |
| 131 | + algorithm: 'HS256', |
| 132 | + expiresIn: '7d' |
| 133 | + }); |
| 134 | + |
| 135 | + return chai.request(app) |
| 136 | + .post('/api/auth/refresh') |
| 137 | + .set('Authorization', `Bearer ${token}`) |
| 138 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 139 | + .catch(err => { |
| 140 | + if (err instanceof chai.AssertionError) { |
| 141 | + throw err; |
| 142 | + } |
| 143 | + |
| 144 | + const res = err.response; |
| 145 | + expect(res).to.have.status(401); |
| 146 | + }); |
| 147 | + }); |
| 148 | + it('Should reject requests with an expired token', function() { |
| 149 | + const token = jwt.sign({ |
| 150 | + user: { |
| 151 | + username, |
| 152 | + firstName, |
| 153 | + lastName |
| 154 | + }, |
| 155 | + exp: Math.floor(Date.now() / 1000) - 10 // Expired ten seconds ago |
| 156 | + }, JWT_SECRET, { |
| 157 | + algorithm: 'HS256', |
| 158 | + subject: username |
| 159 | + }); |
| 160 | + |
| 161 | + return chai.request(app) |
| 162 | + .post('/api/auth/refresh') |
| 163 | + .set('authorization', `Bearer ${token}`) |
| 164 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 165 | + .catch(err => { |
| 166 | + if (err instanceof chai.AssertionError) { |
| 167 | + throw err; |
| 168 | + } |
| 169 | + |
| 170 | + const res = err.response; |
| 171 | + expect(res).to.have.status(401); |
| 172 | + }); |
| 173 | + }); |
| 174 | + it('Should return a valid auth token with a newer expiry date', function() { |
| 175 | + const token = jwt.sign({ |
| 176 | + user: { |
| 177 | + username, |
| 178 | + firstName, |
| 179 | + lastName |
| 180 | + }, |
| 181 | + }, JWT_SECRET, { |
| 182 | + algorithm: 'HS256', |
| 183 | + subject: username, |
| 184 | + expiresIn: '7d' |
| 185 | + }); |
| 186 | + const decoded = jwt.decode(token); |
| 187 | + |
| 188 | + return chai.request(app) |
| 189 | + .post('/api/auth/refresh') |
| 190 | + .set('authorization', `Bearer ${token}`) |
| 191 | + .then(res => { |
| 192 | + expect(res).to.have.status(200); |
| 193 | + expect(res.body).to.be.an('object'); |
| 194 | + const token = res.body.authToken; |
| 195 | + expect(token).to.be.a('string'); |
| 196 | + const payload = jwt.verify(token, JWT_SECRET, { |
| 197 | + algorithm: ["HS256"] |
| 198 | + }); |
| 199 | + expect(payload.user).to.deep.equal({ |
| 200 | + username, |
| 201 | + firstName, |
| 202 | + lastName |
| 203 | + }); |
| 204 | + expect(payload.exp).to.be.at.least(decoded.exp); |
| 205 | + }); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments