category.js 3.6 KB

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