UserPostRegistrationAnalyticsManagerTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Queues = {
  13. createScheduledJob: sinon.stub().resolves(),
  14. }
  15. this.UserGetter = {
  16. promises: {
  17. getUser: sinon.stub().resolves(),
  18. },
  19. }
  20. this.UserGetter.promises.getUser
  21. .withArgs({ _id: this.fakeUserId })
  22. .resolves({ _id: this.fakeUserId })
  23. this.InstitutionsAPI = {
  24. promises: {
  25. getUserAffiliations: sinon.stub().resolves([]),
  26. },
  27. }
  28. this.AnalyticsManager = {
  29. setUserPropertyForUser: sinon.stub().resolves(),
  30. }
  31. this.UserPostRegistrationAnalyticsManager = SandboxedModule.require(
  32. MODULE_PATH,
  33. {
  34. requires: {
  35. '../../infrastructure/Queues': this.Queues,
  36. './UserGetter': this.UserGetter,
  37. '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
  38. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  39. },
  40. }
  41. )
  42. })
  43. describe('schedulePostRegistrationAnalytics', function () {
  44. it('should schedule delayed job on queue', async function () {
  45. await this.UserPostRegistrationAnalyticsManager.schedulePostRegistrationAnalytics(
  46. {
  47. _id: this.fakeUserId,
  48. }
  49. )
  50. sinon.assert.calledWith(
  51. this.Queues.createScheduledJob,
  52. 'post-registration-analytics',
  53. { data: { userId: this.fakeUserId } },
  54. 24 * 60 * 60 * 1000
  55. )
  56. })
  57. })
  58. describe('postRegistrationAnalytics', function () {
  59. it('stops without errors if user is not found', async function () {
  60. await this.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  61. 'not-a-user'
  62. )
  63. expect(this.InstitutionsAPI.promises.getUserAffiliations).not.to.have.been
  64. .called
  65. expect(this.AnalyticsManager.setUserPropertyForUser).not.to.have.been
  66. .called
  67. })
  68. it('sets user property if user has commons account affiliationd', async function () {
  69. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  70. {},
  71. {
  72. institution: {
  73. commonsAccount: true,
  74. },
  75. },
  76. {
  77. institution: {
  78. commonsAccount: false,
  79. },
  80. },
  81. ])
  82. await this.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  83. this.fakeUserId
  84. )
  85. expect(
  86. this.AnalyticsManager.setUserPropertyForUser
  87. ).to.have.been.calledWith(
  88. this.fakeUserId,
  89. 'registered-from-commons-account',
  90. true
  91. )
  92. })
  93. it('does not set user property if user has no commons account affiliation', async function () {
  94. this.InstitutionsAPI.promises.getUserAffiliations.resolves([
  95. {
  96. institution: {
  97. commonsAccount: false,
  98. },
  99. },
  100. ])
  101. await this.UserPostRegistrationAnalyticsManager.postRegistrationAnalytics(
  102. this.fakeUserId
  103. )
  104. expect(this.AnalyticsManager.setUserPropertyForUser).not.to.have.been
  105. .called
  106. })
  107. })
  108. })