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"
|
2015-05-26 14:24:09 +00:00
|
|
|
@OneTimeTokenHandler =
|
2014-05-15 15:20:23 +00:00
|
|
|
getNewToken:sinon.stub()
|
2015-05-26 14:24:09 +00:00
|
|
|
getValueFromTokenAndExpire:sinon.stub()
|
2014-05-15 15:20:23 +00:00
|
|
|
@UserGetter =
|
2018-05-23 14:12:23 +00:00
|
|
|
getUserByMainEmail:sinon.stub()
|
2014-05-15 15:20:23 +00:00
|
|
|
@EmailHandler =
|
|
|
|
sendEmail:sinon.stub()
|
|
|
|
@AuthenticationManager =
|
|
|
|
setUserPassword:sinon.stub()
|
|
|
|
@PasswordResetHandler = SandboxedModule.require modulePath, requires:
|
|
|
|
"../User/UserGetter": @UserGetter
|
2015-05-27 19:50:16 +00:00
|
|
|
"../Security/OneTimeTokenHandler": @OneTimeTokenHandler
|
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)->
|
2018-05-23 14:12:23 +00:00
|
|
|
@UserGetter.getUserByMainEmail.callsArgWith(1)
|
2018-06-19 12:55:34 +00:00
|
|
|
@OneTimeTokenHandler.getNewToken.yields()
|
2014-08-08 10:41:54 +00:00
|
|
|
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err, exists)=>
|
|
|
|
exists.should.equal false
|
2014-05-15 15:20:23 +00:00
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
it "should send the email with the token", (done)->
|
|
|
|
|
2018-05-23 14:12:23 +00:00
|
|
|
@UserGetter.getUserByMainEmail.callsArgWith(1, null, @user)
|
2018-06-19 12:55:34 +00:00
|
|
|
@OneTimeTokenHandler.getNewToken.yields(null, @token)
|
2014-05-15 15:20:23 +00:00
|
|
|
@EmailHandler.sendEmail.callsArgWith(2)
|
2014-08-08 10:41:54 +00:00
|
|
|
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err, exists)=>
|
2014-05-15 15:20:23 +00:00
|
|
|
@EmailHandler.sendEmail.called.should.equal true
|
2014-08-08 10:41:54 +00:00
|
|
|
exists.should.equal true
|
2014-05-15 15:20:23 +00:00
|
|
|
args = @EmailHandler.sendEmail.args[0]
|
|
|
|
args[0].should.equal "passwordResetRequested"
|
2015-04-30 10:59:44 +00:00
|
|
|
args[1].setNewPasswordUrl.should.equal "#{@settings.siteUrl}/user/password/set?passwordResetToken=#{@token}&email=#{encodeURIComponent(@user.email)}"
|
2014-05-15 15:20:23 +00:00
|
|
|
done()
|
|
|
|
|
2015-04-14 12:11:49 +00:00
|
|
|
it "should return exists = false for a holdingAccount", (done) ->
|
|
|
|
@user.holdingAccount = true
|
2018-05-23 14:12:23 +00:00
|
|
|
@UserGetter.getUserByMainEmail.callsArgWith(1, null, @user)
|
2018-06-19 12:55:34 +00:00
|
|
|
@OneTimeTokenHandler.getNewToken.yields()
|
2015-04-14 12:11:49 +00:00
|
|
|
@PasswordResetHandler.generateAndEmailResetToken @user.email, (err, exists)=>
|
|
|
|
exists.should.equal false
|
|
|
|
done()
|
2014-05-15 15:20:23 +00:00
|
|
|
|
2014-05-15 15:50:38 +00:00
|
|
|
describe "setNewUserPassword", ->
|
2014-05-15 15:20:23 +00:00
|
|
|
|
2014-10-08 16:18:24 +00:00
|
|
|
it "should return false if no user id can be found", (done)->
|
2018-06-19 12:55:34 +00:00
|
|
|
@OneTimeTokenHandler.getValueFromTokenAndExpire.yields()
|
2014-10-08 16:18:24 +00:00
|
|
|
@PasswordResetHandler.setNewUserPassword @token, @password, (err, found) =>
|
|
|
|
found.should.equal false
|
2014-05-15 15:20:23 +00:00
|
|
|
@AuthenticationManager.setUserPassword.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should set the user password", (done)->
|
2018-06-19 12:55:34 +00:00
|
|
|
@OneTimeTokenHandler.getValueFromTokenAndExpire.yields(null, @user_id)
|
2014-05-15 15:20:23 +00:00
|
|
|
@AuthenticationManager.setUserPassword.callsArgWith(2)
|
2015-12-11 11:30:06 +00:00
|
|
|
@PasswordResetHandler.setNewUserPassword @token, @password, (err, found, user_id) =>
|
2014-10-08 16:18:24 +00:00
|
|
|
found.should.equal true
|
2015-12-11 11:30:06 +00:00
|
|
|
user_id.should.equal @user_id
|
2014-05-15 15:20:23 +00:00
|
|
|
@AuthenticationManager.setUserPassword.calledWith(@user_id, @password).should.equal true
|
|
|
|
done()
|
|
|
|
|