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

77 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2014-05-15 11:20:23 -04: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"
@TokenGenerator =
getNewToken:sinon.stub()
getUserIdFromToken:sinon.stub()
@UserGetter =
getUser:sinon.stub()
@EmailHandler =
sendEmail:sinon.stub()
@AuthenticationManager =
setUserPassword:sinon.stub()
@PasswordResetHandler = SandboxedModule.require modulePath, requires:
"../User/UserGetter": @UserGetter
"./TokenGenerator": @TokenGenerator
"../Email/EmailHandler":@EmailHandler
"../Authentication/AuthenticationManager":@AuthenticationManager
"settings-sharelatex": @settings
"logger-sharelatex": log:->
@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)
@TokenGenerator.getNewToken.callsArgWith(1)
2014-05-15 11:50:38 -04:00
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err)=>
2014-05-15 11:20:23 -04:00
err.should.exists
done()
it "should send the email with the token", (done)->
@UserGetter.getUser.callsArgWith(1, null, @user)
@TokenGenerator.getNewToken.callsArgWith(1, null, @token)
@EmailHandler.sendEmail.callsArgWith(2)
2014-05-15 11:50:38 -04:00
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err)=>
2014-05-15 11:20:23 -04:00
@EmailHandler.sendEmail.called.should.equal true
args = @EmailHandler.sendEmail.args[0]
args[0].should.equal "passwordResetRequested"
2014-05-15 12:58:25 -04:00
args[1].setNewPasswordUrl.should.equal "#{@settings.siteUrl}/user/password/set?passwordResetToken=#{@token}"
2014-05-15 11:20:23 -04:00
done()
2014-05-15 11:50:38 -04:00
describe "setNewUserPassword", ->
2014-05-15 11:20:23 -04:00
it "should return err if no user id can be found", (done)->
@TokenGenerator.getUserIdFromToken.callsArgWith(1)
2014-05-15 11:50:38 -04:00
@PasswordResetHandler.setNewUserPassword @token, @password, (err)=>
2014-05-15 11:20:23 -04:00
err.should.exists
@AuthenticationManager.setUserPassword.called.should.equal false
done()
it "should set the user password", (done)->
@TokenGenerator.getUserIdFromToken.callsArgWith(1, null, @user_id)
@AuthenticationManager.setUserPassword.callsArgWith(2)
2014-05-15 11:50:38 -04:00
@PasswordResetHandler.setNewUserPassword @token, @password, (err)=>
2014-05-15 11:20:23 -04:00
@AuthenticationManager.setUserPassword.calledWith(@user_id, @password).should.equal true
done()