EmailSenderTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/EmailSender.js'
  21. )
  22. const { expect } = require('chai')
  23. describe('EmailSender', function() {
  24. beforeEach(function() {
  25. this.RateLimiter = { addCount: sinon.stub() }
  26. this.settings = {
  27. email: {
  28. transport: 'ses',
  29. parameters: {
  30. AWSAccessKeyID: 'key',
  31. AWSSecretKey: 'secret'
  32. },
  33. fromAddress: 'bob@bob.com',
  34. replyToAddress: 'sally@gmail.com'
  35. }
  36. }
  37. this.sesClient = { sendMail: sinon.stub() }
  38. this.ses = { createTransport: () => this.sesClient }
  39. this.sender = SandboxedModule.require(modulePath, {
  40. requires: {
  41. nodemailer: this.ses,
  42. 'nodemailer-mandrill-transport': {},
  43. 'nodemailer-sendgrid-transport': {},
  44. 'settings-sharelatex': this.settings,
  45. '../../infrastructure/RateLimiter': this.RateLimiter,
  46. 'logger-sharelatex': {
  47. log() {},
  48. warn() {},
  49. err() {}
  50. },
  51. 'metrics-sharelatex': {
  52. inc() {}
  53. }
  54. }
  55. })
  56. return (this.opts = {
  57. to: 'bob@bob.com',
  58. subject: 'new email',
  59. html: '<hello></hello>'
  60. })
  61. })
  62. describe('sendEmail', function() {
  63. it('should set the properties on the email to send', function(done) {
  64. this.sesClient.sendMail.callsArgWith(1)
  65. return this.sender.sendEmail(this.opts, err => {
  66. expect(err).to.not.exist
  67. const args = this.sesClient.sendMail.args[0][0]
  68. args.html.should.equal(this.opts.html)
  69. args.to.should.equal(this.opts.to)
  70. args.subject.should.equal(this.opts.subject)
  71. return done()
  72. })
  73. })
  74. it('should return a non-specific error', function(done) {
  75. this.sesClient.sendMail.callsArgWith(1, 'error')
  76. return this.sender.sendEmail({}, err => {
  77. err.should.exist
  78. err.toString().should.equal('Error: Cannot send email')
  79. return done()
  80. })
  81. })
  82. it('should use the from address from settings', function(done) {
  83. this.sesClient.sendMail.callsArgWith(1)
  84. return this.sender.sendEmail(this.opts, () => {
  85. const args = this.sesClient.sendMail.args[0][0]
  86. args.from.should.equal(this.settings.email.fromAddress)
  87. return done()
  88. })
  89. })
  90. it('should use the reply to address from settings', function(done) {
  91. this.sesClient.sendMail.callsArgWith(1)
  92. return this.sender.sendEmail(this.opts, () => {
  93. const args = this.sesClient.sendMail.args[0][0]
  94. args.replyTo.should.equal(this.settings.email.replyToAddress)
  95. return done()
  96. })
  97. })
  98. it('should use the reply to address in options as an override', function(done) {
  99. this.sesClient.sendMail.callsArgWith(1)
  100. this.opts.replyTo = 'someone@else.com'
  101. return this.sender.sendEmail(this.opts, () => {
  102. const args = this.sesClient.sendMail.args[0][0]
  103. args.replyTo.should.equal(this.opts.replyTo)
  104. return done()
  105. })
  106. })
  107. it('should not send an email when the rate limiter says no', function(done) {
  108. this.opts.sendingUser_id = '12321312321'
  109. this.RateLimiter.addCount.callsArgWith(1, null, false)
  110. return this.sender.sendEmail(this.opts, () => {
  111. this.sesClient.sendMail.called.should.equal(false)
  112. return done()
  113. })
  114. })
  115. it('should send the email when the rate limtier says continue', function(done) {
  116. this.sesClient.sendMail.callsArgWith(1)
  117. this.opts.sendingUser_id = '12321312321'
  118. this.RateLimiter.addCount.callsArgWith(1, null, true)
  119. return this.sender.sendEmail(this.opts, () => {
  120. this.sesClient.sendMail.called.should.equal(true)
  121. return done()
  122. })
  123. })
  124. it('should not check the rate limiter when there is no sendingUser_id', function(done) {
  125. this.sesClient.sendMail.callsArgWith(1)
  126. return this.sender.sendEmail(this.opts, () => {
  127. this.sesClient.sendMail.called.should.equal(true)
  128. this.RateLimiter.addCount.called.should.equal(false)
  129. return done()
  130. })
  131. })
  132. describe('with plain-text email content', function() {
  133. beforeEach(function() {
  134. return (this.opts.text = 'hello there')
  135. })
  136. it('should set the text property on the email to send', function(done) {
  137. this.sesClient.sendMail.callsArgWith(1)
  138. return this.sender.sendEmail(this.opts, () => {
  139. const args = this.sesClient.sendMail.args[0][0]
  140. args.html.should.equal(this.opts.html)
  141. args.text.should.equal(this.opts.text)
  142. args.to.should.equal(this.opts.to)
  143. args.subject.should.equal(this.opts.subject)
  144. return done()
  145. })
  146. })
  147. })
  148. })
  149. })