ban.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. username: 'useraccount',
  78. message: 'ban message'
  79. })
  80. res.should.be.json
  81. res.should.have.status(200)
  82. res.body.should.have.deep.property('User.username', 'useraccount')
  83. let ban = await Ban.findById(1)
  84. ban.should.have.property('canCreatePosts', false)
  85. ban.should.have.property('canCreateThreads', false)
  86. ban.should.have.property('message', 'ban message')
  87. ban.should.have.property('UserId', userAccountId)
  88. })
  89. it('should return an error if not an administrator', done => {
  90. userAccount
  91. .post('/api/v1/ban')
  92. .set('content-type', 'application/json')
  93. .send({
  94. canCreatePosts: false,
  95. canCreateThreads: false,
  96. username: 'useraccount',
  97. message: 'ban message'
  98. })
  99. .end((err, res) => {
  100. res.should.be.json
  101. res.should.have.status(400)
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  103. done()
  104. })
  105. })
  106. it('should return an error if user id is not valid', done => {
  107. adminAccount
  108. .post('/api/v1/ban')
  109. .set('content-type', 'application/json')
  110. .send({
  111. canCreatePosts: false,
  112. canCreateThreads: false,
  113. username: 'not a user',
  114. message: 'ban message'
  115. })
  116. .end((err, res) => {
  117. res.should.be.json
  118. res.should.have.status(400)
  119. res.body.errors.should.contain.something.that.has.property('message', 'user does not exist')
  120. done()
  121. })
  122. })
  123. it('should return an error if trying to post replies if permissions so set', done => {
  124. userAccount
  125. .post('/api/v1/post')
  126. .set('content-type', 'application/json')
  127. .send({
  128. threadId: 1,
  129. content: 'post'
  130. })
  131. .end((err, res) => {
  132. res.should.be.json
  133. res.should.have.status(400)
  134. res.body.errors.should.contain.something.with.property('message', 'ban message')
  135. done()
  136. })
  137. })
  138. it('should return an error if trying to create thread if permissions so set', done => {
  139. userAccount
  140. .post('/api/v1/thread')
  141. .set('content-type', 'application/json')
  142. .send({
  143. category: 'category',
  144. name: 'thread name'
  145. })
  146. .end((err, res) => {
  147. res.should.be.json
  148. res.should.have.status(400)
  149. res.body.errors.should.contain.something.with.property('message', 'ban message')
  150. done()
  151. })
  152. })
  153. })
  154. describe('/ban GET', () => {
  155. before(done => {
  156. adminAccount
  157. .post('/api/v1/ban')
  158. .set('content-type', 'application/json')
  159. .send({
  160. canCreatePosts: false,
  161. canCreateThreads: false,
  162. username: 'anotheruseraccount',
  163. message: 'ban message2'
  164. })
  165. .end((err, res) => {
  166. done()
  167. })
  168. })
  169. it('should get all bans', async () => {
  170. let res = await adminAccount.get('/api/v1/ban')
  171. res.should.be.json
  172. res.should.have.status(200)
  173. res.body.should.contain.something.with.deep.property('User.username', 'useraccount')
  174. res.body.should.have.length(2)
  175. })
  176. })
  177. describe('/ban/:ban_id DELETE', () => {
  178. it('should remove a ban', async () => {
  179. let res = await adminAccount.delete('/api/v1/ban/1')
  180. res.should.be.json
  181. res.should.have.status(200)
  182. let ban = await Ban.findAll()
  183. ban.should.have.length(1)
  184. ban[0].should.have.property('id', 2)
  185. })
  186. it('should return an error if ban id is not valid', done => {
  187. adminAccount.delete('/api/v1/ban/notarealid')
  188. .end((err, res) => {
  189. res.should.be.json
  190. res.should.have.status(400)
  191. res.body.errors.should.contain.something.that.has.property('message', 'ban does not exist')
  192. done()
  193. })
  194. })
  195. })
  196. })