2016-04-19 11:48:51 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
2016-04-27 12:05:12 -04:00
|
|
|
assert = chai.assert
|
2016-04-19 11:48:51 -04:00
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
2016-04-20 10:06:39 -04:00
|
|
|
modulePath = "../../../../app/js/Features/Compile/ClsiCookieManager.js"
|
2016-04-19 11:48:51 -04:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
realRequst = require("request")
|
2016-04-27 11:20:10 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
describe "ClsiCookieManager", ->
|
2016-04-19 11:48:51 -04:00
|
|
|
beforeEach ->
|
2016-04-27 11:56:21 -04:00
|
|
|
self = @
|
2016-04-19 11:48:51 -04:00
|
|
|
@redisMulti =
|
|
|
|
set:sinon.stub()
|
|
|
|
get:sinon.stub()
|
|
|
|
expire:sinon.stub()
|
|
|
|
exec:sinon.stub()
|
2016-04-27 11:56:21 -04:00
|
|
|
@redis =
|
|
|
|
auth:->
|
|
|
|
get:sinon.stub()
|
|
|
|
multi: -> return self.redisMulti
|
2016-04-19 11:48:51 -04:00
|
|
|
@project_id = "123423431321"
|
|
|
|
@request =
|
|
|
|
get: sinon.stub()
|
|
|
|
cookie:realRequst.cookie
|
|
|
|
jar: realRequst.jar
|
2016-04-27 12:05:12 -04:00
|
|
|
@settings =
|
|
|
|
redis:
|
|
|
|
web:"redis.something"
|
|
|
|
apis:
|
|
|
|
clsi:
|
|
|
|
url: "http://clsi.example.com"
|
2016-05-19 11:55:58 -04:00
|
|
|
clsiCookie:
|
|
|
|
ttl:Math.random()
|
|
|
|
key: "coooookie"
|
2016-04-27 12:05:12 -04:00
|
|
|
@requires =
|
2017-05-15 10:35:01 -04:00
|
|
|
"../../infrastructure/RedisWrapper": @RedisWrapper =
|
|
|
|
client: => @redis
|
2016-04-27 12:05:12 -04:00
|
|
|
"settings-sharelatex": @settings
|
2016-04-19 11:48:51 -04:00
|
|
|
"request": @request
|
|
|
|
|
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
|
2016-04-27 12:05:12 -04:00
|
|
|
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe "getServerId", ->
|
|
|
|
|
|
|
|
it "should call get for the key", (done)->
|
2016-04-27 11:56:21 -04:00
|
|
|
@redis.get.callsArgWith(1, null, "clsi-7")
|
2016-04-20 10:06:39 -04:00
|
|
|
@ClsiCookieManager._getServerId @project_id, (err, serverId)=>
|
2016-04-27 11:56:21 -04:00
|
|
|
@redis.get.calledWith("clsiserver:#{@project_id}").should.equal true
|
2016-04-19 11:48:51 -04:00
|
|
|
serverId.should.equal "clsi-7"
|
|
|
|
done()
|
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
it "should _populateServerIdViaRequest if no key is found", (done)->
|
|
|
|
@ClsiCookieManager._populateServerIdViaRequest = sinon.stub().callsArgWith(1)
|
2016-04-27 11:56:21 -04:00
|
|
|
@redis.get.callsArgWith(1, null)
|
2016-04-20 10:06:39 -04:00
|
|
|
@ClsiCookieManager._getServerId @project_id, (err, serverId)=>
|
|
|
|
@ClsiCookieManager._populateServerIdViaRequest.calledWith(@project_id).should.equal true
|
2016-04-19 11:48:51 -04:00
|
|
|
done()
|
|
|
|
|
2017-05-18 06:09:33 -04:00
|
|
|
it "should _populateServerIdViaRequest if no key is blank", (done)->
|
|
|
|
@ClsiCookieManager._populateServerIdViaRequest = sinon.stub().callsArgWith(1)
|
|
|
|
@redis.get.callsArgWith(1, null, "")
|
|
|
|
@ClsiCookieManager._getServerId @project_id, (err, serverId)=>
|
|
|
|
@ClsiCookieManager._populateServerIdViaRequest.calledWith(@project_id).should.equal true
|
|
|
|
done()
|
|
|
|
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-20 10:06:39 -04:00
|
|
|
describe "_populateServerIdViaRequest", ->
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-27 11:20:10 -04:00
|
|
|
beforeEach ->
|
|
|
|
@response = "some data"
|
|
|
|
@request.get.callsArgWith(1, null, @response)
|
|
|
|
@ClsiCookieManager.setServerId = sinon.stub().callsArgWith(2, null, "clsi-9")
|
|
|
|
|
2016-04-19 11:48:51 -04:00
|
|
|
it "should make a request to the clsi", (done)->
|
2016-04-20 10:06:39 -04:00
|
|
|
@ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
|
|
|
|
args = @ClsiCookieManager.setServerId.args[0]
|
2016-04-19 11:48:51 -04:00
|
|
|
args[0].should.equal @project_id
|
2016-04-27 11:20:10 -04:00
|
|
|
args[1].should.deep.equal @response
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should return the server id", (done)->
|
|
|
|
@ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
|
|
|
|
serverId.should.equal "clsi-9"
|
2016-04-19 11:48:51 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
describe "setServerId", ->
|
|
|
|
|
2016-04-27 11:20:10 -04:00
|
|
|
beforeEach ->
|
|
|
|
@response = "dsadsakj"
|
2016-04-20 10:06:39 -04:00
|
|
|
@ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
|
2016-04-19 11:48:51 -04:00
|
|
|
@redisMulti.exec.callsArgWith(0)
|
2016-04-27 11:20:10 -04:00
|
|
|
|
|
|
|
it "should set the server id with a ttl", (done)->
|
|
|
|
@ClsiCookieManager.setServerId @project_id, @response, (err)=>
|
2016-04-19 11:48:51 -04:00
|
|
|
@redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
|
2016-05-19 11:55:58 -04:00
|
|
|
@redisMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
|
2016-04-19 11:48:51 -04:00
|
|
|
done()
|
|
|
|
|
2016-04-27 11:20:10 -04:00
|
|
|
it "should return the server id", (done)->
|
|
|
|
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
|
|
|
|
serverId.should.equal "clsi-8"
|
|
|
|
done()
|
2016-04-19 11:48:51 -04:00
|
|
|
|
2016-04-27 12:05:12 -04:00
|
|
|
it "should not set the server id if clsiCookies are not enabled", (done)->
|
2016-05-19 11:55:58 -04:00
|
|
|
delete @settings.clsiCookie.key
|
2016-04-27 12:05:12 -04:00
|
|
|
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
|
|
|
|
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
|
|
|
|
@redisMulti.exec.called.should.equal false
|
|
|
|
done()
|
2017-05-19 05:23:01 -04:00
|
|
|
|
|
|
|
it "should not set the server id there is no server id in the response", (done)->
|
|
|
|
@ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns(null)
|
|
|
|
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
|
|
|
|
@redisMulti.exec.called.should.equal false
|
|
|
|
done()
|
2017-05-15 10:35:01 -04:00
|
|
|
|
|
|
|
it "should also set in the secondary if secondary redis is enabled", (done) ->
|
|
|
|
@redisSecondaryMulti =
|
|
|
|
set:sinon.stub()
|
|
|
|
expire:sinon.stub()
|
|
|
|
exec:sinon.stub()
|
|
|
|
@redis_secondary =
|
|
|
|
multi: => @redisSecondaryMulti
|
|
|
|
@settings.redis.clsi_cookie_secondary = {}
|
|
|
|
@RedisWrapper.client = sinon.stub()
|
|
|
|
@RedisWrapper.client.withArgs("clsi_cookie").returns(@redis)
|
|
|
|
@RedisWrapper.client.withArgs("clsi_cookie_secondary").returns(@redis_secondary)
|
|
|
|
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
|
|
|
|
@ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
|
|
|
|
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
|
|
|
|
@redisSecondaryMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
|
|
|
|
@redisSecondaryMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsiCookie.ttl).should.equal true
|
|
|
|
done()
|
2016-04-27 12:05:12 -04:00
|
|
|
|
2016-04-19 11:48:51 -04:00
|
|
|
describe "getCookieJar", ->
|
|
|
|
|
2016-04-27 12:05:12 -04:00
|
|
|
beforeEach ->
|
2016-04-20 10:06:39 -04:00
|
|
|
@ClsiCookieManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
|
2016-04-27 12:05:12 -04:00
|
|
|
|
|
|
|
it "should return a jar with the cookie set populated from redis", (done)->
|
2016-05-19 08:45:44 -04:00
|
|
|
@ClsiCookieManager.getCookieJar @project_id, (err, jar)=>
|
2016-05-19 11:55:58 -04:00
|
|
|
jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].key.should.equal
|
|
|
|
jar._jar.store.idx["clsi.example.com"]["/"][@settings.clsiCookie.key].value.should.equal "clsi-11"
|
2016-04-19 11:48:51 -04:00
|
|
|
done()
|
|
|
|
|
|
|
|
|
2016-04-27 12:05:12 -04:00
|
|
|
it "should return empty cookie jar if clsiCookies are not enabled", (done)->
|
2016-05-19 11:55:58 -04:00
|
|
|
delete @settings.clsiCookie.key
|
2016-04-27 12:05:12 -04:00
|
|
|
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
|
|
|
|
@ClsiCookieManager.getCookieJar @project_id, (err, jar)->
|
|
|
|
assert.deepEqual jar, realRequst.jar()
|
|
|
|
done()
|
2016-04-19 11:48:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|