EmailSenderTests.coffee 2.0 KB

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