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