admin_token.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. server.on('appStarted', () => {
  13. done()
  14. })
  15. })
  16. //Delete all rows in table after
  17. //tests completed
  18. after(() => sequelize.sync({ force: true }))
  19. describe('POST /admin_token', async () => {
  20. let token
  21. let agent = chai.request.agent(server)
  22. before((done) => {
  23. agent
  24. .post('/api/v1/user')
  25. .set('content-type', 'application/json')
  26. .send({
  27. username: 'adminaccount',
  28. password: 'password',
  29. admin: true
  30. })
  31. .then(() => {
  32. done()
  33. })
  34. .catch(done)
  35. })
  36. it('should generate a token if logged in', async () => {
  37. let res = await agent.post('/api/v1/admin_token')
  38. res.should.have.status(200)
  39. res.body.should.have.property('token')
  40. token = res.body.token
  41. })
  42. it('should generate a different token if logged in', async () => {
  43. let res = await agent.post('/api/v1/admin_token')
  44. res.should.have.status(200)
  45. res.body.should.have.property('token')
  46. res.body.token.should.not.equal(token)
  47. })
  48. it('should give an error if not logged in', async () => {
  49. try {
  50. let res = await chai.request(server).post('/api/v1/admin_token')
  51. res.should.have.status(403)
  52. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  53. } catch(res) {
  54. res.should.have.status(403)
  55. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  56. }
  57. })
  58. })
  59. })