mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
persist cookie in redis for compiles.
This commit is contained in:
parent
ffa04c7b55
commit
b37595acf9
8 changed files with 262 additions and 56 deletions
|
@ -6,8 +6,19 @@ Project = require("../../models/Project").Project
|
||||||
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
|
ProjectEntityHandler = require("../Project/ProjectEntityHandler")
|
||||||
logger = require "logger-sharelatex"
|
logger = require "logger-sharelatex"
|
||||||
url = require("url")
|
url = require("url")
|
||||||
|
ClsiRequestManager = require("./ClsiRequestManager")
|
||||||
|
|
||||||
|
|
||||||
module.exports = ClsiManager =
|
module.exports = ClsiManager =
|
||||||
|
|
||||||
|
_makeRequest: (project_id, opts, callback)->
|
||||||
|
ClsiRequestManager.getCookieJar project_id, (err, jar)->
|
||||||
|
if err?
|
||||||
|
logger.err err:err, "error getting cookie jar for clsi request"
|
||||||
|
return callback(err)
|
||||||
|
opts.jar = jar
|
||||||
|
request opts, callback
|
||||||
|
|
||||||
sendRequest: (project_id, options = {}, callback = (error, success) ->) ->
|
sendRequest: (project_id, options = {}, callback = (error, success) ->) ->
|
||||||
ClsiManager._buildRequest project_id, options, (error, req) ->
|
ClsiManager._buildRequest project_id, options, (error, req) ->
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
|
@ -23,7 +34,10 @@ module.exports = ClsiManager =
|
||||||
|
|
||||||
deleteAuxFiles: (project_id, options, callback = (error) ->) ->
|
deleteAuxFiles: (project_id, options, callback = (error) ->) ->
|
||||||
compilerUrl = @_getCompilerUrl(options?.compileGroup)
|
compilerUrl = @_getCompilerUrl(options?.compileGroup)
|
||||||
request.del "#{compilerUrl}/project/#{project_id}", callback
|
opts =
|
||||||
|
url:"#{compilerUrl}/project/#{project_id}"
|
||||||
|
method:"DELETE"
|
||||||
|
ClsiManager._makeRequest project_id, opts, callback
|
||||||
|
|
||||||
_getCompilerUrl: (compileGroup) ->
|
_getCompilerUrl: (compileGroup) ->
|
||||||
if compileGroup == "priority"
|
if compileGroup == "priority"
|
||||||
|
@ -33,13 +47,11 @@ module.exports = ClsiManager =
|
||||||
|
|
||||||
_postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
|
_postToClsi: (project_id, req, compileGroup, callback = (error, response) ->) ->
|
||||||
compilerUrl = @_getCompilerUrl(compileGroup)
|
compilerUrl = @_getCompilerUrl(compileGroup)
|
||||||
request.post {
|
opts =
|
||||||
url: "#{compilerUrl}/project/#{project_id}/compile"
|
url: "#{compilerUrl}/project/#{project_id}/compile"
|
||||||
json: req
|
json: req
|
||||||
jar: false
|
method: "POST"
|
||||||
query:
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
||||||
project_id:project_id
|
|
||||||
}, (error, response, body) ->
|
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
if 200 <= response.statusCode < 300
|
if 200 <= response.statusCode < 300
|
||||||
callback null, body
|
callback null, body
|
||||||
|
@ -117,9 +129,10 @@ module.exports = ClsiManager =
|
||||||
wordcount_url = "#{compilerUrl}/project/#{project_id}/wordcount?file=#{encodeURIComponent(filename)}"
|
wordcount_url = "#{compilerUrl}/project/#{project_id}/wordcount?file=#{encodeURIComponent(filename)}"
|
||||||
if req.compile.options.imageName?
|
if req.compile.options.imageName?
|
||||||
wordcount_url += "&image=#{encodeURIComponent(req.compile.options.imageName)}"
|
wordcount_url += "&image=#{encodeURIComponent(req.compile.options.imageName)}"
|
||||||
request.get {
|
opts =
|
||||||
url: wordcount_url
|
url: wordcount_url
|
||||||
}, (error, response, body) ->
|
method: "GET"
|
||||||
|
ClsiManager._makeRequest project_id, opts, (error, response, body) ->
|
||||||
return callback(error) if error?
|
return callback(error) if error?
|
||||||
if 200 <= response.statusCode < 300
|
if 200 <= response.statusCode < 300
|
||||||
callback null, body
|
callback null, body
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
Settings = require "settings-sharelatex"
|
||||||
|
request = require('request')
|
||||||
|
redis = require("redis-sharelatex")
|
||||||
|
rclient = redis.createClient(Settings.redis.web)
|
||||||
|
cookie = require('cookie')
|
||||||
|
|
||||||
|
buildKey = (project_id)->
|
||||||
|
return "clsiserver:#{project_id}"
|
||||||
|
|
||||||
|
|
||||||
|
ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7
|
||||||
|
|
||||||
|
module.exports = ClsiRequestManager =
|
||||||
|
|
||||||
|
_getServerId : (project_id, callback = (err, serverId)->)->
|
||||||
|
multi = rclient.multi()
|
||||||
|
multi.get buildKey(project_id)
|
||||||
|
multi.expire buildKey(project_id), ONE_WEEK_IN_SECONDS
|
||||||
|
multi.exec (err, results)->
|
||||||
|
if err?
|
||||||
|
return callback(err)
|
||||||
|
serverId = results[0]
|
||||||
|
if serverId?
|
||||||
|
return callback(null, serverId)
|
||||||
|
else
|
||||||
|
return ClsiRequestManager._getServerIdViaRequest project_id, callback
|
||||||
|
|
||||||
|
|
||||||
|
_getServerIdViaRequest :(project_id, callback = (err, serverId)->)->
|
||||||
|
url = "#{Settings.apis.clsi.url}/project/#{project_id}/status"
|
||||||
|
request.get url, (err, res, body)->
|
||||||
|
if err?
|
||||||
|
logger.err err:err, project_id:project_id, "error getting initial server id for project"
|
||||||
|
return callback(err)
|
||||||
|
ClsiRequestManager.setServerId project_id, res, callback
|
||||||
|
|
||||||
|
_parseServerIdFromResponse : (response)->
|
||||||
|
console.log response.headers
|
||||||
|
cookies = cookie.parse(response.headers["set-cookie"]?[0] or "")
|
||||||
|
return cookies?.clsiserver
|
||||||
|
|
||||||
|
setServerId: (project_id, response, callback = ->)->
|
||||||
|
serverId = ClsiRequestManager._parseServerIdFromResponse(response)
|
||||||
|
multi = rclient.multi()
|
||||||
|
multi.set buildKey(project_id), serverId
|
||||||
|
multi.expire buildKey(project_id), ONE_WEEK_IN_SECONDS
|
||||||
|
multi.exec callback
|
||||||
|
|
||||||
|
|
||||||
|
getCookieJar: (project_id, opts, callback = (err, jar)->)->
|
||||||
|
ClsiRequestManager._getServerId project_id, (err, serverId)=>
|
||||||
|
if err?
|
||||||
|
logger.err err:err, project_id:project_id, "error getting server id"
|
||||||
|
return callback(err)
|
||||||
|
cookie = request.cookie("clsiserver=#{serverId}")
|
||||||
|
jar = request.jar()
|
||||||
|
jar.setCookie cookie, Settings.apis.clsi.url
|
||||||
|
callback(null, jar)
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ Settings = require "settings-sharelatex"
|
||||||
AuthenticationController = require "../Authentication/AuthenticationController"
|
AuthenticationController = require "../Authentication/AuthenticationController"
|
||||||
UserGetter = require "../User/UserGetter"
|
UserGetter = require "../User/UserGetter"
|
||||||
RateLimiter = require("../../infrastructure/RateLimiter")
|
RateLimiter = require("../../infrastructure/RateLimiter")
|
||||||
|
ClsiRequestManager = require("./ClsiRequestManager")
|
||||||
|
|
||||||
|
|
||||||
module.exports = CompileController =
|
module.exports = CompileController =
|
||||||
compile: (req, res, next = (error) ->) ->
|
compile: (req, res, next = (error) ->) ->
|
||||||
|
@ -107,31 +109,34 @@ module.exports = CompileController =
|
||||||
CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
|
CompileController.proxyToClsiWithLimits(project_id, url, limits, req, res, next)
|
||||||
|
|
||||||
proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
|
proxyToClsiWithLimits: (project_id, url, limits, req, res, next = (error) ->) ->
|
||||||
if limits.compileGroup == "priority"
|
ClsiRequestManager.getCookieJar project_id, (err, jar)->
|
||||||
compilerUrl = Settings.apis.clsi_priority.url
|
if err?
|
||||||
else
|
logger.err err:err, "error getting cookie jar for clsi request"
|
||||||
compilerUrl = Settings.apis.clsi.url
|
return callback(err)
|
||||||
url = "#{compilerUrl}#{url}"
|
if limits.compileGroup == "priority"
|
||||||
logger.log url: url, "proxying to CLSI"
|
compilerUrl = Settings.apis.clsi_priority.url
|
||||||
oneMinute = 60 * 1000
|
else
|
||||||
# the base request
|
compilerUrl = Settings.apis.clsi.url
|
||||||
options = { url: url, method: req.method, timeout: oneMinute }
|
url = "#{compilerUrl}#{url}"
|
||||||
# if we have a build parameter, pass it through to the clsi
|
logger.log url: url, "proxying to CLSI"
|
||||||
if req.query?.pdfng && req.query?.build? # only for new pdf viewer
|
oneMinute = 60 * 1000
|
||||||
options.qs = {}
|
# the base request
|
||||||
options.qs.build = req.query.build
|
options = { url: url, method: req.method, timeout: oneMinute, jar : jar }
|
||||||
# if we are byte serving pdfs, pass through If-* and Range headers
|
# if we have a build parameter, pass it through to the clsi
|
||||||
# do not send any others, there's a proxying loop if Host: is passed!
|
if req.query?.pdfng && req.query?.build? # only for new pdf viewer
|
||||||
if req.query?.pdfng
|
options.qs = {}
|
||||||
newHeaders = {}
|
options.qs.build = req.query.build
|
||||||
for h, v of req.headers
|
# if we are byte serving pdfs, pass through If-* and Range headers
|
||||||
newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
|
# do not send any others, there's a proxying loop if Host: is passed!
|
||||||
options.headers = newHeaders
|
if req.query?.pdfng
|
||||||
req.query.project_id = project_id
|
newHeaders = {}
|
||||||
proxy = request(options)
|
for h, v of req.headers
|
||||||
proxy.pipe(res)
|
newHeaders[h] = req.headers[h] if h.match /^(If-|Range)/i
|
||||||
proxy.on "error", (error) ->
|
options.headers = newHeaders
|
||||||
logger.warn err: error, url: url, "CLSI proxy error"
|
proxy = request(options)
|
||||||
|
proxy.pipe(res)
|
||||||
|
proxy.on "error", (error) ->
|
||||||
|
logger.warn err: error, url: url, "CLSI proxy error"
|
||||||
|
|
||||||
wordCount: (req, res, next) ->
|
wordCount: (req, res, next) ->
|
||||||
project_id = req.params.Project_id
|
project_id = req.params.Project_id
|
||||||
|
|
|
@ -125,7 +125,7 @@ apiRouter.get "/profile", (req, res) ->
|
||||||
, time
|
, time
|
||||||
|
|
||||||
app.get "/heapdump", (req, res)->
|
app.get "/heapdump", (req, res)->
|
||||||
require('heapdump').writeSnapshot '/tmp/' + Date.now() + '.clsi.heapsnapshot', (err, filename)->
|
require('heapdump').writeSnapshot '/tmp/' + Date.now() + '.web.heapsnapshot', (err, filename)->
|
||||||
res.send filename
|
res.send filename
|
||||||
|
|
||||||
logger.info ("creating HTTP server").yellow
|
logger.info ("creating HTTP server").yellow
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
"body-parser": "^1.13.1",
|
"body-parser": "^1.13.1",
|
||||||
"bufferedstream": "1.6.0",
|
"bufferedstream": "1.6.0",
|
||||||
"connect-redis": "2.3.0",
|
"connect-redis": "2.3.0",
|
||||||
|
"cookie": "^0.2.3",
|
||||||
"cookie-parser": "1.3.5",
|
"cookie-parser": "1.3.5",
|
||||||
"csurf": "^1.8.3",
|
"csurf": "^1.8.3",
|
||||||
"dateformat": "1.0.4-1.2.3",
|
"dateformat": "1.0.4-1.2.3",
|
||||||
|
|
|
@ -7,6 +7,9 @@ SandboxedModule = require('sandboxed-module')
|
||||||
|
|
||||||
describe "ClsiManager", ->
|
describe "ClsiManager", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
@jar = {cookie:"stuff"}
|
||||||
|
@ClsiRequestManager =
|
||||||
|
getCookieJar: sinon.stub().callsArgWith(1, null, @jar)
|
||||||
@ClsiManager = SandboxedModule.require modulePath, requires:
|
@ClsiManager = SandboxedModule.require modulePath, requires:
|
||||||
"settings-sharelatex": @settings =
|
"settings-sharelatex": @settings =
|
||||||
apis:
|
apis:
|
||||||
|
@ -19,8 +22,9 @@ describe "ClsiManager", ->
|
||||||
url: "https://clsipremium.example.com"
|
url: "https://clsipremium.example.com"
|
||||||
"../../models/Project": Project: @Project = {}
|
"../../models/Project": Project: @Project = {}
|
||||||
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
|
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
|
||||||
|
"./ClsiRequestManager": @ClsiRequestManager
|
||||||
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
|
||||||
"request": @request = {}
|
"request": @request = sinon.stub()
|
||||||
@project_id = "project-id"
|
@project_id = "project-id"
|
||||||
@callback = sinon.stub()
|
@callback = sinon.stub()
|
||||||
|
|
||||||
|
@ -80,15 +84,15 @@ describe "ClsiManager", ->
|
||||||
|
|
||||||
describe "deleteAuxFiles", ->
|
describe "deleteAuxFiles", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@request.del = sinon.stub().callsArg(1)
|
@ClsiManager._makeRequest = sinon.stub().callsArg(2)
|
||||||
|
|
||||||
describe "with the standard compileGroup", ->
|
describe "with the standard compileGroup", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
|
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
|
||||||
|
|
||||||
it "should call the delete method in the standard CLSI", ->
|
it "should call the delete method in the standard CLSI", ->
|
||||||
@request.del
|
@ClsiManager._makeRequest
|
||||||
.calledWith("#{@settings.apis.clsi.url}/project/#{@project_id}")
|
.calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi.url}/project/#{@project_id}"})
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
it "should call the callback", ->
|
it "should call the callback", ->
|
||||||
|
@ -99,8 +103,8 @@ describe "ClsiManager", ->
|
||||||
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "priority"}, @callback
|
@ClsiManager.deleteAuxFiles @project_id, {compileGroup: "priority"}, @callback
|
||||||
|
|
||||||
it "should call the delete method in the CLSI", ->
|
it "should call the delete method in the CLSI", ->
|
||||||
@request.del
|
@ClsiManager._makeRequest
|
||||||
.calledWith("#{@settings.apis.clsi_priority.url}/project/#{@project_id}")
|
.calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi_priority.url}/project/#{@project_id}"})
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
describe "_buildRequest", ->
|
describe "_buildRequest", ->
|
||||||
|
@ -235,15 +239,15 @@ describe "ClsiManager", ->
|
||||||
|
|
||||||
describe "successfully", ->
|
describe "successfully", ->
|
||||||
beforeEach ->
|
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
|
@ClsiManager._postToClsi @project_id, @req, "standard", @callback
|
||||||
|
|
||||||
it 'should send the request to the CLSI', ->
|
it 'should send the request to the CLSI', ->
|
||||||
url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
|
url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
|
||||||
@request.post.calledWith({
|
@ClsiManager._makeRequest.calledWith(@project_id, {
|
||||||
|
method: "POST",
|
||||||
url: url
|
url: url
|
||||||
json: @req
|
json: @req
|
||||||
jar: false
|
|
||||||
}).should.equal true
|
}).should.equal true
|
||||||
|
|
||||||
it "should call the callback with the body and no error", ->
|
it "should call the callback with the body and no error", ->
|
||||||
|
@ -251,7 +255,7 @@ describe "ClsiManager", ->
|
||||||
|
|
||||||
describe "when the CLSI returns an error", ->
|
describe "when the CLSI returns an error", ->
|
||||||
beforeEach ->
|
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
|
@ClsiManager._postToClsi @project_id, @req, "standard", @callback
|
||||||
|
|
||||||
it "should call the callback with the body and the error", ->
|
it "should call the callback with the body and the error", ->
|
||||||
|
@ -259,20 +263,20 @@ describe "ClsiManager", ->
|
||||||
|
|
||||||
describe "when the compiler is priority", ->
|
describe "when the compiler is priority", ->
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
|
@ClsiManager._makeRequest = sinon.stub()
|
||||||
@ClsiManager._postToClsi @project_id, @req, "priority", @callback
|
@ClsiManager._postToClsi @project_id, @req, "priority", @callback
|
||||||
|
|
||||||
it "should use the clsi_priority url", ->
|
it "should use the clsi_priority url", ->
|
||||||
url = "#{@settings.apis.clsi_priority.url}/project/#{@project_id}/compile"
|
url = "#{@settings.apis.clsi_priority.url}/project/#{@project_id}/compile"
|
||||||
@request.post.calledWith({
|
@ClsiManager._makeRequest.calledWith(@project_id, {
|
||||||
|
method: "POST",
|
||||||
url: url
|
url: url
|
||||||
json: @req
|
json: @req
|
||||||
jar: false
|
|
||||||
}).should.equal true
|
}).should.equal true
|
||||||
|
|
||||||
describe "wordCount", ->
|
describe "wordCount", ->
|
||||||
beforeEach ->
|
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._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
|
||||||
@ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url"
|
@ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url"
|
||||||
|
|
||||||
|
@ -281,8 +285,8 @@ describe "ClsiManager", ->
|
||||||
@ClsiManager.wordCount @project_id, false, {}, @callback
|
@ClsiManager.wordCount @project_id, false, {}, @callback
|
||||||
|
|
||||||
it "should call wordCount with root file", ->
|
it "should call wordCount with root file", ->
|
||||||
@request.get
|
@ClsiManager._makeRequest
|
||||||
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
|
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
it "should call the callback", ->
|
it "should call the callback", ->
|
||||||
|
@ -293,8 +297,8 @@ describe "ClsiManager", ->
|
||||||
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
|
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
|
||||||
|
|
||||||
it "should call wordCount with param file", ->
|
it "should call wordCount with param file", ->
|
||||||
@request.get
|
@ClsiManager._makeRequest
|
||||||
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
|
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
||||||
describe "with image", ->
|
describe "with image", ->
|
||||||
|
@ -303,6 +307,6 @@ describe "ClsiManager", ->
|
||||||
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
|
@ClsiManager.wordCount @project_id, "main.tex", {}, @callback
|
||||||
|
|
||||||
it "should call wordCount with file and image", ->
|
it "should call wordCount with file and image", ->
|
||||||
@request.get
|
@ClsiManager._makeRequest
|
||||||
.calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
|
.calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
|
||||||
.should.equal true
|
.should.equal true
|
||||||
|
|
|
@ -0,0 +1,111 @@
|
||||||
|
sinon = require('sinon')
|
||||||
|
chai = require('chai')
|
||||||
|
should = chai.should()
|
||||||
|
expect = chai.expect
|
||||||
|
modulePath = "../../../../app/js/Features/Compile/ClsiRequestManager.js"
|
||||||
|
SandboxedModule = require('sandboxed-module')
|
||||||
|
realRequst = require("request")
|
||||||
|
describe "ClsiRequestManager", ->
|
||||||
|
beforeEach ->
|
||||||
|
@redisMulti =
|
||||||
|
set:sinon.stub()
|
||||||
|
get:sinon.stub()
|
||||||
|
expire:sinon.stub()
|
||||||
|
exec:sinon.stub()
|
||||||
|
self = @
|
||||||
|
@project_id = "123423431321"
|
||||||
|
@request =
|
||||||
|
get: sinon.stub()
|
||||||
|
cookie:realRequst.cookie
|
||||||
|
jar: realRequst.jar
|
||||||
|
@ClsiRequestManager = SandboxedModule.require modulePath, requires:
|
||||||
|
"redis-sharelatex" :
|
||||||
|
createClient: =>
|
||||||
|
auth:->
|
||||||
|
multi: -> return self.redisMulti
|
||||||
|
"settings-sharelatex": @settings =
|
||||||
|
redis:
|
||||||
|
web:"redis.something"
|
||||||
|
apis:
|
||||||
|
clsi:
|
||||||
|
url: "http://clsi.example.com"
|
||||||
|
"request": @request
|
||||||
|
|
||||||
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
describe "getServerId", ->
|
||||||
|
|
||||||
|
it "should call get for the key", (done)->
|
||||||
|
@redisMulti.exec.callsArgWith(0, null, ["clsi-7"])
|
||||||
|
@ClsiRequestManager._getServerId @project_id, (err, serverId)=>
|
||||||
|
@redisMulti.get.calledWith("clsiserver:#{@project_id}").should.equal true
|
||||||
|
serverId.should.equal "clsi-7"
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "should expire the key", (done)->
|
||||||
|
@redisMulti.exec.callsArgWith(0, null, ["clsi-7"])
|
||||||
|
@ClsiRequestManager._getServerId @project_id, (err, serverId)=>
|
||||||
|
@redisMulti.expire.calledWith("clsiserver:#{@project_id}", 60 * 60 * 24 * 7).should.equal true
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "should _getServerIdViaRequest if no key is found", (done)->
|
||||||
|
@ClsiRequestManager._getServerIdViaRequest = sinon.stub().callsArgWith(1)
|
||||||
|
@redisMulti.exec.callsArgWith(0, null, [])
|
||||||
|
@ClsiRequestManager._getServerId @project_id, (err, serverId)=>
|
||||||
|
@ClsiRequestManager._getServerIdViaRequest.calledWith(@project_id).should.equal true
|
||||||
|
done()
|
||||||
|
|
||||||
|
|
||||||
|
describe "_getServerIdViaRequest", ->
|
||||||
|
|
||||||
|
it "should make a request to the clsi", (done)->
|
||||||
|
response = "some data"
|
||||||
|
@request.get.callsArgWith(1, null, response)
|
||||||
|
@ClsiRequestManager.setServerId = sinon.stub().callsArgWith(2)
|
||||||
|
@ClsiRequestManager._getServerIdViaRequest @project_id, (err, serverId)=>
|
||||||
|
args = @ClsiRequestManager.setServerId.args[0]
|
||||||
|
args[0].should.equal @project_id
|
||||||
|
args[1].should.deep.equal response
|
||||||
|
done()
|
||||||
|
|
||||||
|
describe "setServerId", ->
|
||||||
|
|
||||||
|
it "should set the server id with a ttl", (done)->
|
||||||
|
@ClsiRequestManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8")
|
||||||
|
response = "dsadsakj"
|
||||||
|
@redisMulti.exec.callsArgWith(0)
|
||||||
|
@ClsiRequestManager.setServerId @project_id, response, (err)=>
|
||||||
|
@redisMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true
|
||||||
|
@redisMulti.expire.calledWith("clsiserver:#{@project_id}", 60 * 60 * 24 * 7).should.equal true
|
||||||
|
done()
|
||||||
|
|
||||||
|
|
||||||
|
describe "getCookieJar", ->
|
||||||
|
|
||||||
|
it "should return a jar with the cookie set populated from redis", (done)->
|
||||||
|
@ClsiRequestManager._getServerId = sinon.stub().callsArgWith(1, null, "clsi-11")
|
||||||
|
opts = {}
|
||||||
|
@ClsiRequestManager.getCookieJar @project_id, opts, (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()
|
||||||
|
|
||||||
|
|
||||||
|
# describe "_parseServerIdFromResponse", ->
|
||||||
|
# it "take the cookie from the response", (done)->
|
||||||
|
|
||||||
|
# a.should.equal
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,9 @@ describe "CompileController", ->
|
||||||
url: "clsi.example.com"
|
url: "clsi.example.com"
|
||||||
clsi_priority:
|
clsi_priority:
|
||||||
url: "clsi-priority.example.com"
|
url: "clsi-priority.example.com"
|
||||||
|
@jar = {cookie:"stuff"}
|
||||||
|
@ClsiRequestManager =
|
||||||
|
getCookieJar:sinon.stub().callsArgWith(1, null, @jar)
|
||||||
@CompileController = SandboxedModule.require modulePath, requires:
|
@CompileController = SandboxedModule.require modulePath, requires:
|
||||||
"settings-sharelatex": @settings
|
"settings-sharelatex": @settings
|
||||||
"request": @request = sinon.stub()
|
"request": @request = sinon.stub()
|
||||||
|
@ -33,6 +36,7 @@ describe "CompileController", ->
|
||||||
"./ClsiManager": @ClsiManager
|
"./ClsiManager": @ClsiManager
|
||||||
"../Authentication/AuthenticationController": @AuthenticationController = {}
|
"../Authentication/AuthenticationController": @AuthenticationController = {}
|
||||||
"../../infrastructure/RateLimiter":@RateLimiter
|
"../../infrastructure/RateLimiter":@RateLimiter
|
||||||
|
"./ClsiRequestManager":@ClsiRequestManager
|
||||||
@project_id = "project-id"
|
@project_id = "project-id"
|
||||||
@user =
|
@user =
|
||||||
features:
|
features:
|
||||||
|
@ -182,6 +186,7 @@ describe "CompileController", ->
|
||||||
it "should open a request to the CLSI", ->
|
it "should open a request to the CLSI", ->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -204,6 +209,7 @@ describe "CompileController", ->
|
||||||
it "should proxy to the priority url if the user has the feature", ()->
|
it "should proxy to the priority url if the user has the feature", ()->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -218,6 +224,7 @@ describe "CompileController", ->
|
||||||
it "should open a request to the CLSI", ->
|
it "should open a request to the CLSI", ->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -240,6 +247,7 @@ describe "CompileController", ->
|
||||||
it "should proxy to the priority url if the user has the feature", ()->
|
it "should proxy to the priority url if the user has the feature", ()->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -254,6 +262,7 @@ describe "CompileController", ->
|
||||||
it "should proxy to the standard url", ()->
|
it "should proxy to the standard url", ()->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -269,6 +278,7 @@ describe "CompileController", ->
|
||||||
it "should proxy to the standard url without the build parameter", ()->
|
it "should proxy to the standard url without the build parameter", ()->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -286,6 +296,7 @@ describe "CompileController", ->
|
||||||
it "should open a request to the CLSI", ->
|
it "should open a request to the CLSI", ->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -313,6 +324,7 @@ describe "CompileController", ->
|
||||||
it "should proxy to the priority url if the user has the feature", ()->
|
it "should proxy to the priority url if the user has the feature", ()->
|
||||||
@request
|
@request
|
||||||
.calledWith(
|
.calledWith(
|
||||||
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
||||||
timeout: 60 * 1000
|
timeout: 60 * 1000
|
||||||
|
@ -331,8 +343,8 @@ describe "CompileController", ->
|
||||||
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
||||||
|
|
||||||
it "should proxy to the standard url with the build parameter", ()->
|
it "should proxy to the standard url with the build parameter", ()->
|
||||||
@request
|
@request.calledWith(
|
||||||
.calledWith(
|
jar:@jar
|
||||||
method: @req.method
|
method: @req.method
|
||||||
qs: {build: 1234}
|
qs: {build: 1234}
|
||||||
url: "#{@settings.apis.clsi.url}#{@url}",
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
||||||
|
|
Loading…
Reference in a new issue