NotificationsBuilderTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const SandboxedModule = require('sandboxed-module')
  2. require('chai').should()
  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. describe('ipMatcherAffiliation', function() {
  11. beforeEach(function() {
  12. this.handler = { createNotification: sinon.stub().callsArgWith(6) }
  13. this.settings = { apis: { v1: { url: 'v1.url', user: '', pass: '' } } }
  14. this.request = sinon.stub()
  15. this.controller = SandboxedModule.require(modulePath, {
  16. globals: {
  17. console: console
  18. },
  19. requires: {
  20. './NotificationsHandler': this.handler,
  21. 'settings-sharelatex': this.settings,
  22. request: this.request,
  23. 'logger-sharelatex': {
  24. log() {},
  25. err() {}
  26. }
  27. }
  28. })
  29. })
  30. describe('with portal and with SSO', function() {
  31. beforeEach(function() {
  32. this.body = {
  33. id: 1,
  34. name: 'stanford',
  35. enrolment_ad_html: 'v1 ad content',
  36. is_university: true,
  37. portal_slug: null,
  38. sso_enabled: false
  39. }
  40. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  41. })
  42. it('should call v1 and create affiliation notifications', function(done) {
  43. const ip = '192.168.0.1'
  44. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  45. this.request.calledOnce.should.equal(true)
  46. const expectedOpts = {
  47. institutionId: this.body.id,
  48. university_name: this.body.name,
  49. content: this.body.enrolment_ad_html,
  50. ssoEnabled: false,
  51. portalPath: undefined
  52. }
  53. this.handler.createNotification
  54. .calledWith(
  55. userId,
  56. `ip-matched-affiliation-${this.body.id}`,
  57. 'notification_ip_matched_affiliation',
  58. expectedOpts
  59. )
  60. .should.equal(true)
  61. done()
  62. })
  63. })
  64. })
  65. describe('without portal and without SSO', function() {
  66. beforeEach(function() {
  67. this.body = {
  68. id: 1,
  69. name: 'stanford',
  70. enrolment_ad_html: 'v1 ad content',
  71. is_university: true,
  72. portal_slug: 'stanford',
  73. sso_enabled: true
  74. }
  75. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  76. })
  77. it('should call v1 and create affiliation notifications', function(done) {
  78. const ip = '192.168.0.1'
  79. this.controller.ipMatcherAffiliation(userId).create(ip, callback => {
  80. this.request.calledOnce.should.equal(true)
  81. const expectedOpts = {
  82. institutionId: this.body.id,
  83. university_name: this.body.name,
  84. content: this.body.enrolment_ad_html,
  85. ssoEnabled: true,
  86. portalPath: '/edu/stanford'
  87. }
  88. this.handler.createNotification
  89. .calledWith(
  90. userId,
  91. `ip-matched-affiliation-${this.body.id}`,
  92. 'notification_ip_matched_affiliation',
  93. expectedOpts
  94. )
  95. .should.equal(true)
  96. done()
  97. })
  98. })
  99. })
  100. })
  101. })