category.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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('Category', () => {
  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((done) => {
  20. sequelize.sync({ force: true })
  21. .then(() => {
  22. done(null);
  23. })
  24. .catch((err) => {
  25. done(err)
  26. })
  27. })
  28. describe('POST /category', () => {
  29. let agent = chai.request.agent(server)
  30. it('should add a new category if logged in', async () => {
  31. await agent
  32. .post('/api/v1/user')
  33. .set('content-type', 'application/json')
  34. .send({
  35. username: 'adminaccount',
  36. password: 'password',
  37. admin: true
  38. })
  39. let res = await agent
  40. .post('/api/v1/category')
  41. .set('content-type', 'application/json')
  42. .send({ name: 'category' })
  43. res.should.be.json
  44. res.should.have.status(200)
  45. res.body.should.have.property('name', 'category')
  46. })
  47. it('should return an error if category already exists', async () => {
  48. try {
  49. let res = await agent
  50. .post('/api/v1/category')
  51. .set('content-type', 'application/json')
  52. .send({ name: 'category' })
  53. res.should.be.json
  54. res.should.have.status(400)
  55. res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  56. } catch (res) {
  57. res.should.have.status(400)
  58. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyExists)
  59. }
  60. })
  61. it('should return an error if not an admin account', async () => {
  62. let agent = chai.request.agent(server)
  63. await agent
  64. .post('/api/v1/user')
  65. .set('content-type', 'application/json')
  66. .send({
  67. username: 'username',
  68. password: 'password',
  69. })
  70. try {
  71. let res = await agent
  72. .post('/api/v1/category')
  73. .set('content-type', 'application/json')
  74. .send({ name: 'category1' })
  75. res.should.be.json
  76. res.should.have.status(401)
  77. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  78. } catch (res) {
  79. res.should.have.status(401)
  80. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  81. }
  82. })
  83. it('should return an error if not logged', async () => {
  84. try {
  85. await chai.request(server)
  86. .post('/api/v1/category')
  87. .set('content-type', 'application/json')
  88. .send({ name: 'category1' })
  89. res.should.be.json
  90. res.should.have.status(401)
  91. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  92. } catch (res) {
  93. res.should.have.status(401)
  94. JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  95. }
  96. })
  97. })
  98. describe('GET /category', () => {
  99. before(async () => {
  100. let agent = chai.request.agent(server)
  101. await agent
  102. .post('/api/v1/user/adminaccount/login')
  103. .set('content-type', 'application/json')
  104. .send({ password: 'password' })
  105. await agent
  106. .post('/api/v1/category')
  107. .set('content-type', 'application/json')
  108. .send({ name: 'another_category' })
  109. })
  110. it('should return all categories', async () => {
  111. let res = await chai.request(server)
  112. .get('/api/v1/category')
  113. res.should.be.json
  114. res.should.have.status(200)
  115. res.body.should.contain.an.item.with.property('name', 'category')
  116. res.body.should.contain.an.item.with.property('name', 'another_category')
  117. })
  118. })
  119. })