delete_thread.js 3.3 KB

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