overleaf/services/web/test/UnitTests/coffee/PasswordReset/PasswordResetHandlerTests.coffee

79 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2014-05-15 15:20:23 +00:00
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
sinon = require('sinon')
modulePath = path.join __dirname, "../../../../app/js/Features/PasswordReset/PasswordResetHandler"
expect = require("chai").expect
describe "PasswordResetHandler", ->
beforeEach ->
@settings =
siteUrl: "www.sharelatex.com"
@PasswordResetTokenHandler =
2014-05-15 15:20:23 +00:00
getNewToken:sinon.stub()
getUserIdFromTokenAndExpire:sinon.stub()
2014-05-15 15:20:23 +00:00
@UserGetter =
getUser:sinon.stub()
@EmailHandler =
sendEmail:sinon.stub()
@AuthenticationManager =
setUserPassword:sinon.stub()
@PasswordResetHandler = SandboxedModule.require modulePath, requires:
"../User/UserGetter": @UserGetter
"./PasswordResetTokenHandler": @PasswordResetTokenHandler
2014-05-15 15:20:23 +00:00
"../Email/EmailHandler":@EmailHandler
"../Authentication/AuthenticationManager":@AuthenticationManager
"settings-sharelatex": @settings
2014-05-15 17:26:00 +00:00
"logger-sharelatex":
log:->
err:->
2014-05-15 15:20:23 +00:00
@token = "12312321i"
@user_id = "user_id_here"
@user =
email :"bob@bob.com"
@password = "my great secret password"
describe "generateAndEmailResetToken", ->
it "should check the user exists", (done)->
@UserGetter.getUser.callsArgWith(1)
@PasswordResetTokenHandler.getNewToken.callsArgWith(1)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err)=>
should.exist(err)
2014-05-15 15:20:23 +00:00
done()
it "should send the email with the token", (done)->
@UserGetter.getUser.callsArgWith(1, null, @user)
@PasswordResetTokenHandler.getNewToken.callsArgWith(1, null, @token)
2014-05-15 15:20:23 +00:00
@EmailHandler.sendEmail.callsArgWith(2)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err)=>
2014-05-15 15:20:23 +00:00
@EmailHandler.sendEmail.called.should.equal true
args = @EmailHandler.sendEmail.args[0]
args[0].should.equal "passwordResetRequested"
2014-05-15 16:58:25 +00:00
args[1].setNewPasswordUrl.should.equal "#{@settings.siteUrl}/user/password/set?passwordResetToken=#{@token}"
2014-05-15 15:20:23 +00:00
done()
2014-05-15 15:50:38 +00:00
describe "setNewUserPassword", ->
2014-05-15 15:20:23 +00:00
it "should return err if no user id can be found", (done)->
@PasswordResetTokenHandler.getUserIdFromTokenAndExpire.callsArgWith(1)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.setNewUserPassword @token, @password, (err)=>
2014-05-15 15:20:23 +00:00
err.should.exists
@AuthenticationManager.setUserPassword.called.should.equal false
done()
it "should set the user password", (done)->
@PasswordResetTokenHandler.getUserIdFromTokenAndExpire.callsArgWith(1, null, @user_id)
2014-05-15 15:20:23 +00:00
@AuthenticationManager.setUserPassword.callsArgWith(2)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.setNewUserPassword @token, @password, (err)=>
2014-05-15 15:20:23 +00:00
@AuthenticationManager.setUserPassword.calledWith(@user_id, @password).should.equal true
done()