EmailSenderTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "nodemailer-mandrill-transport":{}
  27. "nodemailer-sendgrid-transport":{}
  28. "settings-sharelatex":@settings
  29. '../../infrastructure/RateLimiter':@RateLimiter
  30. "logger-sharelatex":
  31. log:->
  32. warn:->
  33. err:->
  34. "metrics-sharelatex": inc:->
  35. @opts =
  36. to: "bob@bob.com"
  37. subject: "new email"
  38. html: "<hello></hello>"
  39. describe "sendEmail", ->
  40. it "should set the properties on the email to send", (done)->
  41. @sesClient.sendMail.callsArgWith(1)
  42. @sender.sendEmail @opts, (err) =>
  43. expect(err).to.not.exist
  44. args = @sesClient.sendMail.args[0][0]
  45. args.html.should.equal @opts.html
  46. args.to.should.equal @opts.to
  47. args.subject.should.equal @opts.subject
  48. done()
  49. it "should return a non-specific error", (done)->
  50. @sesClient.sendMail.callsArgWith(1, "error")
  51. @sender.sendEmail {}, (err)=>
  52. err.should.exist
  53. err.toString().should.equal 'Error: Cannot send email'
  54. done()
  55. it "should use the from address from settings", (done)->
  56. @sesClient.sendMail.callsArgWith(1)
  57. @sender.sendEmail @opts, =>
  58. args = @sesClient.sendMail.args[0][0]
  59. args.from.should.equal @settings.email.fromAddress
  60. done()
  61. it "should use the reply to address from settings", (done)->
  62. @sesClient.sendMail.callsArgWith(1)
  63. @sender.sendEmail @opts, =>
  64. args = @sesClient.sendMail.args[0][0]
  65. args.replyTo.should.equal @settings.email.replyToAddress
  66. done()
  67. it "should use the reply to address in options as an override", (done)->
  68. @sesClient.sendMail.callsArgWith(1)
  69. @opts.replyTo = "someone@else.com"
  70. @sender.sendEmail @opts, =>
  71. args = @sesClient.sendMail.args[0][0]
  72. args.replyTo.should.equal @opts.replyTo
  73. done()
  74. it "should not send an email when the rate limiter says no", (done)->
  75. @opts.sendingUser_id = "12321312321"
  76. @RateLimiter.addCount.callsArgWith(1, null, false)
  77. @sender.sendEmail @opts, =>
  78. @sesClient.sendMail.called.should.equal false
  79. done()
  80. it "should send the email when the rate limtier says continue", (done)->
  81. @sesClient.sendMail.callsArgWith(1)
  82. @opts.sendingUser_id = "12321312321"
  83. @RateLimiter.addCount.callsArgWith(1, null, true)
  84. @sender.sendEmail @opts, =>
  85. @sesClient.sendMail.called.should.equal true
  86. done()
  87. it "should not check the rate limiter when there is no sendingUser_id", (done)->
  88. @sesClient.sendMail.callsArgWith(1)
  89. @sender.sendEmail @opts, =>
  90. @sesClient.sendMail.called.should.equal true
  91. @RateLimiter.addCount.called.should.equal false
  92. done()
  93. describe 'with plain-text email content', () ->
  94. beforeEach ->
  95. @opts.text = "hello there"
  96. it "should set the text property on the email to send", (done)->
  97. @sesClient.sendMail.callsArgWith(1)
  98. @sender.sendEmail @opts, =>
  99. args = @sesClient.sendMail.args[0][0]
  100. args.html.should.equal @opts.html
  101. args.text.should.equal @opts.text
  102. args.to.should.equal @opts.to
  103. args.subject.should.equal @opts.subject
  104. done()