report.js 7.6 KB

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