EmailSenderTests.coffee 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. @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. "../../infrastructure/Metrics": inc:->
  30. @opts =
  31. to: "bob@bob.com"
  32. subject: "new email"
  33. html: "<hello></hello>"
  34. describe "sendEmail", ->
  35. it "should set the properties on the email to send", (done)->
  36. @sesClient.sendMail.callsArgWith(1)
  37. @sender.sendEmail @opts, =>
  38. args = @sesClient.sendMail.args[0][0]
  39. args.html.should.equal @opts.html
  40. args.to.should.equal @opts.to
  41. args.subject.should.equal @opts.subject
  42. done()
  43. it "should return the error", (done)->
  44. @sesClient.sendMail.callsArgWith(1, "error")
  45. @sender.sendEmail {}, (err)=>
  46. err.should.equal "error"
  47. done()
  48. it "should use the from address from settings", (done)->
  49. @sesClient.sendMail.callsArgWith(1)
  50. @sender.sendEmail @opts, =>
  51. args = @sesClient.sendMail.args[0][0]
  52. args.from.should.equal @settings.email.fromAddress
  53. done()
  54. it "should use the reply to address from settings", (done)->
  55. @sesClient.sendMail.callsArgWith(1)
  56. @sender.sendEmail @opts, =>
  57. args = @sesClient.sendMail.args[0][0]
  58. args.replyTo.should.equal @settings.email.replyToAddress
  59. done()
  60. it "should use the reply to address in options as an override", (done)->
  61. @sesClient.sendMail.callsArgWith(1)
  62. @opts.replyTo = "someone@else.com"
  63. @sender.sendEmail @opts, =>
  64. args = @sesClient.sendMail.args[0][0]
  65. args.replyTo.should.equal @opts.replyTo
  66. done()