notification.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 user
  82. .post('/api/v1/post')
  83. .set('content-type', 'application/json')
  84. .send({ threadId: 1, content: 'POST 2', mentions: ['adminaccount'] })
  85. let res = await admin.get('/api/v1/notification')
  86. res.should.have.status(200)
  87. res.should.be.json
  88. res.body.should.have.property('Notifications')
  89. res.body.Notifications.should.have.property('length', 2)
  90. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  91. res.body.Notifications.should.contain.something.that.has.deep.property('User.username', 'useraccount')
  92. res.body.Notifications.should.contain.something.that.has.deep.property('Post.content', '<p>POST 1</p>\n')
  93. res.body.should.have.property('unreadCount', 2)
  94. })
  95. it('should return an error if any mention is not a string', done => {
  96. user
  97. .post('/api/v1/post')
  98. .set('content-type', 'application/json')
  99. .send({ threadId: 1, content: 'POST 1', mentions: ['adminaccount', 123] })
  100. .end((err, res) => {
  101. res.should.have.status(400)
  102. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('mention', 'string'))
  103. done()
  104. })
  105. })
  106. it('should return an error if any mentions is not an array', done => {
  107. user
  108. .post('/api/v1/post')
  109. .set('content-type', 'application/json')
  110. .send({ threadId: 1, content: 'POST 1', mentions: false })
  111. .end((err, res) => {
  112. res.should.have.status(400)
  113. res.body.errors.should.contain.something.that.deep.equals(Errors.invalidParameterType('mentions', 'array'))
  114. done()
  115. })
  116. })
  117. it('should not crash if user doesnt exist', async () => {
  118. let deleteRes = await admin
  119. .delete('/api/v1/notification')
  120. deleteRes.should.be.json
  121. deleteRes.should.have.status(200)
  122. deleteRes.should.have.property('success', true)
  123. let res = await admin.get('/api/v1/notification')
  124. res.should.have.status(200)
  125. res.should.be.json
  126. res.body.should.have.property('Notifications')
  127. res.body.Notifications.should.have.property('length', 2)
  128. res.body.should.have.property('unreadCount', 0)
  129. })
  130. it('should set unreadCount to 0', async () => {
  131. await user
  132. .post('/api/v1/post')
  133. .set('content-type', 'application/json')
  134. .send({ threadId: 1, content: 'POST 1', mentions: ['adminaccount'] })
  135. await user
  136. .post('/api/v1/post')
  137. .set('content-type', 'application/json')
  138. .send({ threadId: 1, content: 'POST 2', mentions: ['adminaccount'] })
  139. let res = await admin.get('/api/v1/notification')
  140. res.should.have.status(200)
  141. res.should.be.json
  142. res.body.should.have.property('Notifications')
  143. res.body.Notifications.should.have.property('length', 2)
  144. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  145. res.body.Notifications.should.contain.something.that.has.deep.property('User.username', 'useraccount')
  146. res.body.Notifications.should.contain.something.that.has.deep.property('Post.content', '<p>POST 1</p>\n')
  147. res.body.should.have.property('unreadCount', 2)
  148. })
  149. })
  150. describe('/notification/:id', () => {
  151. })
  152. })