NotificationsHandlerTests.js 5.3 KB

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