delete_thread.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. await user
  67. .post('/api/v1/report')
  68. .set('content-type', 'application/json')
  69. .send({
  70. postId: 1,
  71. reason: 'inappropriate'
  72. })
  73. await user
  74. .post('/api/v1/report')
  75. .set('content-type', 'application/json')
  76. .send({
  77. postId: 2,
  78. reason: 'inappropriate'
  79. })
  80. return true
  81. } catch (e) {
  82. return e
  83. }
  84. })
  85. it('should delete a thread and corresponding posts', async () => {
  86. let res = await admin.delete('/api/v1/thread/1')
  87. res.should.be.json
  88. res.should.have.status(200)
  89. let thread = await Thread.findById(1)
  90. let posts = await Post.findAll({
  91. where: {
  92. ThreadId: 1
  93. }
  94. })
  95. let reports = await admin.get('/api/v1/report')
  96. expect(thread).to.be.null
  97. expect(posts).to.have.property('length', 0)
  98. expect(reports.body).to.have.property('length', 0)
  99. })
  100. it('should return an error if thread does not exist', done => {
  101. admin
  102. .delete('/api/v1/thread/404')
  103. .end((err, res) => {
  104. res.should.have.status(400)
  105. res.body.errors.should.contain.something.that.has.property(
  106. 'message',
  107. 'invalid thread id'
  108. )
  109. done()
  110. })
  111. })
  112. it('should retun an error if not an admin', done => {
  113. chai.request(server)
  114. .delete('/api/v1/thread/1')
  115. .end((err, res) => {
  116. res.should.have.status(401)
  117. res.body.errors.should.contain.something.which.deep.equals(Errors.requestNotAuthorized)
  118. done()
  119. })
  120. })
  121. })
  122. after(() => {
  123. sequelize.sync({ force: true })
  124. })
  125. })