EmailHandlerTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/Email/EmailHandler'
  21. )
  22. const { expect } = require('chai')
  23. describe('EmailHandler', function() {
  24. beforeEach(function() {
  25. this.settings = { email: {} }
  26. this.EmailBuilder = { buildEmail: sinon.stub() }
  27. this.EmailSender = { sendEmail: sinon.stub() }
  28. this.EmailHandler = SandboxedModule.require(modulePath, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. './EmailBuilder': this.EmailBuilder,
  34. './EmailSender': this.EmailSender,
  35. 'settings-sharelatex': this.settings,
  36. 'logger-sharelatex': {
  37. log() {}
  38. }
  39. }
  40. })
  41. return (this.html = '<html>hello</html>')
  42. })
  43. describe('send email', function() {
  44. it('should use the correct options', function(done) {
  45. this.EmailBuilder.buildEmail.returns({ html: this.html })
  46. this.EmailSender.sendEmail.callsArgWith(1)
  47. const opts = { to: 'bob@bob.com' }
  48. return this.EmailHandler.sendEmail('welcome', opts, () => {
  49. const args = this.EmailSender.sendEmail.args[0][0]
  50. args.html.should.equal(this.html)
  51. return done()
  52. })
  53. })
  54. it('should return the erroor', function(done) {
  55. this.EmailBuilder.buildEmail.returns({ html: this.html })
  56. this.EmailSender.sendEmail.callsArgWith(1, 'error')
  57. const opts = {
  58. to: 'bob@bob.com',
  59. subject: 'hello bob'
  60. }
  61. return this.EmailHandler.sendEmail('welcome', opts, err => {
  62. err.should.equal('error')
  63. return done()
  64. })
  65. })
  66. it('should not send an email if lifecycle is not enabled', function(done) {
  67. this.settings.email.lifecycle = false
  68. this.EmailBuilder.buildEmail.returns({ type: 'lifecycle' })
  69. return this.EmailHandler.sendEmail('welcome', {}, () => {
  70. this.EmailSender.sendEmail.called.should.equal(false)
  71. return done()
  72. })
  73. })
  74. it('should send an email if lifecycle is not enabled but the type is notification', function(done) {
  75. this.settings.email.lifecycle = false
  76. this.EmailBuilder.buildEmail.returns({ type: 'notification' })
  77. this.EmailSender.sendEmail.callsArgWith(1)
  78. const opts = { to: 'bob@bob.com' }
  79. return this.EmailHandler.sendEmail('welcome', opts, () => {
  80. this.EmailSender.sendEmail.called.should.equal(true)
  81. return done()
  82. })
  83. })
  84. it('should send lifecycle email if it is enabled', function(done) {
  85. this.settings.email.lifecycle = true
  86. this.EmailBuilder.buildEmail.returns({ type: 'lifecycle' })
  87. this.EmailSender.sendEmail.callsArgWith(1)
  88. const opts = { to: 'bob@bob.com' }
  89. return this.EmailHandler.sendEmail('welcome', opts, () => {
  90. this.EmailSender.sendEmail.called.should.equal(true)
  91. return done()
  92. })
  93. })
  94. describe('with plain-text email content', function() {
  95. beforeEach(function() {
  96. return (this.text = 'hello there')
  97. })
  98. it('should pass along the text field', function(done) {
  99. this.EmailBuilder.buildEmail.returns({
  100. html: this.html,
  101. text: this.text
  102. })
  103. this.EmailSender.sendEmail.callsArgWith(1)
  104. const opts = { to: 'bob@bob.com' }
  105. return this.EmailHandler.sendEmail('welcome', opts, () => {
  106. const args = this.EmailSender.sendEmail.args[0][0]
  107. args.html.should.equal(this.html)
  108. args.text.should.equal(this.text)
  109. return done()
  110. })
  111. })
  112. })
  113. })
  114. })