NotificationsBuilderTests.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. enrolment_ad_html: 'v1 ad content',
  93. is_university: true,
  94. portal_slug: null,
  95. sso_enabled: false,
  96. }
  97. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  98. })
  99. it('should call v1 and create affiliation notifications', async function () {
  100. const ip = '192.168.0.1'
  101. await this.controller.promises.ipMatcherAffiliation(userId).create(ip)
  102. this.request.calledOnce.should.equal(true)
  103. const expectedOpts = {
  104. institutionId: this.body.id,
  105. university_name: this.body.name,
  106. content: this.body.enrolment_ad_html,
  107. ssoEnabled: false,
  108. portalPath: undefined,
  109. }
  110. this.handler.createNotification
  111. .calledWith(
  112. userId,
  113. `ip-matched-affiliation-${this.body.id}`,
  114. 'notification_ip_matched_affiliation',
  115. expectedOpts
  116. )
  117. .should.equal(true)
  118. })
  119. })
  120. describe('without portal and without SSO', function () {
  121. beforeEach(function () {
  122. this.body = {
  123. id: 1,
  124. name: 'stanford',
  125. enrolment_ad_html: 'v1 ad content',
  126. is_university: true,
  127. portal_slug: 'stanford',
  128. sso_enabled: true,
  129. }
  130. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  131. })
  132. it('should call v1 and create affiliation notifications', async function () {
  133. const ip = '192.168.0.1'
  134. await this.controller.promises.ipMatcherAffiliation(userId).create(ip)
  135. this.request.calledOnce.should.equal(true)
  136. const expectedOpts = {
  137. institutionId: this.body.id,
  138. university_name: this.body.name,
  139. content: this.body.enrolment_ad_html,
  140. ssoEnabled: true,
  141. portalPath: '/edu/stanford',
  142. }
  143. this.handler.createNotification
  144. .calledWith(
  145. userId,
  146. `ip-matched-affiliation-${this.body.id}`,
  147. 'notification_ip_matched_affiliation',
  148. expectedOpts
  149. )
  150. .should.equal(true)
  151. })
  152. })
  153. })
  154. })