notification.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let server = require('../server')
  4. let should = chai.should()
  5. let { sequelize } = require('../models')
  6. const Errors = require('../lib/errors.js')
  7. chai.use(require('chai-http'))
  8. chai.use(require('chai-things'))
  9. describe('Notifications', () => {
  10. let admin = chai.request.agent(server)
  11. let user = chai.request.agent(server)
  12. //Wait for app to start before commencing
  13. before((done) => {
  14. function serverStarted () {
  15. admin
  16. .post('/api/v1/user')
  17. .set('content-type', 'application/json')
  18. .send({
  19. username: 'adminaccount',
  20. password: 'password',
  21. admin: true
  22. })
  23. .then(() => {
  24. return user
  25. .post('/api/v1/user')
  26. .set('content-type', 'application/json')
  27. .send({
  28. username: 'useraccount',
  29. password: 'password',
  30. })
  31. })
  32. .then(() => {
  33. return admin
  34. .post('/api/v1/category')
  35. .set('content-type', 'application/json')
  36. .send({ name: 'category' })
  37. })
  38. .then(() => {
  39. return admin
  40. .post('/api/v1/thread')
  41. .set('content-type', 'application/json')
  42. .send({ category: 'CATEGORY', name: 'thread' })
  43. })
  44. .then(_ => {
  45. done()
  46. })
  47. .catch(done)
  48. }
  49. if(server.locals.appStarted) serverStarted()
  50. server.on('appStarted', () => {
  51. serverStarted()
  52. })
  53. })
  54. after(() => sequelize.sync({ force: true }))
  55. describe('/notification', () => {
  56. it('should return no notifications', done => {
  57. user
  58. .get('/api/v1/notification')
  59. .end((err, res) => {
  60. res.should.have.status(200)
  61. res.should.be.json
  62. res.body.should.have.property('Notifications')
  63. res.body.Notifications.should.have.property('length', 0)
  64. res.body.should.have.property('unreadCount', 0)
  65. done()
  66. })
  67. })
  68. it('should return an error if not logged in', done => {
  69. chai.request(server)
  70. .get('/api/v1/notification')
  71. .end((err, res) => {
  72. res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
  73. done()
  74. })
  75. })
  76. it('should return a mention notification if user mentioned', async () => {
  77. await user
  78. .post('/api/v1/post')
  79. .set('content-type', 'application/json')
  80. .send({ threadId: 1, content: 'POST 1', mentions: ['adminaccount'] })
  81. await admin
  82. .post('/api/v1/post')
  83. .set('content-type', 'application/json')
  84. .send({ threadId: 1, content: 'POST 2', mentions: ['useraccount'] })
  85. await user
  86. .post('/api/v1/post')
  87. .set('content-type', 'application/json')
  88. .send({ threadId: 1, content: 'POST 3', mentions: ['adminaccount'] })
  89. let res = await admin.get('/api/v1/notification')
  90. res.should.have.status(200)
  91. res.should.be.json
  92. res.body.should.have.property('Notifications')
  93. res.body.Notifications.should.have.property('length', 2)
  94. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  95. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.User.username', 'useraccount')
  96. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.Post.content', '<p>POST 1</p>\n')
  97. res.body.should.have.property('unreadCount', 2)
  98. })
  99. it('should return an error if any mention is not a string', done => {
  100. user
  101. .post('/api/v1/post')
  102. .set('content-type', 'application/json')
  103. .send({ threadId: 1, content: 'POST 1', mentions: ['adminaccount', 123] })
  104. .end((err, res) => {
  105. res.should.have.status(400)
  106. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('mention', 'string'))
  107. done()
  108. })
  109. })
  110. it('should return an error if any mentions is not an array', async () => {
  111. let res = await user
  112. .post('/api/v1/post')
  113. .set('content-type', 'application/json')
  114. .send({ threadId: 1, content: 'POST 4', mentions: ['notrealaccount'] })
  115. res.should.be.json
  116. res.should.have.status(200)
  117. })
  118. it('should not crash if user doesnt exist', async () => {
  119. await admin.put('/api/v1/notification')
  120. let res = await admin.get('/api/v1/notification')
  121. res.should.have.status(200)
  122. res.should.be.json
  123. res.body.should.have.property('Notifications')
  124. res.body.Notifications.should.have.property('length', 2)
  125. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  126. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.User.username', 'useraccount')
  127. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.Post.content', '<p>POST 1</p>\n')
  128. res.body.should.have.property('unreadCount', 0)
  129. })
  130. it('should set unreadCount to 0', async () => {
  131. await admin.put('/api/v1/notification')
  132. let res = await admin.get('/api/v1/notification')
  133. res.should.have.status(200)
  134. res.should.be.json
  135. res.body.should.have.property('Notifications')
  136. res.body.Notifications.should.have.property('length', 2)
  137. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  138. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.User.username', 'useraccount')
  139. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.Post.content', '<p>POST 1</p>\n')
  140. res.body.should.have.property('unreadCount', 0)
  141. })
  142. })
  143. describe('/notification/:id', () => {
  144. it('should set notification as interacted', async () => {
  145. await admin.put('/api/v1/notification/1')
  146. let res = await admin.get('/api/v1/notification')
  147. res.should.have.status(200)
  148. res.should.be.json
  149. res.body.Notifications.should.have.property('length', 2)
  150. res.body.Notifications[0].should.have.property('interacted', true)
  151. })
  152. it('should return an error if notification does not exist', done => {
  153. admin
  154. .put('/api/v1/notification/notanid')
  155. .end((err, res) => {
  156. res.should.have.status(400)
  157. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'invalid notification id'))
  158. done()
  159. })
  160. })
  161. it('should delete a notification', async () => {
  162. await admin.delete('/api/v1/notification/1')
  163. await admin.delete('/api/v1/notification/3')
  164. let res = await admin.get('/api/v1/notification')
  165. res.should.have.status(200)
  166. res.should.be.json
  167. res.body.Notifications.should.have.property('length', 0)
  168. })
  169. it('should return an error if notification does not exist', done => {
  170. admin
  171. .delete('/api/v1/notification/notanid')
  172. .end((err, res) => {
  173. res.should.have.status(400)
  174. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'invalid notification id'))
  175. done()
  176. })
  177. })
  178. })
  179. })