admin_token.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('AdminToken', () => {
  10. //Wait for app to start before commencing
  11. before((done) => {
  12. if(server.locals.appStarted) done()
  13. server.on('appStarted', () => {
  14. done()
  15. })
  16. })
  17. //Delete all rows in table after
  18. //tests completed
  19. after(() => sequelize.sync({ force: true }))
  20. describe('POST /admin_token', async () => {
  21. let token
  22. let agent = chai.request.agent(server)
  23. before((done) => {
  24. agent
  25. .post('/api/v1/user')
  26. .set('content-type', 'application/json')
  27. .send({
  28. username: 'adminaccount',
  29. password: 'password',
  30. admin: true
  31. })
  32. .then(() => {
  33. done()
  34. })
  35. .catch(done)
  36. })
  37. it('should generate a token if logged in', async () => {
  38. let res = await agent.post('/api/v1/admin_token')
  39. res.should.have.status(200)
  40. res.body.should.have.property('token')
  41. token = res.body.token
  42. })
  43. it('should generate a different token if logged in', async () => {
  44. let res = await agent.post('/api/v1/admin_token')
  45. res.should.have.status(200)
  46. res.body.should.have.property('token')
  47. res.body.token.should.not.equal(token)
  48. })
  49. it('should give an error if not logged in', async () => {
  50. try {
  51. let res = await chai.request(server).post('/api/v1/admin_token')
  52. res.should.have.status(401)
  53. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  54. } catch(res) {
  55. res.should.have.status(401)
  56. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  57. }
  58. })
  59. })
  60. })