EmailSenderTests.coffee 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "Email", ->
  9. beforeEach ->
  10. @settings =
  11. email:
  12. transport: "ses"
  13. parameters:
  14. AWSAccessKeyID: "key"
  15. AWSSecretKey: "secret"
  16. fromAddress: "bob@bob.com"
  17. replyToAddress: "sally@gmail.com"
  18. @sesClient =
  19. sendMail: sinon.stub()
  20. @ses =
  21. createTransport: => @sesClient
  22. @sender = SandboxedModule.require modulePath, requires:
  23. 'nodemailer': @ses
  24. "settings-sharelatex":@settings
  25. "logger-sharelatex":
  26. log:->
  27. warn:->
  28. err:->
  29. @opts =
  30. to: "bob@bob.com"
  31. subject: "new email"
  32. html: "<hello></hello>"
  33. describe "sendEmail", ->
  34. it "should set the properties on the email to send", (done)->
  35. @sesClient.sendMail.callsArgWith(1)
  36. @sender.sendEmail @opts, =>
  37. args = @sesClient.sendMail.args[0][0]
  38. args.html.should.equal @opts.html
  39. args.to.should.equal @opts.to
  40. args.subject.should.equal @opts.subject
  41. done()
  42. it "should return the error", (done)->
  43. @sesClient.sendMail.callsArgWith(1, "error")
  44. @sender.sendEmail {}, (err)=>
  45. err.should.equal "error"
  46. done()
  47. it "should use the from address from settings", (done)->
  48. @sesClient.sendMail.callsArgWith(1)
  49. @sender.sendEmail @opts, =>
  50. args = @sesClient.sendMail.args[0][0]
  51. args.from.should.equal @settings.email.fromAddress
  52. done()
  53. it "should use the reply to address from settings", (done)->
  54. @sesClient.sendMail.callsArgWith(1)
  55. @sender.sendEmail @opts, =>
  56. args = @sesClient.sendMail.args[0][0]
  57. args.replyTo.should.equal @settings.email.replyToAddress
  58. done()
  59. it "should use the reply to address in options as an override", (done)->
  60. @sesClient.sendMail.callsArgWith(1)
  61. @opts.replyTo = "someone@else.com"
  62. @sender.sendEmail @opts, =>
  63. args = @sesClient.sendMail.args[0][0]
  64. args.replyTo.should.equal @opts.replyTo
  65. done()