UserOnboardingEmailManagerTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/UserOnboardingEmailManager'
  8. )
  9. describe('UserOnboardingEmailManager', function () {
  10. beforeEach(function () {
  11. this.fakeUserId = '123abc'
  12. this.fakeUserEmail = 'frog@overleaf.com'
  13. this.onboardingEmailsQueue = {
  14. add: sinon.stub().resolves(),
  15. process: callback => {
  16. this.queueProcessFunction = callback
  17. },
  18. }
  19. this.Queues = {
  20. createScheduledJob: sinon.stub().resolves(),
  21. }
  22. this.UserGetter = {
  23. promises: {
  24. getUser: sinon.stub().resolves(null),
  25. },
  26. }
  27. this.UserGetter.promises.getUser
  28. .withArgs({ _id: this.fakeUserId })
  29. .resolves({
  30. _id: this.fakeUserId,
  31. email: this.fakeUserEmail,
  32. })
  33. this.EmailHandler = {
  34. promises: {
  35. sendEmail: sinon.stub().resolves(),
  36. },
  37. }
  38. this.UserUpdater = {
  39. promises: {
  40. updateUser: sinon.stub().resolves(),
  41. },
  42. }
  43. this.UserOnboardingEmailManager = SandboxedModule.require(MODULE_PATH, {
  44. requires: {
  45. '../../infrastructure/Queues': this.Queues,
  46. '../Email/EmailHandler': this.EmailHandler,
  47. './UserGetter': this.UserGetter,
  48. './UserUpdater': this.UserUpdater,
  49. '@overleaf/settings': (this.Settings = {
  50. enableOnboardingEmails: true,
  51. }),
  52. },
  53. })
  54. })
  55. describe('scheduleOnboardingEmail', function () {
  56. it('should schedule delayed job on queue', async function () {
  57. await this.UserOnboardingEmailManager.scheduleOnboardingEmail({
  58. _id: this.fakeUserId,
  59. })
  60. sinon.assert.calledWith(
  61. this.Queues.createScheduledJob,
  62. 'emails-onboarding',
  63. { data: { userId: this.fakeUserId } },
  64. 24 * 60 * 60 * 1000
  65. )
  66. })
  67. })
  68. describe('sendOnboardingEmail', function () {
  69. describe('when onboarding emails are disabled', function () {
  70. beforeEach(function () {
  71. this.Settings.enableOnboardingEmails = false
  72. })
  73. it('should not send onboarding email', async function () {
  74. await this.UserOnboardingEmailManager.sendOnboardingEmail(
  75. this.fakeUserId
  76. )
  77. expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called
  78. expect(this.UserUpdater.promises.updateUser).not.to.have.been.called
  79. })
  80. })
  81. describe('when onboarding emails are enabled', function () {
  82. it('should send onboarding email and update user', async function () {
  83. await this.UserOnboardingEmailManager.sendOnboardingEmail(
  84. this.fakeUserId
  85. )
  86. expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith(
  87. 'userOnboardingEmail',
  88. {
  89. to: this.fakeUserEmail,
  90. }
  91. )
  92. expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith(
  93. this.fakeUserId,
  94. { $set: { onboardingEmailSentAt: sinon.match.date } }
  95. )
  96. })
  97. it('should stop if user is not found', async function () {
  98. await this.UserOnboardingEmailManager.sendOnboardingEmail({
  99. data: { userId: 'deleted-user' },
  100. })
  101. expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called
  102. expect(this.UserUpdater.promises.updateUser).not.to.have.been.called
  103. })
  104. })
  105. })
  106. })