Merge branch 'project_url_query' into clsi-dynamic-load

This commit is contained in:
Henry Oswald
2016-05-19 13:08:20 +01:00
14 changed files with 411 additions and 76 deletions
@@ -0,0 +1,133 @@
sinon = require('sinon')
chai = require('chai')
assert = chai.assert
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/Features/Compile/ClsiCookieManager.js"
SandboxedModule = require('sandboxed-module')
realRequst = require("request")
describe "ClsiCookieManager", ->
beforeEach ->
self = @
@redisMulti =
set:sinon.stub()
get:sinon.stub()
expire:sinon.stub()
exec:sinon.stub()
@redis =
auth:->
get:sinon.stub()
multi: -> return self.redisMulti
@project_id = "123423431321"
@request =
get: sinon.stub()
cookie:realRequst.cookie
jar: realRequst.jar
@settings =
redis:
web:"redis.something"
apis:
clsi:
url: "http://clsi.example.com"
clsi_cookie_expire_length_seconds: Math.random()
clsiCookieKey: "coooookie"
@requires =
"redis-sharelatex" :
createClient: =>
@redis
"settings-sharelatex": @settings
"request": @request
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
describe "getServerId", ->
it "should call get for the key", (done)->
@redis.get.callsArgWith(1, null, "clsi-7")
@ClsiCookieManager._getServerId @project_id, (err, serverId)=>
@redis.get.calledWith("clsiserver:#{@project_id}").should.equal true
serverId.should.equal "clsi-7"
done()
it "should _populateServerIdViaRequest if no key is found", (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()
describe "_populateServerIdViaRequest", ->
beforeEach ->
@response = "some data"
@request.get.callsArgWith(1, null, @response)
@ClsiCookieManager.setServerId = sinon.stub().callsArgWith(2, null, "clsi-9")
it "should make a request to the clsi", (done)->
@ClsiCookieManager._populateServerIdViaRequest @project_id, (err, serverId)=>
args = @ClsiCookieManager.setServerId.args[0]
args[0].should.equal @project_id
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"
done()
describe "setServerId", ->
beforeEach ->
@response = "dsadsakj"
@ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
@redisMulti.exec.callsArgWith(0)
it "should set the server id with a ttl", (done)->
@ClsiCookieManager.setServerId @project_id, @response, (err)=>
@redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
@redisMulti.expire.calledWith("clsiserver:#{@project_id}", @settings.clsi_cookie_expire_length_seconds).should.equal true
done()
it "should return the server id", (done)->
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
serverId.should.equal "clsi-8"
done()
it "should not set the server id if clsiCookies are not enabled", (done)->
delete @settings.clsiCookieKey
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
@ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=>
@redisMulti.exec.called.should.equal false
done()
describe "getCookieJar", ->
beforeEach ->
@ClsiCookieManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
it "should return a jar with the cookie set populated from redis", (done)->
@ClsiCookieManager.getCookieJar @project_id, (err, jar)->
jar._jar.store.idx["clsi.example.com"]["/"].clsiserver.key.should.equal "clsiserver"
jar._jar.store.idx["clsi.example.com"]["/"].clsiserver.value.should.equal "clsi-11"
done()
it "should return empty cookie jar if clsiCookies are not enabled", (done)->
delete @settings.clsiCookieKey
@ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires
@ClsiCookieManager.getCookieJar @project_id, (err, jar)->
assert.deepEqual jar, realRequst.jar()
done()
@@ -7,6 +7,11 @@ SandboxedModule = require('sandboxed-module')
describe "ClsiManager", ->
beforeEach ->
@jar = {cookie:"stuff"}
@ClsiCookieManager =
getCookieJar: sinon.stub().callsArgWith(1, null, @jar)
setServerId: sinon.stub().callsArgWith(2)
_getServerId:sinon.stub()
@ClsiManager = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings =
apis:
@@ -19,14 +24,16 @@ describe "ClsiManager", ->
url: "https://clsipremium.example.com"
"../../models/Project": Project: @Project = {}
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"./ClsiCookieManager": @ClsiCookieManager
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
"request": @request = {}
"request": @request = sinon.stub()
@project_id = "project-id"
@callback = sinon.stub()
describe "sendRequest", ->
beforeEach ->
@ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
@ClsiCookieManager._getServerId.callsArgWith(1, null, "clsi3")
describe "with a successful compile", ->
beforeEach ->
@@ -80,15 +87,15 @@ describe "ClsiManager", ->
describe "deleteAuxFiles", ->
beforeEach ->
@request.del = sinon.stub().callsArg(1)
@ClsiManager._makeRequest = sinon.stub().callsArg(2)
describe "with the standard compileGroup", ->
beforeEach ->
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
it "should call the delete method in the standard CLSI", ->
@request.del
.calledWith("#{@settings.apis.clsi.url}/project/#{@project_id}")
@ClsiManager._makeRequest
.calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi.url}/project/#{@project_id}"})
.should.equal true
it "should call the callback", ->
@@ -99,8 +106,8 @@ describe "ClsiManager", ->
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "priority"}, @callback
it "should call the delete method in the CLSI", ->
@request.del
.calledWith("#{@settings.apis.clsi_priority.url}/project/#{@project_id}")
@ClsiManager._makeRequest
.calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi_priority.url}/project/#{@project_id}"})
.should.equal true
describe "_buildRequest", ->
@@ -235,15 +242,15 @@ describe "ClsiManager", ->
describe "successfully", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, @body = { mock: "foo" })
@ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 204}, @body = { mock: "foo" })
@ClsiManager._postToClsi @project_id, @req, "standard", @callback
it 'should send the request to the CLSI', ->
url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
@request.post.calledWith({
@ClsiManager._makeRequest.calledWith(@project_id, {
method: "POST",
url: url
json: @req
jar: false
}).should.equal true
it "should call the callback with the body and no error", ->
@@ -251,7 +258,7 @@ describe "ClsiManager", ->
describe "when the CLSI returns an error", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
@ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 500}, @body = { mock: "foo" })
@ClsiManager._postToClsi @project_id, @req, "standard", @callback
it "should call the callback with the body and the error", ->
@@ -259,20 +266,20 @@ describe "ClsiManager", ->
describe "when the compiler is priority", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
@ClsiManager._makeRequest = sinon.stub()
@ClsiManager._postToClsi @project_id, @req, "priority", @callback
it "should use the clsi_priority url", ->
url = "#{@settings.apis.clsi_priority.url}/project/#{@project_id}/compile"
@request.post.calledWith({
@ClsiManager._makeRequest.calledWith(@project_id, {
method: "POST",
url: url
json: @req
jar: false
}).should.equal true
describe "wordCount", ->
beforeEach ->
@request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body = { mock: "foo" })
@ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 200}, @body = { mock: "foo" })
@ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
@ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url"
@@ -281,8 +288,8 @@ describe "ClsiManager", ->
@ClsiManager.wordCount @project_id, false, {}, @callback
it "should call wordCount with root file", ->
@request.get
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
@ClsiManager._makeRequest
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
.should.equal true
it "should call the callback", ->
@@ -293,8 +300,8 @@ describe "ClsiManager", ->
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
it "should call wordCount with param file", ->
@request.get
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
@ClsiManager._makeRequest
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
.should.equal true
describe "with image", ->
@@ -303,6 +310,50 @@ describe "ClsiManager", ->
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
it "should call wordCount with file and image", ->
@request.get
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
@ClsiManager._makeRequest
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
.should.equal true
describe "_makeRequest", ->
beforeEach ->
@response = {there:"something"}
@request.callsArgWith(1, null, @response)
@opts =
method: "SOMETHIGN"
url: "http://a place on the web"
it "should process a request with a cookie jar", (done)->
@ClsiManager._makeRequest @project_id, @opts, =>
args = @request.args[0]
args[0].method.should.equal @opts.method
args[0].url.should.equal @opts.url
args[0].jar.should.equal @jar
done()
it "should set the cookie again on response as it might have changed", (done)->
@ClsiManager._makeRequest @project_id, @opts, =>
@ClsiCookieManager.setServerId.calledWith(@project_id, @response).should.equal true
done()
@@ -22,6 +22,9 @@ describe "CompileController", ->
url: "clsi.example.com"
clsi_priority:
url: "clsi-priority.example.com"
@jar = {cookie:"stuff"}
@ClsiCookieManager =
getCookieJar:sinon.stub().callsArgWith(1, null, @jar)
@CompileController = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings
"request": @request = sinon.stub()
@@ -33,6 +36,7 @@ describe "CompileController", ->
"./ClsiManager": @ClsiManager
"../Authentication/AuthenticationController": @AuthenticationController = {}
"../../infrastructure/RateLimiter":@RateLimiter
"./ClsiCookieManager":@ClsiCookieManager
@project_id = "project-id"
@user =
features:
@@ -182,6 +186,7 @@ describe "CompileController", ->
it "should open a request to the CLSI", ->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi.url}#{@url}",
timeout: 60 * 1000
@@ -204,6 +209,7 @@ describe "CompileController", ->
it "should proxy to the priority url if the user has the feature", ()->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi_priority.url}#{@url}",
timeout: 60 * 1000
@@ -218,6 +224,7 @@ describe "CompileController", ->
it "should open a request to the CLSI", ->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi.url}#{@url}",
timeout: 60 * 1000
@@ -240,6 +247,7 @@ describe "CompileController", ->
it "should proxy to the priority url if the user has the feature", ()->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi_priority.url}#{@url}",
timeout: 60 * 1000
@@ -254,6 +262,7 @@ describe "CompileController", ->
it "should proxy to the standard url", ()->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi.url}#{@url}",
timeout: 60 * 1000
@@ -269,6 +278,7 @@ describe "CompileController", ->
it "should proxy to the standard url without the build parameter", ()->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi.url}#{@url}",
timeout: 60 * 1000
@@ -286,6 +296,7 @@ describe "CompileController", ->
it "should open a request to the CLSI", ->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi.url}#{@url}",
timeout: 60 * 1000
@@ -313,6 +324,7 @@ describe "CompileController", ->
it "should proxy to the priority url if the user has the feature", ()->
@request
.calledWith(
jar:@jar
method: @req.method
url: "#{@settings.apis.clsi_priority.url}#{@url}",
timeout: 60 * 1000
@@ -331,8 +343,8 @@ describe "CompileController", ->
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
it "should proxy to the standard url with the build parameter", ()->
@request
.calledWith(
@request.calledWith(
jar:@jar
method: @req.method
qs: {build: 1234}
url: "#{@settings.apis.clsi.url}#{@url}",
@@ -63,6 +63,10 @@ class MockResponse
@body = body if body
@callback() if @callback?
status: (@statusCode)->
return @
setHeader: (header, value) ->
@headers[header] = value