NotificationsHandlerTests.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const SandboxedModule = require('sandboxed-module')
  2. const { assert } = require('chai')
  3. const sinon = require('sinon')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/Features/Notifications/NotificationsHandler.js'
  7. )
  8. describe('NotificationsHandler', function () {
  9. const userId = '123nd3ijdks'
  10. const notificationId = '123njdskj9jlk'
  11. const notificationUrl = 'notification.overleaf.testing'
  12. beforeEach(function () {
  13. this.request = sinon.stub().callsArgWith(1)
  14. this.handler = SandboxedModule.require(modulePath, {
  15. requires: {
  16. '@overleaf/settings': {
  17. apis: { notifications: { url: notificationUrl } },
  18. },
  19. request: this.request,
  20. },
  21. })
  22. })
  23. describe('getUserNotifications', function () {
  24. it('should get unread notifications', async function () {
  25. const stubbedNotifications = [{ _id: notificationId, user_id: userId }]
  26. this.request.callsArgWith(
  27. 1,
  28. null,
  29. { statusCode: 200 },
  30. stubbedNotifications
  31. )
  32. const unreadNotifications =
  33. await this.handler.promises.getUserNotifications(userId)
  34. stubbedNotifications.should.deep.equal(unreadNotifications)
  35. const getOpts = {
  36. uri: `${notificationUrl}/user/${userId}`,
  37. json: true,
  38. timeout: 1000,
  39. method: 'GET',
  40. }
  41. this.request.calledWith(getOpts).should.equal(true)
  42. })
  43. it('should return empty arrays if there are no notifications', async function () {
  44. this.request.callsArgWith(1, null, { statusCode: 200 }, null)
  45. const unreadNotifications =
  46. await this.handler.promises.getUserNotifications(userId)
  47. unreadNotifications.length.should.equal(0)
  48. })
  49. })
  50. describe('markAsRead', function () {
  51. beforeEach(function () {
  52. this.key = 'some key here'
  53. })
  54. it('should send a delete request when a delete has been received to mark a notification', async function () {
  55. await this.handler.promises.markAsReadWithKey(userId, this.key)
  56. const opts = {
  57. uri: `${notificationUrl}/user/${userId}`,
  58. json: {
  59. key: this.key,
  60. },
  61. timeout: 1000,
  62. method: 'DELETE',
  63. }
  64. this.request.calledWith(opts).should.equal(true)
  65. })
  66. })
  67. describe('createNotification', function () {
  68. beforeEach(function () {
  69. this.key = 'some key here'
  70. this.messageOpts = { value: 12344 }
  71. this.templateKey = 'renderThisHtml'
  72. this.expiry = null
  73. })
  74. it('should post the message over', async function () {
  75. await this.handler.promises.createNotification(
  76. userId,
  77. this.key,
  78. this.templateKey,
  79. this.messageOpts,
  80. this.expiry
  81. )
  82. const args = this.request.args[0][0]
  83. args.uri.should.equal(`${notificationUrl}/user/${userId}`)
  84. args.timeout.should.equal(1000)
  85. const expectedJson = {
  86. key: this.key,
  87. templateKey: this.templateKey,
  88. messageOpts: this.messageOpts,
  89. forceCreate: true,
  90. }
  91. assert.deepEqual(args.json, expectedJson)
  92. })
  93. describe('when expiry date is supplied', function () {
  94. beforeEach(function () {
  95. this.key = 'some key here'
  96. this.messageOpts = { value: 12344 }
  97. this.templateKey = 'renderThisHtml'
  98. this.expiry = new Date()
  99. })
  100. it('should post the message over with expiry field', async function () {
  101. await this.handler.promises.createNotification(
  102. userId,
  103. this.key,
  104. this.templateKey,
  105. this.messageOpts,
  106. this.expiry
  107. )
  108. const args = this.request.args[0][0]
  109. args.uri.should.equal(`${notificationUrl}/user/${userId}`)
  110. args.timeout.should.equal(1000)
  111. const expectedJson = {
  112. key: this.key,
  113. templateKey: this.templateKey,
  114. messageOpts: this.messageOpts,
  115. expires: this.expiry,
  116. forceCreate: true,
  117. }
  118. assert.deepEqual(args.json, expectedJson)
  119. })
  120. })
  121. })
  122. describe('markAsReadByKeyOnly', function () {
  123. beforeEach(function () {
  124. this.key = 'some key here'
  125. })
  126. it('should send a delete request when a delete has been received to mark a notification', async function () {
  127. await this.handler.promises.markAsReadByKeyOnly(this.key)
  128. const opts = {
  129. uri: `${notificationUrl}/key/${this.key}`,
  130. timeout: 1000,
  131. method: 'DELETE',
  132. }
  133. this.request.calledWith(opts).should.equal(true)
  134. })
  135. })
  136. })