EmailSenderTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "../../infrastructure/Metrics": 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, =>
  41. args = @sesClient.sendMail.args[0][0]
  42. args.html.should.equal @opts.html
  43. args.to.should.equal @opts.to
  44. args.subject.should.equal @opts.subject
  45. done()
  46. it "should return the error", (done)->
  47. @sesClient.sendMail.callsArgWith(1, "error")
  48. @sender.sendEmail {}, (err)=>
  49. err.should.equal "error"
  50. done()
  51. it "should use the from address from settings", (done)->
  52. @sesClient.sendMail.callsArgWith(1)
  53. @sender.sendEmail @opts, =>
  54. args = @sesClient.sendMail.args[0][0]
  55. args.from.should.equal @settings.email.fromAddress
  56. done()
  57. it "should use the reply to address from settings", (done)->
  58. @sesClient.sendMail.callsArgWith(1)
  59. @sender.sendEmail @opts, =>
  60. args = @sesClient.sendMail.args[0][0]
  61. args.replyTo.should.equal @settings.email.replyToAddress
  62. done()
  63. it "should use the reply to address in options as an override", (done)->
  64. @sesClient.sendMail.callsArgWith(1)
  65. @opts.replyTo = "someone@else.com"
  66. @sender.sendEmail @opts, =>
  67. args = @sesClient.sendMail.args[0][0]
  68. args.replyTo.should.equal @opts.replyTo
  69. done()
  70. it "should not send an email when the rate limiter says no", (done)->
  71. @opts.sendingUser_id = "12321312321"
  72. @RateLimiter.addCount.callsArgWith(1, null, false)
  73. @sender.sendEmail @opts, =>
  74. @sesClient.sendMail.called.should.equal false
  75. done()
  76. it "should send the email when the rate limtier says continue", (done)->
  77. @sesClient.sendMail.callsArgWith(1)
  78. @opts.sendingUser_id = "12321312321"
  79. @RateLimiter.addCount.callsArgWith(1, null, true)
  80. @sender.sendEmail @opts, =>
  81. @sesClient.sendMail.called.should.equal true
  82. done()
  83. it "should not check the rate limiter when there is no sendingUser_id", (done)->
  84. @sesClient.sendMail.callsArgWith(1)
  85. @sender.sendEmail @opts, =>
  86. @sesClient.sendMail.called.should.equal true
  87. @RateLimiter.addCount.called.should.equal false
  88. done()
  89. describe 'with plain-text email content', () ->
  90. beforeEach ->
  91. @opts.text = "hello there"
  92. it "should set the text property on the email to send", (done)->
  93. @sesClient.sendMail.callsArgWith(1)
  94. @sender.sendEmail @opts, =>
  95. args = @sesClient.sendMail.args[0][0]
  96. args.html.should.equal @opts.html
  97. args.text.should.equal @opts.text
  98. args.to.should.equal @opts.to
  99. args.subject.should.equal @opts.subject
  100. done()