NotificationsBuilderTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const SandboxedModule = require('sandboxed-module')
  2. const { expect } = require('chai')
  3. const sinon = require('sinon')
  4. const modulePath = require('path').join(
  5. __dirname,
  6. '../../../../app/src/Features/Notifications/NotificationsBuilder.js'
  7. )
  8. describe('NotificationsBuilder', function () {
  9. const userId = '123nd3ijdks'
  10. beforeEach(function () {
  11. this.handler = { createNotification: sinon.stub().callsArgWith(6) }
  12. this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
  13. this.request = sinon.stub()
  14. this.controller = SandboxedModule.require(modulePath, {
  15. requires: {
  16. './NotificationsHandler': this.handler,
  17. '@overleaf/settings': this.settings,
  18. request: this.request,
  19. },
  20. })
  21. })
  22. describe('dropboxUnlinkedDueToLapsedReconfirmation', function () {
  23. it('should create the notification', async function () {
  24. await this.controller.promises
  25. .dropboxUnlinkedDueToLapsedReconfirmation(userId)
  26. .create()
  27. expect(this.handler.createNotification).to.have.been.calledWith(
  28. userId,
  29. 'drobox-unlinked-due-to-lapsed-reconfirmation',
  30. 'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
  31. {},
  32. null,
  33. true
  34. )
  35. })
  36. describe('NotificationsHandler error', function () {
  37. let anError
  38. beforeEach(function () {
  39. anError = new Error('oops')
  40. this.handler.createNotification.yields(anError)
  41. })
  42. it('should return errors from NotificationsHandler', async function () {
  43. let error
  44. try {
  45. await this.controller.promises
  46. .dropboxUnlinkedDueToLapsedReconfirmation(userId)
  47. .create()
  48. } catch (err) {
  49. error = err
  50. }
  51. expect(error).to.equal(anError)
  52. })
  53. })
  54. })
  55. describe('groupInvitation', function () {
  56. const subscriptionId = '123123bcabca'
  57. beforeEach(function () {
  58. this.invite = {
  59. token: '123123abcabc',
  60. inviterName: 'Mr Overleaf',
  61. managedUsersEnabled: false,
  62. }
  63. })
  64. it('should create the notification', async function () {
  65. await this.controller.promises
  66. .groupInvitation(
  67. userId,
  68. subscriptionId,
  69. this.invite.managedUsersEnabled
  70. )
  71. .create(this.invite)
  72. expect(this.handler.createNotification).to.have.been.calledWith(
  73. userId,
  74. `groupInvitation-${subscriptionId}-${userId}`,
  75. 'notification_group_invitation',
  76. {
  77. token: this.invite.token,
  78. inviterName: this.invite.inviterName,
  79. managedUsersEnabled: this.invite.managedUsersEnabled,
  80. },
  81. null,
  82. true
  83. )
  84. })
  85. })
  86. describe('ipMatcherAffiliation', function () {
  87. describe('with portal and with SSO', function () {
  88. beforeEach(function () {
  89. this.body = {
  90. id: 1,
  91. name: 'stanford',
  92. is_university: true,
  93. portal_slug: null,
  94. sso_enabled: false,
  95. }
  96. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  97. })
  98. it('should call v1 and create affiliation notifications', async function () {
  99. const ip = '192.168.0.1'
  100. await this.controller.promises.ipMatcherAffiliation(userId).create(ip)
  101. this.request.calledOnce.should.equal(true)
  102. const expectedOpts = {
  103. institutionId: this.body.id,
  104. university_name: this.body.name,
  105. ssoEnabled: false,
  106. portalPath: undefined,
  107. }
  108. this.handler.createNotification
  109. .calledWith(
  110. userId,
  111. `ip-matched-affiliation-${this.body.id}`,
  112. 'notification_ip_matched_affiliation',
  113. expectedOpts
  114. )
  115. .should.equal(true)
  116. })
  117. })
  118. describe('without portal and without SSO', function () {
  119. beforeEach(function () {
  120. this.body = {
  121. id: 1,
  122. name: 'stanford',
  123. is_university: true,
  124. portal_slug: 'stanford',
  125. sso_enabled: true,
  126. }
  127. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  128. })
  129. it('should call v1 and create affiliation notifications', async function () {
  130. const ip = '192.168.0.1'
  131. await this.controller.promises.ipMatcherAffiliation(userId).create(ip)
  132. this.request.calledOnce.should.equal(true)
  133. const expectedOpts = {
  134. institutionId: this.body.id,
  135. university_name: this.body.name,
  136. ssoEnabled: true,
  137. portalPath: '/edu/stanford',
  138. }
  139. this.handler.createNotification
  140. .calledWith(
  141. userId,
  142. `ip-matched-affiliation-${this.body.id}`,
  143. 'notification_ip_matched_affiliation',
  144. expectedOpts
  145. )
  146. .should.equal(true)
  147. })
  148. })
  149. })
  150. })