UserEmailsConfirmationHandlerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/User/UserEmailsConfirmationHandler"
  7. expect = require("chai").expect
  8. Errors = require "../../../../app/js/Features/Errors/Errors"
  9. EmailHelper = require "../../../../app/js/Features/Helpers/EmailHelper"
  10. describe "UserEmailsConfirmationHandler", ->
  11. beforeEach ->
  12. @UserEmailsConfirmationHandler = SandboxedModule.require modulePath, requires:
  13. "settings-sharelatex": @settings =
  14. siteUrl: "emails.example.com"
  15. "logger-sharelatex": @logger = { log: sinon.stub() }
  16. "../Security/OneTimeTokenHandler": @OneTimeTokenHandler = {}
  17. "../Errors/Errors": Errors
  18. "./UserUpdater": @UserUpdater = {}
  19. "../Email/EmailHandler": @EmailHandler = {}
  20. "../Helpers/EmailHelper": EmailHelper
  21. @user_id = "mock-user-id"
  22. @email = "mock@example.com"
  23. @callback = sinon.stub()
  24. describe "sendConfirmationEmail", ->
  25. beforeEach ->
  26. @OneTimeTokenHandler.getNewToken = sinon.stub().yields(null, @token = "new-token")
  27. @EmailHandler.sendEmail = sinon.stub().yields()
  28. describe 'successfully', ->
  29. beforeEach ->
  30. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, @callback
  31. it "should generate a token for the user which references their id and email", ->
  32. @OneTimeTokenHandler.getNewToken
  33. .calledWith(
  34. 'email_confirmation',
  35. {@user_id, @email},
  36. { expiresIn: 365 * 24 * 60 * 60 }
  37. )
  38. .should.equal true
  39. it 'should send an email to the user', ->
  40. @EmailHandler.sendEmail
  41. .calledWith('confirmEmail', {
  42. to: @email,
  43. confirmEmailUrl: 'emails.example.com/user/emails/confirm?token=new-token'
  44. sendingUser_id: @user_id
  45. })
  46. .should.equal true
  47. it 'should call the callback', ->
  48. @callback.called.should.equal true
  49. describe 'with invalid email', ->
  50. beforeEach ->
  51. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, '!"£$%^&*()', @callback
  52. it 'should return an error', ->
  53. @callback.calledWith(sinon.match.instanceOf(Error)).should.equal true
  54. describe 'a custom template', ->
  55. beforeEach ->
  56. @UserEmailsConfirmationHandler.sendConfirmationEmail @user_id, @email, 'myCustomTemplate', @callback
  57. it 'should send an email with the given template', ->
  58. @EmailHandler.sendEmail
  59. .calledWith('myCustomTemplate')
  60. .should.equal true
  61. describe "confirmEmailFromToken", ->
  62. beforeEach ->
  63. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  64. null,
  65. {@user_id, @email}
  66. )
  67. @UserUpdater.confirmEmail = sinon.stub().yields()
  68. describe "successfully", ->
  69. beforeEach ->
  70. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  71. it "should call getValueFromTokenAndExpire", ->
  72. @OneTimeTokenHandler.getValueFromTokenAndExpire
  73. .calledWith('email_confirmation', @token)
  74. .should.equal true
  75. it "should confirm the email of the user_id", ->
  76. @UserUpdater.confirmEmail
  77. .calledWith(@user_id, @email)
  78. .should.equal true
  79. it "should call the callback", ->
  80. @callback.called.should.equal true
  81. describe 'with an expired token', ->
  82. beforeEach ->
  83. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(null, null)
  84. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  85. it "should call the callback with a NotFoundError", ->
  86. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  87. describe 'with no user_id in the token', ->
  88. beforeEach ->
  89. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  90. null,
  91. {@email}
  92. )
  93. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  94. it "should call the callback with a NotFoundError", ->
  95. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true
  96. describe 'with no email in the token', ->
  97. beforeEach ->
  98. @OneTimeTokenHandler.getValueFromTokenAndExpire = sinon.stub().yields(
  99. null,
  100. {@user_id}
  101. )
  102. @UserEmailsConfirmationHandler.confirmEmailFromToken @token = 'mock-token', @callback
  103. it "should call the callback with a NotFoundError", ->
  104. @callback.calledWith(sinon.match.instanceOf(Errors.NotFoundError)).should.equal true