EmailSenderTests.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. should = require('chai').should()
  2. SandboxedModule = require('sandboxed-module')
  3. assert = require('assert')
  4. path = require('path')
  5. sinon = require('sinon')
  6. modulePath = path.join __dirname, "../../../../app/js/Features/Email/EmailSender.js"
  7. expect = require("chai").expect
  8. describe "EmailSender", ->
  9. beforeEach ->
  10. @RateLimiter =
  11. addCount:sinon.stub()
  12. @settings =
  13. email:
  14. transport: "ses"
  15. parameters:
  16. AWSAccessKeyID: "key"
  17. AWSSecretKey: "secret"
  18. fromAddress: "bob@bob.com"
  19. replyToAddress: "sally@gmail.com"
  20. @sesClient =
  21. sendMail: sinon.stub()
  22. @ses =
  23. createTransport: => @sesClient
  24. @sender = SandboxedModule.require modulePath, requires:
  25. 'nodemailer': @ses
  26. "settings-sharelatex":@settings
  27. '../../infrastructure/RateLimiter':@RateLimiter
  28. "logger-sharelatex":
  29. log:->
  30. warn:->
  31. err:->
  32. "metrics-sharelatex": inc:->
  33. @opts =
  34. to: "bob@bob.com"
  35. subject: "new email"
  36. html: "<hello></hello>"
  37. describe "sendEmail", ->
  38. it "should set the properties on the email to send", (done)->
  39. @sesClient.sendMail.callsArgWith(1)
  40. @sender.sendEmail @opts, (err) =>
  41. expect(err).to.not.exist
  42. args = @sesClient.sendMail.args[0][0]
  43. args.html.should.equal @opts.html
  44. args.to.should.equal @opts.to
  45. args.subject.should.equal @opts.subject
  46. done()
  47. it "should return a non-specific error", (done)->
  48. @sesClient.sendMail.callsArgWith(1, "error")
  49. @sender.sendEmail {}, (err)=>
  50. err.should.exist
  51. err.toString().should.equal 'Error: Cannot send email'
  52. done()
  53. it "should use the from address from settings", (done)->
  54. @sesClient.sendMail.callsArgWith(1)
  55. @sender.sendEmail @opts, =>
  56. args = @sesClient.sendMail.args[0][0]
  57. args.from.should.equal @settings.email.fromAddress
  58. done()
  59. it "should use the reply to address from settings", (done)->
  60. @sesClient.sendMail.callsArgWith(1)
  61. @sender.sendEmail @opts, =>
  62. args = @sesClient.sendMail.args[0][0]
  63. args.replyTo.should.equal @settings.email.replyToAddress
  64. done()
  65. it "should use the reply to address in options as an override", (done)->
  66. @sesClient.sendMail.callsArgWith(1)
  67. @opts.replyTo = "someone@else.com"
  68. @sender.sendEmail @opts, =>
  69. args = @sesClient.sendMail.args[0][0]
  70. args.replyTo.should.equal @opts.replyTo
  71. done()
  72. it "should not send an email when the rate limiter says no", (done)->
  73. @opts.sendingUser_id = "12321312321"
  74. @RateLimiter.addCount.callsArgWith(1, null, false)
  75. @sender.sendEmail @opts, =>
  76. @sesClient.sendMail.called.should.equal false
  77. done()
  78. it "should send the email when the rate limtier says continue", (done)->
  79. @sesClient.sendMail.callsArgWith(1)
  80. @opts.sendingUser_id = "12321312321"
  81. @RateLimiter.addCount.callsArgWith(1, null, true)
  82. @sender.sendEmail @opts, =>
  83. @sesClient.sendMail.called.should.equal true
  84. done()
  85. it "should not check the rate limiter when there is no sendingUser_id", (done)->
  86. @sesClient.sendMail.callsArgWith(1)
  87. @sender.sendEmail @opts, =>
  88. @sesClient.sendMail.called.should.equal true
  89. @RateLimiter.addCount.called.should.equal false
  90. done()
  91. describe 'with plain-text email content', () ->
  92. beforeEach ->
  93. @opts.text = "hello there"
  94. it "should set the text property on the email to send", (done)->
  95. @sesClient.sendMail.callsArgWith(1)
  96. @sender.sendEmail @opts, =>
  97. args = @sesClient.sendMail.args[0][0]
  98. args.html.should.equal @opts.html
  99. args.text.should.equal @opts.text
  100. args.to.should.equal @opts.to
  101. args.subject.should.equal @opts.subject
  102. done()