UserPostRegistrationAnalyticsManagerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const MODULE_PATH = path.join(
  6. __dirname,
  7. '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager'
  8. )
  9. describe('UserPostRegistrationAnalyticsManager', function () {
  10. beforeEach(function () {
  11. this.fakeUserId = '123abc'
  12. this.postRegistrationAnalyticsQueue = {
  13. add: sinon.stub().resolves(),
  14. process: callback => {
  15. this.queueProcessFunction = callback
  16. },
  17. }
  18. this.Queues = {
  19. getPostRegistrationAnalyticsQueue: sinon
  20. .stub()
  21. .returns(this.postRegistrationAnalyticsQueue),
  22. }
  23. this.UserGetter = {
  24. promises: {
  25. getUser: sinon.stub().resolves({ _id: this.fakeUserId }),
  26. },
  27. }
  28. this.InstitutionsAPI = {
  29. promises: {
  30. getUserAffiliations: sinon.stub().resolves([]),
  31. },
  32. }
  33. this.AnalyticsManager = {
  34. setUserProperty: sinon.stub().resolves(),
  35. }
  36. this.Features = {
  37. hasFeature: sinon.stub().returns(true),
  38. }
  39. this.init = isSAAS => {
  40. this.Features.hasFeature.withArgs('saas').returns(isSAAS)
  41. this.UserPostRegistrationAnalyticsManager = SandboxedModule.require(
  42. MODULE_PATH,
  43. {
  44. requires: {
  45. '../../infrastructure/Features': this.Features,
  46. '../../infrastructure/Queues': this.Queues,
  47. './UserGetter': this.UserGetter,
  48. '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
  49. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  50. },
  51. }
  52. )
  53. }
  54. })
  55. describe('in Server CE/Pro', function () {
  56. beforeEach(function () {
  57. this.init(false)
  58. })
  59. it('should schedule delayed job on queue', function () {
  60. this.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  61. { _id: this.fakeUserId }
  62. )
  63. expect(this.Queues.getPostRegistrationAnalyticsQueue).to.not.have.been
  64. .called
  65. expect(this.postRegistrationAnalyticsQueue.add).to.not.have.been.called
  66. })
  67. })
  68. describe('in SAAS', function () {
  69. beforeEach(function () {
  70. this.init(true)
  71. })
  72. describe('schedule jobs in SAAS', function () {
  73. it('should schedule delayed job on queue', function () {
  74. this.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  75. {
  76. _id: this.fakeUserId,
  77. }
  78. )
  79. sinon.assert.calledWithMatch(
  80. this.postRegistrationAnalyticsQueue.add,
  81. { userId: this.fakeUserId },
  82. { delay: 24 * 60 * 60 * 1000 }
  83. )
  84. })
  85. })
  86. describe('process jobs', function () {
  87. it('stops without errors if user is not found', async function () {
  88. this.UserGetter.promises.getUser.resolves(null)
  89. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  90. sinon.assert.calledWith(this.UserGetter.promises.getUser, {
  91. _id: this.fakeUserId,
  92. })
  93. sinon.assert.notCalled(
  94. this.InstitutionsAPI.promises.getUserAffiliations
  95. )
  96. sinon.assert.notCalled(this.AnalyticsManager.setUserProperty)
  97. })
  98. it('sets user property if user has commons account affiliationd', async function () {
  99. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  100. {},
  101. {
  102. institution: {
  103. commonsAccount: true,
  104. },
  105. },
  106. {
  107. institution: {
  108. commonsAccount: false,
  109. },
  110. },
  111. ])
  112. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  113. sinon.assert.calledWith(this.UserGetter.promises.getUser, {
  114. _id: this.fakeUserId,
  115. })
  116. sinon.assert.calledWith(
  117. this.InstitutionsAPI.promises.getUserAffiliations,
  118. this.fakeUserId
  119. )
  120. sinon.assert.calledWith(
  121. this.AnalyticsManager.setUserProperty,
  122. this.fakeUserId,
  123. 'registered-from-commons-account',
  124. true
  125. )
  126. })
  127. it('does not set user property if user has no commons account affiliation', async function () {
  128. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  129. {
  130. institution: {
  131. commonsAccount: false,
  132. },
  133. },
  134. ])
  135. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  136. sinon.assert.notCalled(this.AnalyticsManager.setUserProperty)
  137. })
  138. })
  139. })
  140. })