NotificationsBuilderTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 (done) {
  23. it('should create the notification', function (done) {
  24. this.controller
  25. .dropboxUnlinkedDueToLapsedReconfirmation(userId)
  26. .create(error => {
  27. expect(error).to.not.exist
  28. expect(this.handler.createNotification).to.have.been.calledWith(
  29. userId,
  30. 'drobox-unlinked-due-to-lapsed-reconfirmation',
  31. 'notification_dropbox_unlinked_due_to_lapsed_reconfirmation',
  32. {},
  33. null,
  34. true
  35. )
  36. done()
  37. })
  38. })
  39. describe('NotificationsHandler error', function () {
  40. let anError
  41. beforeEach(function () {
  42. anError = new Error('oops')
  43. this.handler.createNotification.yields(anError)
  44. })
  45. it('should return errors from NotificationsHandler', function (done) {
  46. this.controller
  47. .dropboxUnlinkedDueToLapsedReconfirmation(userId)
  48. .create(error => {
  49. expect(error).to.exist
  50. expect(error).to.deep.equal(anError)
  51. done()
  52. })
  53. })
  54. })
  55. })
  56. describe('groupInvitation', function (done) {
  57. const subscriptionId = '123123bcabca'
  58. beforeEach(function () {
  59. this.invite = {
  60. token: '123123abcabc',
  61. inviterName: 'Mr Overleaf',
  62. managedUsersEnabled: false,
  63. }
  64. })
  65. it('should create the notification', function (done) {
  66. this.controller
  67. .groupInvitation(
  68. userId,
  69. subscriptionId,
  70. this.invite.managedUsersEnabled
  71. )
  72. .create(this.invite, error => {
  73. expect(error).to.not.exist
  74. expect(this.handler.createNotification).to.have.been.calledWith(
  75. userId,
  76. `groupInvitation-${subscriptionId}-${userId}`,
  77. 'notification_group_invitation',
  78. {
  79. token: this.invite.token,
  80. inviterName: this.invite.inviterName,
  81. managedUsersEnabled: this.invite.managedUsersEnabled,
  82. },
  83. null,
  84. true
  85. )
  86. done()
  87. })
  88. })
  89. })
  90. describe('ipMatcherAffiliation', function () {
  91. describe('with portal and with SSO', function () {
  92. beforeEach(function () {
  93. this.body = {
  94. id: 1,
  95. name: 'stanford',
  96. enrolment_ad_html: 'v1 ad content',
  97. is_university: true,
  98. portal_slug: null,
  99. sso_enabled: false,
  100. }
  101. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  102. })
  103. it('should call v1 and create affiliation notifications', function (done) {
  104. const ip = '192.168.0.1'
  105. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  106. this.request.calledOnce.should.equal(true)
  107. const expectedOpts = {
  108. institutionId: this.body.id,
  109. university_name: this.body.name,
  110. content: this.body.enrolment_ad_html,
  111. ssoEnabled: false,
  112. portalPath: undefined,
  113. }
  114. this.handler.createNotification
  115. .calledWith(
  116. userId,
  117. `ip-matched-affiliation-${this.body.id}`,
  118. 'notification_ip_matched_affiliation',
  119. expectedOpts
  120. )
  121. .should.equal(true)
  122. done()
  123. })
  124. })
  125. })
  126. describe('without portal and without SSO', function () {
  127. beforeEach(function () {
  128. this.body = {
  129. id: 1,
  130. name: 'stanford',
  131. enrolment_ad_html: 'v1 ad content',
  132. is_university: true,
  133. portal_slug: 'stanford',
  134. sso_enabled: true,
  135. }
  136. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  137. })
  138. it('should call v1 and create affiliation notifications', function (done) {
  139. const ip = '192.168.0.1'
  140. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  141. this.request.calledOnce.should.equal(true)
  142. const expectedOpts = {
  143. institutionId: this.body.id,
  144. university_name: this.body.name,
  145. content: this.body.enrolment_ad_html,
  146. ssoEnabled: true,
  147. portalPath: '/edu/stanford',
  148. }
  149. this.handler.createNotification
  150. .calledWith(
  151. userId,
  152. `ip-matched-affiliation-${this.body.id}`,
  153. 'notification_ip_matched_affiliation',
  154. expectedOpts
  155. )
  156. .should.equal(true)
  157. done()
  158. })
  159. })
  160. })
  161. })
  162. })