2014-05-15 12:16:20 -04:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
2014-05-16 05:43:55 -04:00
|
|
|
modulePath = path.join __dirname, "../../../../app/js/Features/PasswordReset/PasswordResetTokenHandler"
|
2014-05-15 12:16:20 -04:00
|
|
|
expect = require("chai").expect
|
|
|
|
|
2014-05-16 05:43:55 -04:00
|
|
|
describe "PasswordResetTokenHandler", ->
|
2014-05-15 12:16:20 -04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@user_id = "user id here"
|
2014-05-16 05:52:31 -04:00
|
|
|
@stubbedToken = require("crypto").randomBytes(32)
|
2014-05-15 12:16:20 -04:00
|
|
|
|
|
|
|
@settings =
|
|
|
|
redis:
|
|
|
|
web:{}
|
|
|
|
@redisMulti =
|
|
|
|
set:sinon.stub()
|
2014-05-15 12:20:42 -04:00
|
|
|
get:sinon.stub()
|
|
|
|
del:sinon.stub()
|
2014-05-15 12:16:20 -04:00
|
|
|
expire:sinon.stub()
|
|
|
|
exec:sinon.stub()
|
|
|
|
@uuid = v4 : -> return @stubbedToken
|
|
|
|
self = @
|
2014-05-16 05:43:55 -04:00
|
|
|
@PasswordResetTokenHandler = SandboxedModule.require modulePath, requires:
|
2014-05-15 12:16:20 -04:00
|
|
|
"redis" :
|
|
|
|
createClient: =>
|
|
|
|
auth:->
|
|
|
|
multi: -> return self.redisMulti
|
|
|
|
|
|
|
|
"settings-sharelatex":@settings
|
|
|
|
"logger-sharelatex": log:->
|
|
|
|
"node-uuid":@uuid
|
|
|
|
|
|
|
|
|
|
|
|
describe "getNewToken", ->
|
|
|
|
|
|
|
|
it "should set a new token into redis with a ttl", (done)->
|
|
|
|
@redisMulti.exec.callsArgWith(0)
|
2014-05-16 05:43:55 -04:00
|
|
|
@PasswordResetTokenHandler.getNewToken @user_id, (err, token)=>
|
2014-05-15 12:58:25 -04:00
|
|
|
@redisMulti.set "password_token:#{@stubbedToken}", @user_id
|
|
|
|
@redisMulti.expire "password_token:#{@stubbedToken}", 60 * 60
|
2014-05-15 12:16:20 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return if there was an error", (done)->
|
|
|
|
@redisMulti.exec.callsArgWith(0, "error")
|
2014-05-16 05:43:55 -04:00
|
|
|
@PasswordResetTokenHandler.getNewToken @user_id, (err, token)=>
|
2014-05-15 12:16:20 -04:00
|
|
|
err.should.exist
|
|
|
|
done()
|
2014-05-15 12:20:42 -04:00
|
|
|
|
|
|
|
|
2014-05-16 05:43:55 -04:00
|
|
|
describe "getUserIdFromTokenAndExpire", ->
|
2014-05-15 12:20:42 -04:00
|
|
|
|
|
|
|
it "should get and delete the token", (done)->
|
|
|
|
@redisMulti.exec.callsArgWith(0, null, [@user_id])
|
2014-05-16 05:43:55 -04:00
|
|
|
@PasswordResetTokenHandler.getUserIdFromTokenAndExpire @stubbedToken, (err, user_id)=>
|
2014-05-15 12:20:42 -04:00
|
|
|
user_id.should.equal @user_id
|
2014-05-15 12:58:25 -04:00
|
|
|
@redisMulti.get.calledWith("password_token:#{@stubbedToken}").should.equal true
|
|
|
|
@redisMulti.del.calledWith("password_token:#{@stubbedToken}").should.equal true
|
2014-05-15 12:20:42 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
|