UserPostRegistrationAnalyticsManagerTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const SandboxedModule = require('sandboxed-module')
  2. const path = require('path')
  3. const sinon = require('sinon')
  4. const MODULE_PATH = path.join(
  5. __dirname,
  6. '../../../../app/src/Features/User/UserPostRegistrationAnalyticsManager'
  7. )
  8. describe('UserPostRegistrationAnalyticsManager', function () {
  9. beforeEach(function () {
  10. this.fakeUserId = '123abc'
  11. this.postRegistrationAnalyticsQueue = {
  12. add: sinon.stub().resolves(),
  13. process: callback => {
  14. this.queueProcessFunction = callback
  15. },
  16. }
  17. const self = this
  18. this.Queues = {
  19. getPostRegistrationAnalyticsQueue: () => {
  20. return self.postRegistrationAnalyticsQueue
  21. },
  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.UserPostRegistrationAnalyticsManager = SandboxedModule.require(
  37. MODULE_PATH,
  38. {
  39. globals: {
  40. console: console,
  41. },
  42. requires: {
  43. '../../infrastructure/Queues': this.Queues,
  44. './UserGetter': this.UserGetter,
  45. '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
  46. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  47. },
  48. }
  49. )
  50. })
  51. describe('schedule jobs', function () {
  52. it('should schedule delayed job on queue', function () {
  53. this.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  54. {
  55. _id: this.fakeUserId,
  56. }
  57. )
  58. sinon.assert.calledWithMatch(
  59. this.postRegistrationAnalyticsQueue.add,
  60. { userId: this.fakeUserId },
  61. { delay: 24 * 60 * 60 * 1000 }
  62. )
  63. })
  64. })
  65. describe('process jobs', function () {
  66. it('stops without errors if user is not found', async function () {
  67. this.UserGetter.promises.getUser.resolves(null)
  68. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  69. sinon.assert.calledWith(this.UserGetter.promises.getUser, {
  70. _id: this.fakeUserId,
  71. })
  72. sinon.assert.notCalled(this.InstitutionsAPI.promises.getUserAffiliations)
  73. sinon.assert.notCalled(this.AnalyticsManager.setUserProperty)
  74. })
  75. it('sets user property if user has commons account affiliationd', async function () {
  76. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  77. {},
  78. {
  79. institution: {
  80. commonsAccount: true,
  81. },
  82. },
  83. {
  84. institution: {
  85. commonsAccount: false,
  86. },
  87. },
  88. ])
  89. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  90. sinon.assert.calledWith(this.UserGetter.promises.getUser, {
  91. _id: this.fakeUserId,
  92. })
  93. sinon.assert.calledWith(
  94. this.InstitutionsAPI.promises.getUserAffiliations,
  95. this.fakeUserId
  96. )
  97. sinon.assert.calledWith(
  98. this.AnalyticsManager.setUserProperty,
  99. this.fakeUserId,
  100. 'registered-from-commons-account',
  101. true
  102. )
  103. })
  104. it('does not set user property if user has no commons account affiliation', async function () {
  105. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  106. {
  107. institution: {
  108. commonsAccount: false,
  109. },
  110. },
  111. ])
  112. await this.queueProcessFunction({ data: { userId: this.fakeUserId } })
  113. sinon.assert.notCalled(this.AnalyticsManager.setUserProperty)
  114. })
  115. })
  116. })