category.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { User, Category } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Thread and post', () => {
  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. Promise.all([
  20. User.sync({ force: true }),
  21. Category.sync({ force: true })
  22. ])
  23. .then(() => {
  24. done(null);
  25. })
  26. .catch((err) => {
  27. done(err)
  28. })
  29. })
  30. describe('POST /category', () => {
  31. let agent = chai.request.agent(server)
  32. it('should add a new category if logged in', async () => {
  33. await agent
  34. .post('/api/v1/user')
  35. .set('content-type', 'application/json')
  36. .send({
  37. username: 'adminaccount',
  38. password: 'password',
  39. admin: true
  40. })
  41. let res = await agent
  42. .post('/api/v1/category')
  43. .set('content-type', 'application/json')
  44. .send({ name: 'category' })
  45. res.should.be.json
  46. res.should.have.status(200)
  47. res.body.should.have.property('name', 'category')
  48. })
  49. it('should return an error if category already exists', async () => {
  50. let res = await agent
  51. .post('/api/v1/category')
  52. .set('content-type', 'application/json')
  53. .send({ name: 'category' })
  54. res.should.be.json
  55. res.should.have.status(400)
  56. res.body.errors.should.contain.something.that.deep.equals(Errors.categoryAlreadyCreated)
  57. })
  58. it('should return an error if not an admin account', async () => {
  59. let agent = chai.request.agent(server)
  60. await agent
  61. .post('/api/v1/user')
  62. .set('content-type', 'application/json')
  63. .send({
  64. username: 'username',
  65. password: 'password',
  66. })
  67. let res = await agent
  68. .post('/api/v1/category')
  69. .set('content-type', 'application/json')
  70. .send({ name: 'category1' })
  71. res.should.be.json
  72. res.should.have.status(401)
  73. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  74. })
  75. it('should return an error if not logged', async () => {
  76. await chai.request(server)
  77. .post('/api/v1/category')
  78. .set('content-type', 'application/json')
  79. .send({ name: 'category1' })
  80. res.should.be.json
  81. res.should.have.status(401)
  82. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  83. })
  84. })
  85. describe('GET /category', async () => {
  86. let agent = chai.request.agent(server)
  87. await agent
  88. .post('/api/v1/user/adminaccount')
  89. .set('content-type', 'application/json')
  90. .send({ password: 'password' })
  91. await agent
  92. .post('/api/v1/category')
  93. .set('content-type', 'application/json')
  94. .send({ name: 'another_category' })
  95. it('should return all categories', async () => {
  96. let res = await chai.request(server)
  97. .get('/api/v1/category')
  98. res.should.be.json
  99. res.should.have.status(200)
  100. res.body.should.contain.an.item.with.property('name', 'category')
  101. res.body.should.contain.an.item.with.property('name', 'another_category')
  102. })
  103. })
  104. })