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. getOnboardingEmailsQueue: sinon
  21. .stub()
  22. .returns(this.onboardingEmailsQueue),
  23. }
  24. this.UserGetter = {
  25. promises: {
  26. getUser: sinon.stub().resolves(null),
  27. },
  28. }
  29. this.UserGetter.promises.getUser
  30. .withArgs({ _id: this.fakeUserId })
  31. .resolves({
  32. _id: this.fakeUserId,
  33. email: this.fakeUserEmail,
  34. })
  35. this.EmailHandler = {
  36. promises: {
  37. sendEmail: sinon.stub().resolves(),
  38. },
  39. }
  40. this.UserUpdater = {
  41. promises: {
  42. updateUser: sinon.stub().resolves(),
  43. },
  44. }
  45. this.UserOnboardingEmailManager = SandboxedModule.require(MODULE_PATH, {
  46. requires: {
  47. '../../infrastructure/Queues': this.Queues,
  48. '../Email/EmailHandler': this.EmailHandler,
  49. './UserGetter': this.UserGetter,
  50. './UserUpdater': this.UserUpdater,
  51. },
  52. })
  53. })
  54. describe('scheduleOnboardingEmail', function () {
  55. it('should schedule delayed job on queue', async function () {
  56. await this.UserOnboardingEmailManager.scheduleOnboardingEmail({
  57. _id: this.fakeUserId,
  58. })
  59. expect(this.onboardingEmailsQueue.add).to.have.been.calledWith(
  60. { userId: this.fakeUserId },
  61. { delay: 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. })