NotificationsBuilderTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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('ipMatcherAffiliation', function () {
  57. describe('with portal and with SSO', function () {
  58. beforeEach(function () {
  59. this.body = {
  60. id: 1,
  61. name: 'stanford',
  62. enrolment_ad_html: 'v1 ad content',
  63. is_university: true,
  64. portal_slug: null,
  65. sso_enabled: false,
  66. }
  67. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  68. })
  69. it('should call v1 and create affiliation notifications', function (done) {
  70. const ip = '192.168.0.1'
  71. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  72. this.request.calledOnce.should.equal(true)
  73. const expectedOpts = {
  74. institutionId: this.body.id,
  75. university_name: this.body.name,
  76. content: this.body.enrolment_ad_html,
  77. ssoEnabled: false,
  78. portalPath: undefined,
  79. }
  80. this.handler.createNotification
  81. .calledWith(
  82. userId,
  83. `ip-matched-affiliation-${this.body.id}`,
  84. 'notification_ip_matched_affiliation',
  85. expectedOpts
  86. )
  87. .should.equal(true)
  88. done()
  89. })
  90. })
  91. })
  92. describe('without portal and without SSO', function () {
  93. beforeEach(function () {
  94. this.body = {
  95. id: 1,
  96. name: 'stanford',
  97. enrolment_ad_html: 'v1 ad content',
  98. is_university: true,
  99. portal_slug: 'stanford',
  100. sso_enabled: true,
  101. }
  102. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  103. })
  104. it('should call v1 and create affiliation notifications', function (done) {
  105. const ip = '192.168.0.1'
  106. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  107. this.request.calledOnce.should.equal(true)
  108. const expectedOpts = {
  109. institutionId: this.body.id,
  110. university_name: this.body.name,
  111. content: this.body.enrolment_ad_html,
  112. ssoEnabled: true,
  113. portalPath: '/edu/stanford',
  114. }
  115. this.handler.createNotification
  116. .calledWith(
  117. userId,
  118. `ip-matched-affiliation-${this.body.id}`,
  119. 'notification_ip_matched_affiliation',
  120. expectedOpts
  121. )
  122. .should.equal(true)
  123. done()
  124. })
  125. })
  126. })
  127. })
  128. })