notification.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.has.property('message', 'mentions must be an array of strings');
  107. done()
  108. })
  109. })
  110. it('should return an error if any mentions is not an array', done => {
  111. user
  112. .post('/api/v1/post')
  113. .set('content-type', 'application/json')
  114. .send({ threadId: 1, content: 'POST 4', mentions: 123 })
  115. .end((err, res) => {
  116. res.should.have.status(400)
  117. res.body.errors.should.contain.something.that.has.property('message', 'mentions must be an array of strings');
  118. done()
  119. })
  120. })
  121. it('should not crash if user doesnt exist', async () => {
  122. let res = await user
  123. .post('/api/v1/post')
  124. .set('content-type', 'application/json')
  125. .send({ threadId: 1, content: 'POST 4', mentions: ['notrealaccount'] });
  126. res.should.have.status(200);
  127. })
  128. it('should set unreadCount to 0', async () => {
  129. await admin.put('/api/v1/notification')
  130. let res = await admin.get('/api/v1/notification')
  131. res.should.have.status(200)
  132. res.should.be.json
  133. res.body.should.have.property('Notifications')
  134. res.body.Notifications.should.have.property('length', 2)
  135. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  136. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.User.username', 'useraccount')
  137. res.body.Notifications.should.contain.something.that.has.deep.property('PostNotification.Post.content', '<p>POST 1</p>\n')
  138. res.body.should.have.property('unreadCount', 0)
  139. })
  140. })
  141. describe('/notification/:id', () => {
  142. it('should set notification as interacted', async () => {
  143. await admin.put('/api/v1/notification/1')
  144. let res = await admin.get('/api/v1/notification')
  145. res.should.have.status(200)
  146. res.should.be.json
  147. res.body.Notifications.should.have.property('length', 2)
  148. res.body.Notifications[1].should.have.property('interacted', true)
  149. })
  150. it('should return an error if notification does not exist', done => {
  151. admin
  152. .put('/api/v1/notification/notanid')
  153. .end((err, res) => {
  154. res.should.have.status(400)
  155. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'invalid notification id'))
  156. done()
  157. })
  158. })
  159. it('should delete a notification', async () => {
  160. await admin.delete('/api/v1/notification/1')
  161. await admin.delete('/api/v1/notification/3')
  162. let res = await admin.get('/api/v1/notification')
  163. res.should.have.status(200)
  164. res.should.be.json
  165. res.body.Notifications.should.have.property('length', 0)
  166. })
  167. it('should return an error if notification does not exist', done => {
  168. admin
  169. .delete('/api/v1/notification/notanid')
  170. .end((err, res) => {
  171. res.should.have.status(400)
  172. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameter('id', 'invalid notification id'))
  173. done()
  174. })
  175. })
  176. })
  177. })