EmailHandlerTests.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/Email/EmailHandler'
  8. )
  9. describe('EmailHandler', function() {
  10. beforeEach(function() {
  11. this.html = '<html>hello</html>'
  12. this.Settings = { email: {} }
  13. this.EmailBuilder = {
  14. buildEmail: sinon.stub().returns({ html: this.html })
  15. }
  16. this.EmailSender = {
  17. promises: {
  18. sendEmail: sinon.stub().resolves()
  19. }
  20. }
  21. this.EmailHandler = SandboxedModule.require(MODULE_PATH, {
  22. requires: {
  23. './EmailBuilder': this.EmailBuilder,
  24. './EmailSender': this.EmailSender,
  25. 'settings-sharelatex': this.Settings
  26. }
  27. })
  28. })
  29. describe('send email', function() {
  30. it('should use the correct options', async function() {
  31. const opts = { to: 'bob@bob.com' }
  32. await this.EmailHandler.promises.sendEmail('welcome', opts)
  33. expect(this.EmailSender.promises.sendEmail).to.have.been.calledWithMatch({
  34. html: this.html
  35. })
  36. })
  37. it('should return the error', async function() {
  38. this.EmailSender.promises.sendEmail.rejects(new Error('boom'))
  39. const opts = {
  40. to: 'bob@bob.com',
  41. subject: 'hello bob'
  42. }
  43. await expect(this.EmailHandler.promises.sendEmail('welcome', opts)).to.be
  44. .rejected
  45. })
  46. it('should not send an email if lifecycle is not enabled', async function() {
  47. this.Settings.email.lifecycle = false
  48. this.EmailBuilder.buildEmail.returns({ type: 'lifecycle' })
  49. await this.EmailHandler.promises.sendEmail('welcome', {})
  50. expect(this.EmailSender.promises.sendEmail).not.to.have.been.called
  51. })
  52. it('should send an email if lifecycle is not enabled but the type is notification', async function() {
  53. this.Settings.email.lifecycle = false
  54. this.EmailBuilder.buildEmail.returns({ type: 'notification' })
  55. const opts = { to: 'bob@bob.com' }
  56. await this.EmailHandler.promises.sendEmail('welcome', opts)
  57. expect(this.EmailSender.promises.sendEmail).to.have.been.called
  58. })
  59. it('should send lifecycle email if it is enabled', async function() {
  60. this.Settings.email.lifecycle = true
  61. this.EmailBuilder.buildEmail.returns({ type: 'lifecycle' })
  62. const opts = { to: 'bob@bob.com' }
  63. await this.EmailHandler.promises.sendEmail('welcome', opts)
  64. expect(this.EmailSender.promises.sendEmail).to.have.been.called
  65. })
  66. describe('with plain-text email content', function() {
  67. beforeEach(function() {
  68. this.text = 'hello there'
  69. })
  70. it('should pass along the text field', async function() {
  71. this.EmailBuilder.buildEmail.returns({
  72. html: this.html,
  73. text: this.text
  74. })
  75. const opts = { to: 'bob@bob.com' }
  76. await this.EmailHandler.promises.sendEmail('welcome', opts)
  77. expect(
  78. this.EmailSender.promises.sendEmail
  79. ).to.have.been.calledWithMatch({
  80. html: this.html,
  81. text: this.text
  82. })
  83. })
  84. })
  85. })
  86. })