report.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 be fine with multiple reports from one user', async () => {
  92. let res = await userAccount
  93. .post('/api/v1/report')
  94. .set('content-type', 'application/json')
  95. .send({
  96. postId: 1,
  97. reason: 'inappropriate'
  98. })
  99. res.should.have.status(200)
  100. res.should.be.json
  101. let report1 = await Report.findById(1, {
  102. include: [{ model: User, as: 'FlaggedByUser' }]
  103. })
  104. report1.should.not.be.null
  105. report1.should.have.deep.property('FlaggedByUser.username', 'useraccount')
  106. report1.should.have.property('reason', 'spam')
  107. let report2 = await Report.findById(res.body.id, {
  108. include: [{ model: User, as: 'FlaggedByUser' }]
  109. })
  110. report2.should.not.be.null
  111. report2.should.have.deep.property('FlaggedByUser.username', 'useraccount')
  112. report2.should.have.property('reason', 'inappropriate')
  113. })
  114. it('should return an error if not a logged in user', done => {
  115. chai.request(server)
  116. .post('/api/v1/report')
  117. .set('content-type', 'application/json')
  118. .send({
  119. postId: 1,
  120. reason: 'spam'
  121. })
  122. .end((err, res) => {
  123. res.should.have.status(401)
  124. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  125. done()
  126. })
  127. })
  128. it('should return an error if invalid post id provided', done => {
  129. userAccount
  130. .post('/api/v1/report')
  131. .set('content-type', 'application/json')
  132. .send({
  133. postId: 'fake',
  134. reason: 'spam'
  135. })
  136. .end((err, res) => {
  137. res.should.have.status(400)
  138. res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
  139. done()
  140. })
  141. })
  142. it('should return an error if invalid report reason provided', done => {
  143. userAccount
  144. .post('/api/v1/report')
  145. .set('content-type', 'application/json')
  146. .send({
  147. postId: 1,
  148. reason: 'not a reason'
  149. })
  150. .end((err, res) => {
  151. res.should.have.status(400)
  152. res.body.errors.should.contain.something.that.has.property('message', 'Report reason can only be one of the pre-defined options')
  153. done()
  154. })
  155. })
  156. })
  157. describe('GET /report', () => {
  158. before(async () => {
  159. await Report.destroy({
  160. where: {}
  161. })
  162. let report1 = await Report.create({ reason: 'spam' })
  163. let report2 = await Report.create({ reason: 'inappropriate' })
  164. let post1 = await Post.findById(1)
  165. let post2 = await Post.findById(2)
  166. let user = await User.find({
  167. where: { username: 'useraccount' }
  168. })
  169. await report1.setFlaggedByUser(user)
  170. await report1.setPost(post1)
  171. await report2.setFlaggedByUser(user)
  172. await report2.setPost(post2)
  173. })
  174. it('should return all reports', async () => {
  175. let res = await adminAccount
  176. .get('/api/v1/report')
  177. res.should.have.status(200)
  178. res.should.be.json
  179. res.body.should.have.length(2)
  180. res.body.should.contain.something.with.deep.property('FlaggedByUser.username', 'useraccount')
  181. res.body.should.contain.something.with.deep.property('reason', 'spam')
  182. res.body.should.contain.something.with.deep.property('reason', 'inappropriate')
  183. })
  184. it('should return an error if not admin account', done => {
  185. userAccount
  186. .get('/api/v1/report')
  187. .end((err, res) => {
  188. res.should.have.status(401)
  189. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  190. done()
  191. })
  192. })
  193. })
  194. describe('DELETE /report/:id', () => {
  195. let reportId;
  196. before(async () => {
  197. let report = await Report.create({ reason: 'spam' })
  198. let post = await Post.findById(1)
  199. let user = await User.find({
  200. where: { username: 'useraccount' }
  201. })
  202. await report.setFlaggedByUser(user)
  203. await report.setPost(post)
  204. reportId = report.id
  205. })
  206. it('should delete the report', async () => {
  207. let res = await adminAccount.delete('/api/v1/report/' + reportId)
  208. res.should.have.status(200)
  209. let report = await Report.findById(reportId)
  210. expect(report).to.be.null
  211. })
  212. it('should return an error if not an admin', done => {
  213. userAccount
  214. .delete('/api/v1/report/2')
  215. .end((err, res) => {
  216. res.should.have.status(401)
  217. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  218. done()
  219. })
  220. })
  221. it('should return an error if invalid id', done => {
  222. adminAccount
  223. .delete('/api/v1/report/fake')
  224. .end((err, res) => {
  225. res.should.have.status(400)
  226. res.body.errors.should.contain.something.that.has.property('message', 'Post id is not valid')
  227. done()
  228. })
  229. })
  230. })
  231. })