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

114 lines
3.8 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/PasswordResetController"
expect = require("chai").expect
describe "PasswordResetController", ->
beforeEach ->
@settings = {}
2014-05-15 15:50:38 +00:00
@PasswordResetHandler =
generateAndEmailResetToken:sinon.stub()
setNewUserPassword:sinon.stub()
@RateLimiter =
addCount: sinon.stub()
2014-05-15 15:20:23 +00:00
@PasswordResetController = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
2014-05-15 15:50:38 +00:00
"./PasswordResetHandler":@PasswordResetHandler
2014-05-15 15:20:23 +00:00
"logger-sharelatex": log:->
"../../infrastructure/RateLimiter":@RateLimiter
2014-05-15 15:20:23 +00:00
2014-05-15 15:50:38 +00:00
@email = "bob@bob.com "
@token = "my security token that was emailed to me"
@password = "my new password"
@req =
body:
email:@email
passwordResetToken:@token
2014-05-15 15:50:38 +00:00
password:@password
2014-05-15 15:20:23 +00:00
@res = {}
2014-05-15 15:20:23 +00:00
2014-05-15 15:50:38 +00:00
describe "requestReset", ->
2014-05-15 15:20:23 +00:00
it "should error if the rate limit is hit", (done)->
@PasswordResetHandler.generateAndEmailResetToken.callsArgWith(1)
@RateLimiter.addCount.callsArgWith(1, null, false)
@res.send = (code)=>
code.should.equal 500
@PasswordResetHandler.generateAndEmailResetToken.calledWith(@email.trim()).should.equal false
done()
@PasswordResetController.requestReset @req, @res
2014-05-15 15:50:38 +00:00
it "should tell the handler to process that email", (done)->
@RateLimiter.addCount.callsArgWith(1, null, true)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.generateAndEmailResetToken.callsArgWith(1)
@res.send = (code)=>
code.should.equal 200
@PasswordResetHandler.generateAndEmailResetToken.calledWith(@email.trim()).should.equal true
done()
@PasswordResetController.requestReset @req, @res
2014-05-15 15:20:23 +00:00
2014-05-15 15:50:38 +00:00
it "should send a 500 if there is an error", (done)->
@RateLimiter.addCount.callsArgWith(1, null, true)
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.generateAndEmailResetToken.callsArgWith(1, "error")
@res.send = (code)=>
code.should.equal 500
done()
@PasswordResetController.requestReset @req, @res
2014-06-10 16:54:29 +00:00
it "should lowercase the email address", (done)->
@email = "UPerCaseEMAIL@example.Com"
@req.body.email = @email
@RateLimiter.addCount.callsArgWith(1, null, true)
@PasswordResetHandler.generateAndEmailResetToken.callsArgWith(1)
@res.send = (code)=>
code.should.equal 200
@PasswordResetHandler.generateAndEmailResetToken.calledWith(@email.toLowerCase()).should.equal true
done()
@PasswordResetController.requestReset @req, @res
2014-05-15 15:50:38 +00:00
describe "setNewUserPassword", ->
it "should tell the user handler to reset the password", (done)->
@PasswordResetHandler.setNewUserPassword.callsArgWith(2)
@res.send = (code)=>
code.should.equal 200
@PasswordResetHandler.setNewUserPassword.calledWith(@token, @password).should.equal true
done()
@PasswordResetController.setNewUserPassword @req, @res
it "should send a 500 if there is an error", (done)->
@PasswordResetHandler.setNewUserPassword.callsArgWith(2, "error")
@res.send = (code)=>
code.should.equal 500
done()
@PasswordResetController.setNewUserPassword @req, @res
it "should error if there is no password", (done)->
@req.body.password = ""
@PasswordResetHandler.setNewUserPassword.callsArgWith(2)
@res.send = (code)=>
code.should.equal 500
@PasswordResetHandler.setNewUserPassword.called.should.equal false
done()
@PasswordResetController.setNewUserPassword @req, @res
it "should error if there is no password", (done)->
@req.body.passwordResetToken = ""
2014-05-15 15:50:38 +00:00
@PasswordResetHandler.setNewUserPassword.callsArgWith(2)
@res.send = (code)=>
code.should.equal 500
@PasswordResetHandler.setNewUserPassword.called.should.equal false
done()
@PasswordResetController.setNewUserPassword @req, @res
2014-05-15 15:20:23 +00:00