delete_thread.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize, Thread, Post, User } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. let expect = chai.expect
  10. describe('Thread', () => {
  11. let admin = chai.request.agent(server)
  12. let user = chai.request.agent(server)
  13. //Wait for app to start before commencing
  14. before((done) => {
  15. if(server.locals.appStarted) done()
  16. server.on('appStarted', () => {
  17. done()
  18. })
  19. })
  20. describe('Delete', () => {
  21. before(async () => {
  22. try {
  23. let accounts = []
  24. accounts.push(
  25. admin
  26. .post('/api/v1/user')
  27. .set('content-type', 'application/json')
  28. .send({
  29. username: 'adminaccount',
  30. password: 'password',
  31. admin: true
  32. })
  33. )
  34. accounts.push(
  35. user
  36. .post('/api/v1/user')
  37. .set('content-type', 'application/json')
  38. .send({
  39. username: 'useraccount1',
  40. password: 'password'
  41. })
  42. )
  43. await Promise.all(accounts)
  44. await admin
  45. .post('/api/v1/category')
  46. .set('content-type', 'application/json')
  47. .send({ name: 'category' })
  48. await user
  49. .post('/api/v1/thread')
  50. .set('content-type', 'application/json')
  51. .send({ category: 'CATEGORY', name: 'thread' })
  52. await user
  53. .post('/api/v1/post')
  54. .set('content-type', 'application/json')
  55. .send({
  56. content: 'post 1',
  57. threadId: 1
  58. })
  59. await user
  60. .post('/api/v1/post')
  61. .set('content-type', 'application/json')
  62. .send({
  63. content: 'post 2',
  64. threadId: 1
  65. })
  66. return true
  67. } catch (e) {
  68. return e
  69. }
  70. })
  71. it('should delete a thread and corresponding posts', async () => {
  72. let res = await admin.delete('/api/v1/thread/1')
  73. res.should.be.json
  74. res.should.have.status(200)
  75. let thread = await Thread.findById(1)
  76. let posts = await Post.findAll({
  77. where: {
  78. ThreadId: 1
  79. }
  80. })
  81. expect(thread).to.be.null
  82. expect(posts).to.have.property('length', 0)
  83. })
  84. it('should return an error if thread does not exist', done => {
  85. admin
  86. .delete('/api/v1/thread/404')
  87. .end((err, res) => {
  88. res.should.have.status(400)
  89. res.body.errors.should.contain.something.that.has.property(
  90. 'message',
  91. 'invalid thread id'
  92. )
  93. done()
  94. })
  95. })
  96. it('should retun an error if not an admin', done => {
  97. chai.request(server)
  98. .delete('/api/v1/thread/1')
  99. .end((err, res) => {
  100. res.should.have.status(401)
  101. res.body.errors.should.contain.something.which.deep.equals(Errors.requestNotAuthorized)
  102. done()
  103. })
  104. })
  105. })
  106. after(() => {
  107. sequelize.sync({ force: true })
  108. })
  109. })