report.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let expect = chai.expect
  6. let { sequelize, Report, Post, User } = require('../models')
  7. const Errors = require('../lib/errors.js')
  8. chai.use(require('chai-http'))
  9. chai.use(require('chai-things'))
  10. let adminAccount = chai.request.agent(server)
  11. let userAccount = chai.request.agent(server)
  12. describe('Report', () => {
  13. //Wait for app to start before commencing
  14. before((done) => {
  15. function createAccounts () {
  16. adminAccount
  17. .post('/api/v1/user')
  18. .set('content-type', 'application/json')
  19. .send({
  20. username: 'adminaccount',
  21. password: 'password',
  22. admin: true
  23. })
  24. .then(_ => {
  25. return userAccount
  26. .post('/api/v1/user')
  27. .set('content-type', 'application/json')
  28. .send({
  29. username: 'useraccount',
  30. password: 'password'
  31. })
  32. })
  33. .then(_ => {
  34. return adminAccount
  35. .post('/api/v1/category')
  36. .set('content-type', 'application/json')
  37. .send({ name: 'category_name' })
  38. })
  39. .then(_ => {
  40. return userAccount
  41. .post('/api/v1/thread')
  42. .set('content-type', 'application/json')
  43. .send({ name: 'thread', category: 'category_name' })
  44. })
  45. .then(_ => {
  46. return userAccount
  47. .post('/api/v1/post')
  48. .set('content-type', 'application/json')
  49. .send({ content: 'post to report', threadId: 1 })
  50. })
  51. .then(_ => {
  52. return userAccount
  53. .post('/api/v1/post')
  54. .set('content-type', 'application/json')
  55. .send({ content: 'post to report 2', threadId: 1 })
  56. })
  57. .then(_ => done())
  58. .catch(done)
  59. }
  60. if(server.locals.appStarted) createAccounts()
  61. server.on('appStarted', () => {
  62. createAccounts()
  63. })
  64. })
  65. //Delete all rows in table after
  66. //tests completed
  67. after((done) => {
  68. sequelize.sync({ force: true })
  69. .then(() => {
  70. done(null);
  71. })
  72. .catch((err) => {
  73. done(err)
  74. })
  75. })
  76. describe('POST /report', () => {
  77. it('should create a new report', async () => {
  78. let res = await userAccount
  79. .post('/api/v1/report')
  80. .set('content-type', 'application/json')
  81. .send({
  82. postId: 1,
  83. reason: 'spam'
  84. })
  85. res.should.have.status(200)
  86. res.should.be.json
  87. let report = await Report.findById(1)
  88. report.should.not.be.null
  89. report.should.have.property('reason', 'spam')
  90. })
  91. it('should return an error if not a logged in user', done => {
  92. chai.request(server)
  93. .post('/api/v1/report')
  94. .set('content-type', 'application/json')
  95. .send({
  96. postId: 1,
  97. reason: 'spam'
  98. })
  99. .end((err, res) => {
  100. res.should.have.status(401)
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  102. done()
  103. })
  104. })
  105. it('should return an error if invalid post id provided', done => {
  106. userAccount
  107. .post('/api/v1/report')
  108. .set('content-type', 'application/json')
  109. .send({
  110. postId: 'fake',
  111. reason: 'spam'
  112. })
  113. .end((err, res) => {
  114. res.should.have.status(400)
  115. res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
  116. done()
  117. })
  118. })
  119. it('should return an error if invalid report reason provided', done => {
  120. userAccount
  121. .post('/api/v1/report')
  122. .set('content-type', 'application/json')
  123. .send({
  124. postId: 1,
  125. reason: 'not a reason'
  126. })
  127. .end((err, res) => {
  128. res.should.have.status(400)
  129. res.body.errors.should.contain.something.that.has.property('message', 'Report reason can only be one of the pre-defined options')
  130. done()
  131. })
  132. })
  133. })
  134. describe('GET /report', () => {
  135. before(async () => {
  136. await Report.destroy({
  137. where: { id: 1 }
  138. })
  139. let report1 = await Report.create({ reason: 'spam' })
  140. let report2 = await Report.create({ reason: 'inappropriate' })
  141. let post1 = await Post.findById(1)
  142. let post2 = await Post.findById(2)
  143. let user = await User.find({
  144. where: { username: 'useraccount' }
  145. })
  146. await report1.setFlaggedByUser(user)
  147. await report1.setPost(post1)
  148. await report2.setFlaggedByUser(user)
  149. await report2.setPost(post2)
  150. })
  151. it('should return all reports', async () => {
  152. let res = await adminAccount
  153. .get('/api/v1/report')
  154. res.should.have.status(200)
  155. res.should.be.json
  156. res.body.should.have.length(2)
  157. res.body[0].should.have.deep.property('User.username', 'useraccount')
  158. res.body[0].should.have.deep.property('Post.id', 1)
  159. res.body[0].should.have.property('reason', 'spam')
  160. })
  161. it('should return an error if not admin account', done => {
  162. userAccount
  163. .get('/api/v1/report')
  164. .end((err, res) => {
  165. res.should.have.status(401)
  166. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  167. done()
  168. })
  169. })
  170. })
  171. })