UserOnboardingEmailManagerTests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. },
  50. })
  51. })
  52. describe('scheduleOnboardingEmail', function () {
  53. it('should schedule delayed job on queue', async function () {
  54. await this.UserOnboardingEmailManager.scheduleOnboardingEmail({
  55. _id: this.fakeUserId,
  56. })
  57. sinon.assert.calledWith(
  58. this.Queues.createScheduledJob,
  59. 'emails-onboarding',
  60. { data: { userId: this.fakeUserId } },
  61. 24 * 60 * 60 * 1000
  62. )
  63. })
  64. })
  65. describe('sendOnboardingEmail', function () {
  66. it('should send onboarding email and update user', async function () {
  67. await this.UserOnboardingEmailManager.sendOnboardingEmail(this.fakeUserId)
  68. expect(this.EmailHandler.promises.sendEmail).to.have.been.calledWith(
  69. 'userOnboardingEmail',
  70. {
  71. to: this.fakeUserEmail,
  72. }
  73. )
  74. expect(this.UserUpdater.promises.updateUser).to.have.been.calledWith(
  75. this.fakeUserId,
  76. { $set: { onboardingEmailSentAt: sinon.match.date } }
  77. )
  78. })
  79. it('should stop if user is not found', async function () {
  80. await this.UserOnboardingEmailManager.sendOnboardingEmail({
  81. data: { userId: 'deleted-user' },
  82. })
  83. expect(this.EmailHandler.promises.sendEmail).not.to.have.been.called
  84. expect(this.UserUpdater.promises.updateUser).not.to.have.been.called
  85. })
  86. })
  87. })