NotificationsHandlerTests.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const { assert } = require('chai')
  16. const sinon = require('sinon')
  17. const modulePath = require('path').join(
  18. __dirname,
  19. '../../../../app/src/Features/Notifications/NotificationsHandler.js'
  20. )
  21. const _ = require('lodash')
  22. describe('NotificationsHandler', function () {
  23. const userId = '123nd3ijdks'
  24. const notificationId = '123njdskj9jlk'
  25. const notificationUrl = 'notification.overleaf.testing'
  26. beforeEach(function () {
  27. this.request = sinon.stub().callsArgWith(1)
  28. return (this.handler = SandboxedModule.require(modulePath, {
  29. requires: {
  30. '@overleaf/settings': {
  31. apis: { notifications: { url: notificationUrl } },
  32. },
  33. request: this.request,
  34. },
  35. }))
  36. })
  37. describe('getUserNotifications', function () {
  38. it('should get unread notifications', function (done) {
  39. const stubbedNotifications = [{ _id: notificationId, user_id: userId }]
  40. this.request.callsArgWith(
  41. 1,
  42. null,
  43. { statusCode: 200 },
  44. stubbedNotifications
  45. )
  46. return this.handler.getUserNotifications(
  47. userId,
  48. (err, unreadNotifications) => {
  49. stubbedNotifications.should.deep.equal(unreadNotifications)
  50. const getOpts = {
  51. uri: `${notificationUrl}/user/${userId}`,
  52. json: true,
  53. timeout: 1000,
  54. method: 'GET',
  55. }
  56. this.request.calledWith(getOpts).should.equal(true)
  57. return done()
  58. }
  59. )
  60. })
  61. it('should return empty arrays if there are no notifications', function () {
  62. this.request.callsArgWith(1, null, { statusCode: 200 }, null)
  63. return this.handler.getUserNotifications(
  64. userId,
  65. (err, unreadNotifications) => {
  66. return unreadNotifications.length.should.equal(0)
  67. }
  68. )
  69. })
  70. })
  71. describe('markAsRead', function () {
  72. beforeEach(function () {
  73. return (this.key = 'some key here')
  74. })
  75. it('should send a delete request when a delete has been received to mark a notification', function (done) {
  76. return this.handler.markAsReadWithKey(userId, this.key, () => {
  77. const opts = {
  78. uri: `${notificationUrl}/user/${userId}`,
  79. json: {
  80. key: this.key,
  81. },
  82. timeout: 1000,
  83. method: 'DELETE',
  84. }
  85. this.request.calledWith(opts).should.equal(true)
  86. return done()
  87. })
  88. })
  89. })
  90. describe('createNotification', function () {
  91. beforeEach(function () {
  92. this.key = 'some key here'
  93. this.messageOpts = { value: 12344 }
  94. this.templateKey = 'renderThisHtml'
  95. return (this.expiry = null)
  96. })
  97. it('should post the message over', function (done) {
  98. return this.handler.createNotification(
  99. userId,
  100. this.key,
  101. this.templateKey,
  102. this.messageOpts,
  103. this.expiry,
  104. () => {
  105. const args = this.request.args[0][0]
  106. args.uri.should.equal(`${notificationUrl}/user/${userId}`)
  107. args.timeout.should.equal(1000)
  108. const expectedJson = {
  109. key: this.key,
  110. templateKey: this.templateKey,
  111. messageOpts: this.messageOpts,
  112. forceCreate: true,
  113. }
  114. assert.deepEqual(args.json, expectedJson)
  115. return done()
  116. }
  117. )
  118. })
  119. describe('when expiry date is supplied', function () {
  120. beforeEach(function () {
  121. this.key = 'some key here'
  122. this.messageOpts = { value: 12344 }
  123. this.templateKey = 'renderThisHtml'
  124. return (this.expiry = new Date())
  125. })
  126. it('should post the message over with expiry field', function (done) {
  127. return this.handler.createNotification(
  128. userId,
  129. this.key,
  130. this.templateKey,
  131. this.messageOpts,
  132. this.expiry,
  133. () => {
  134. const args = this.request.args[0][0]
  135. args.uri.should.equal(`${notificationUrl}/user/${userId}`)
  136. args.timeout.should.equal(1000)
  137. const expectedJson = {
  138. key: this.key,
  139. templateKey: this.templateKey,
  140. messageOpts: this.messageOpts,
  141. expires: this.expiry,
  142. forceCreate: true,
  143. }
  144. assert.deepEqual(args.json, expectedJson)
  145. return done()
  146. }
  147. )
  148. })
  149. })
  150. })
  151. describe('markAsReadByKeyOnly', function () {
  152. beforeEach(function () {
  153. return (this.key = 'some key here')
  154. })
  155. it('should send a delete request when a delete has been received to mark a notification', function (done) {
  156. return this.handler.markAsReadByKeyOnly(this.key, () => {
  157. const opts = {
  158. uri: `${notificationUrl}/key/${this.key}`,
  159. timeout: 1000,
  160. method: 'DELETE',
  161. }
  162. this.request.calledWith(opts).should.equal(true)
  163. return done()
  164. })
  165. })
  166. })
  167. })