ban.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. process.env.NODE_ENV = 'test'
  2. process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
  3. let chai = require('chai')
  4. let server = require('../server')
  5. let should = chai.should()
  6. let expect = chai.expect
  7. let { sequelize, User, Ban } = require('../models')
  8. const Errors = require('../lib/errors.js')
  9. chai.use(require('chai-http'))
  10. chai.use(require('chai-things'))
  11. let adminAccount = chai.request.agent(server)
  12. let userAccount = chai.request.agent(server)
  13. let userAccountId, anotherUserAccountId
  14. describe('Ban', () => {
  15. //Wait for app to start before commencing
  16. before((done) => {
  17. function createAccounts () {
  18. adminAccount
  19. .post('/api/v1/user')
  20. .set('content-type', 'application/json')
  21. .send({
  22. username: 'adminaccount',
  23. password: 'password',
  24. admin: true
  25. })
  26. .then(_ => {
  27. return userAccount
  28. .post('/api/v1/user')
  29. .set('content-type', 'application/json')
  30. .send({
  31. username: 'anotheruseraccount',
  32. password: 'password'
  33. })
  34. })
  35. .then(res => {
  36. anotherUserAccountId = res.body.id
  37. return userAccount
  38. .post('/api/v1/user')
  39. .set('content-type', 'application/json')
  40. .send({
  41. username: 'useraccount',
  42. password: 'password'
  43. })
  44. })
  45. .then(res => {
  46. userAccountId = res.body.id
  47. return adminAccount
  48. .post('/api/v1/category')
  49. .set('content-type', 'application/json')
  50. .send({ name: 'category_name' })
  51. })
  52. .then(_ => {
  53. return userAccount
  54. .post('/api/v1/thread')
  55. .set('content-type', 'application/json')
  56. .send({ name: 'thread', category: 'category_name' })
  57. })
  58. .then(_ => done())
  59. .catch(done)
  60. }
  61. if(server.locals.appStarted) createAccounts()
  62. server.on('appStarted', () => {
  63. createAccounts()
  64. })
  65. })
  66. //Delete all rows in table after
  67. //tests completed
  68. after(() => sequelize.sync({ force: true }) )
  69. describe('/ban POST', () => {
  70. it('should create a ban for the user', async () => {
  71. let res = await adminAccount
  72. .post('/api/v1/ban')
  73. .set('content-type', 'application/json')
  74. .send({
  75. canCreatePosts: false,
  76. canCreateThreads: false,
  77. userId: userAccountId,
  78. message: 'ban message'
  79. })
  80. res.should.be.json
  81. res.should.have.status(200)
  82. let ban = await Ban.findById(1)
  83. ban.should.have.property('canCreatePosts', false)
  84. ban.should.have.property('canCreateThreads', false)
  85. ban.should.have.property('message', 'ban message')
  86. ban.should.have.property('UserId', userAccountId)
  87. })
  88. it('should return an error if not an administrator', done => {
  89. userAccount
  90. .post('/api/v1/ban')
  91. .set('content-type', 'application/json')
  92. .send({
  93. canCreatePosts: false,
  94. canCreateThreads: false,
  95. userId: userAccountId,
  96. message: 'ban message'
  97. })
  98. .end((err, res) => {
  99. res.should.be.json
  100. res.should.have.status(400)
  101. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  102. done()
  103. })
  104. })
  105. it('should return an error if user id is not valid', done => {
  106. adminAccount
  107. .post('/api/v1/ban')
  108. .set('content-type', 'application/json')
  109. .send({
  110. canCreatePosts: false,
  111. canCreateThreads: false,
  112. userId: 'not an id',
  113. message: 'ban message'
  114. })
  115. .end((err, res) => {
  116. res.should.be.json
  117. res.should.have.status(400)
  118. res.body.errors.should.contain.something.that.has.property('message', 'user does not exist')
  119. done()
  120. })
  121. })
  122. it('should return an error if trying to post replies if permissions so set', done => {
  123. userAccount
  124. .post('/api/v1/post')
  125. .set('content-type', 'application/json')
  126. .send({
  127. threadId: 1,
  128. content: 'post'
  129. })
  130. .end((err, res) => {
  131. res.should.be.json
  132. res.should.have.status(400)
  133. res.body.errors.should.contain.something.with.property('message', 'ban message')
  134. done()
  135. })
  136. })
  137. it('should return an error if trying to create thread if permissions so set', done => {
  138. userAccount
  139. .post('/api/v1/thread')
  140. .set('content-type', 'application/json')
  141. .send({
  142. category: 'category',
  143. name: 'thread name'
  144. })
  145. .end((err, res) => {
  146. res.should.be.json
  147. res.should.have.status(400)
  148. res.body.errors.should.contain.something.with.property('message', 'ban message')
  149. done()
  150. })
  151. })
  152. })
  153. describe('/ban GET', () => {
  154. before(done => {
  155. adminAccount
  156. .post('/api/v1/ban')
  157. .set('content-type', 'application/json')
  158. .send({
  159. canCreatePosts: false,
  160. canCreateThreads: false,
  161. userId: anotherUserAccountId,
  162. message: 'ban message2'
  163. })
  164. .end((err, res) => {
  165. done()
  166. })
  167. })
  168. it('should get all bans', async () => {
  169. let res = await adminAccount.get('/api/v1/ban')
  170. res.should.be.json
  171. res.should.have.status(200)
  172. res.body.should.contain.something.with.deep.property('User.username', 'useraccount')
  173. res.body.should.have.length(2)
  174. })
  175. })
  176. describe('/ban/:ban_id DELETE', () => {
  177. it('should remove a ban', async () => {
  178. let res = await adminAccount.delete('/api/v1/ban/1')
  179. res.should.be.json
  180. res.should.have.status(200)
  181. let ban = await Ban.findAll()
  182. ban.should.have.length(1)
  183. ban[0].should.have.property('id', 2)
  184. })
  185. it('should return an error if ban id is not valid', done => {
  186. adminAccount.delete('/api/v1/ban/notarealid')
  187. .end((err, res) => {
  188. res.should.be.json
  189. res.should.have.status(400)
  190. res.body.errors.should.contain.something.that.has.property('message', 'ban does not exist')
  191. done()
  192. })
  193. })
  194. })
  195. })