notification.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 not crash if user doesnt exist', async () => {
  96. let deleteRes = await admin
  97. .delete('/api/v1/notification')
  98. deleteRes.should.be.json
  99. deleteRes.should.have.status(200)
  100. deleteRes.should.have.property('success', true)
  101. let res = await admin.get('/api/v1/notification')
  102. res.should.have.status(200)
  103. res.should.be.json
  104. res.body.should.have.property('Notifications')
  105. res.body.Notifications.should.have.property('length', 2)
  106. res.body.should.have.property('unreadCount', 0)
  107. })
  108. it('should set unreadCount to 0', async () => {
  109. await user
  110. .post('/api/v1/post')
  111. .set('content-type', 'application/json')
  112. .send({ threadId: 1, content: 'POST 1', mentions: ['adminaccount'] })
  113. await user
  114. .post('/api/v1/post')
  115. .set('content-type', 'application/json')
  116. .send({ threadId: 1, content: 'POST 2', mentions: ['adminaccount'] })
  117. let res = await admin.get('/api/v1/notification')
  118. res.should.have.status(200)
  119. res.should.be.json
  120. res.body.should.have.property('Notifications')
  121. res.body.Notifications.should.have.property('length', 2)
  122. res.body.Notifications.should.contain.something.that.has.deep.property('interacted', false)
  123. res.body.Notifications.should.contain.something.that.has.deep.property('User.username', 'useraccount')
  124. res.body.Notifications.should.contain.something.that.has.deep.property('Post.content', '<p>POST 1</p>\n')
  125. res.body.should.have.property('unreadCount', 2)
  126. })
  127. })
  128. describe('/notification/:id', () => {
  129. })
  130. })