|
| 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/secret', function() { |
| 49 | + it('Should reject requests with no credentials', function() { |
| 50 | + return chai.request(app) |
| 51 | + .get('/api/secret') |
| 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 | + |
| 63 | + it('Should reject requests with an invalid token', function() { |
| 64 | + const token = jwt.sign({ |
| 65 | + username, |
| 66 | + firstName, |
| 67 | + lastName |
| 68 | + }, 'wrongSecret', { |
| 69 | + algorithm: 'HS256', |
| 70 | + expiresIn: '7d' |
| 71 | + }); |
| 72 | + |
| 73 | + return chai.request(app) |
| 74 | + .get('/api/secret') |
| 75 | + .set('Authorization', `Bearer ${token}`) |
| 76 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 77 | + .catch(err => { |
| 78 | + if (err instanceof chai.AssertionError) { |
| 79 | + throw err; |
| 80 | + } |
| 81 | + |
| 82 | + const res = err.response; |
| 83 | + expect(res).to.have.status(401); |
| 84 | + }); |
| 85 | + }); |
| 86 | + it('Should reject requests with an expired token', function() { |
| 87 | + const token = jwt.sign({ |
| 88 | + user: { |
| 89 | + username, |
| 90 | + firstName, |
| 91 | + lastName |
| 92 | + }, |
| 93 | + exp: Math.floor(Date.now() / 1000) - 10 // Expired ten seconds ago |
| 94 | + }, JWT_SECRET, { |
| 95 | + algorithm: 'HS256', |
| 96 | + subject: username |
| 97 | + }); |
| 98 | + |
| 99 | + return chai.request(app) |
| 100 | + .get('/api/secret') |
| 101 | + .set('authorization', `Bearer ${token}`) |
| 102 | + .then(() => expect.fail(null, null, 'Request should not succeed')) |
| 103 | + .catch(err => { |
| 104 | + if (err instanceof chai.AssertionError) { |
| 105 | + throw err; |
| 106 | + } |
| 107 | + |
| 108 | + const res = err.response; |
| 109 | + expect(res).to.have.status(401); |
| 110 | + }); |
| 111 | + }); |
| 112 | + it('Should send a secret', function() { |
| 113 | + const token = jwt.sign({ |
| 114 | + user: { |
| 115 | + username, |
| 116 | + firstName, |
| 117 | + lastName |
| 118 | + }, |
| 119 | + }, JWT_SECRET, { |
| 120 | + algorithm: 'HS256', |
| 121 | + subject: username, |
| 122 | + expiresIn: '7d' |
| 123 | + }); |
| 124 | + |
| 125 | + return chai.request(app) |
| 126 | + .get('/api/secret') |
| 127 | + .set('authorization', `Bearer ${token}`) |
| 128 | + .then(res => { |
| 129 | + expect(res).to.have.status(200); |
| 130 | + expect(res.body).to.be.an('object'); |
| 131 | + expect(res.body.secret).to.equal('rosebud'); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments