2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
2014-10-16 11:34:21 -04:00
|
|
|
assert = require("chai").assert
|
2014-02-12 05:23:40 -05:00
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/Features/Compile/CompileController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
|
|
|
|
|
|
|
describe "CompileController", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager =
|
|
|
|
compile: sinon.stub()
|
2014-10-16 11:02:31 -04:00
|
|
|
@ClsiManager = {}
|
2014-10-16 11:34:21 -04:00
|
|
|
@UserGetter =
|
|
|
|
getUser:sinon.stub()
|
2015-10-30 07:35:11 -04:00
|
|
|
@RateLimiter = {addCount:sinon.stub()}
|
|
|
|
@settings =
|
|
|
|
apis:
|
|
|
|
clsi:
|
|
|
|
url: "clsi.example.com"
|
|
|
|
clsi_priority:
|
|
|
|
url: "clsi-priority.example.com"
|
2014-02-12 05:23:40 -05:00
|
|
|
@CompileController = SandboxedModule.require modulePath, requires:
|
2015-10-30 07:35:11 -04:00
|
|
|
"settings-sharelatex": @settings
|
2014-03-27 13:00:41 -04:00
|
|
|
"request": @request = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
"../../models/Project": Project: @Project = {}
|
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
|
|
|
"../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
|
|
|
|
"./CompileManager":@CompileManager
|
2014-10-16 11:34:21 -04:00
|
|
|
"../User/UserGetter":@UserGetter
|
2014-05-19 11:10:41 -04:00
|
|
|
"./ClsiManager": @ClsiManager
|
2014-05-19 10:28:35 -04:00
|
|
|
"../Authentication/AuthenticationController": @AuthenticationController = {}
|
2015-10-30 07:35:11 -04:00
|
|
|
"../../infrastructure/RateLimiter":@RateLimiter
|
2014-02-12 05:23:40 -05:00
|
|
|
@project_id = "project-id"
|
2014-10-16 11:34:21 -04:00
|
|
|
@user =
|
|
|
|
features:
|
|
|
|
compileGroup: "premium"
|
|
|
|
compileTimeout: 100
|
2014-02-12 05:23:40 -05:00
|
|
|
@next = sinon.stub()
|
|
|
|
@req = new MockRequest()
|
|
|
|
@res = new MockResponse()
|
|
|
|
|
2014-05-19 10:28:35 -04:00
|
|
|
describe "compile", ->
|
2014-10-17 08:23:29 -04:00
|
|
|
|
2014-05-19 10:28:35 -04:00
|
|
|
describe "when not an auto compile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
2014-10-21 04:34:58 -04:00
|
|
|
@req.session = {}
|
2014-05-19 10:28:35 -04:00
|
|
|
@AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
|
2014-11-28 09:26:21 -05:00
|
|
|
@CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"], @output = "mock-output")
|
2014-05-19 10:28:35 -04:00
|
|
|
@CompileController.compile @req, @res, @next
|
|
|
|
|
|
|
|
it "should look up the user id", ->
|
|
|
|
@AuthenticationController.getLoggedInUserId
|
|
|
|
.calledWith(@req)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should do the compile without the auto compile flag", ->
|
|
|
|
@CompileManager.compile
|
2014-11-28 09:26:21 -05:00
|
|
|
.calledWith(@project_id, @user_id, { isAutoCompile: false })
|
2014-05-19 10:28:35 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should set the content-type of the response to application/json", ->
|
|
|
|
@res.contentType
|
|
|
|
.calledWith("application/json")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send a successful response reporting the status and files", ->
|
|
|
|
@res.statusCode.should.equal 200
|
|
|
|
@res.body.should.equal JSON.stringify({
|
|
|
|
status: @status
|
|
|
|
outputFiles: @outputFiles
|
|
|
|
})
|
|
|
|
|
|
|
|
describe "when an auto compile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@req.query =
|
|
|
|
auto_compile: "true"
|
|
|
|
@AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
|
|
|
|
@CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
|
|
|
|
@CompileController.compile @req, @res, @next
|
|
|
|
|
|
|
|
it "should do the compile with the auto compile flag", ->
|
2014-11-28 09:26:21 -05:00
|
|
|
@CompileManager.compile
|
|
|
|
.calledWith(@project_id, @user_id, { isAutoCompile: true })
|
|
|
|
.should.equal true
|
2014-05-19 10:28:35 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "downloadPdf", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@project =
|
|
|
|
getSafeProjectName: () => @safe_name = "safe-name"
|
|
|
|
|
2015-10-30 07:35:11 -04:00
|
|
|
@req.query = {pdfng:true}
|
2014-02-12 05:23:40 -05:00
|
|
|
@Project.findById = sinon.stub().callsArgWith(2, null, @project)
|
|
|
|
|
|
|
|
describe "when downloading for embedding", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileController.proxyToClsi = sinon.stub()
|
2015-10-30 07:35:11 -04:00
|
|
|
@RateLimiter.addCount.callsArgWith(1, null, true)
|
2014-02-12 05:23:40 -05:00
|
|
|
@CompileController.downloadPdf(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should look up the project", ->
|
|
|
|
@Project.findById
|
|
|
|
.calledWith(@project_id, {name: 1})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should set the content-type of the response to application/pdf", ->
|
|
|
|
@res.contentType
|
|
|
|
.calledWith("application/pdf")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should set the content-disposition header with the project name", ->
|
|
|
|
@res.header
|
|
|
|
.calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should increment the pdf-downloads metric", ->
|
|
|
|
@Metrics.inc
|
|
|
|
.calledWith("pdf-downloads")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should proxy the PDF from the CLSI", ->
|
2015-10-30 07:35:11 -04:00
|
|
|
@CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res, @next).should.equal true
|
|
|
|
|
|
|
|
describe "when the pdf is not going to be used in pdfjs viewer", ->
|
|
|
|
|
|
|
|
it "should check the rate limiter when pdfng is not set", (done)->
|
|
|
|
@req.query = {}
|
|
|
|
@RateLimiter.addCount.callsArgWith(1, null, true)
|
|
|
|
@CompileController.proxyToClsi = (project_id, url)=>
|
2015-11-25 06:07:53 -05:00
|
|
|
@RateLimiter.addCount.args[0][0].throttle.should.equal 1000
|
2015-10-30 07:35:11 -04:00
|
|
|
done()
|
|
|
|
@CompileController.downloadPdf @req, @res
|
|
|
|
|
|
|
|
|
|
|
|
it "should check the rate limiter when pdfng is false", (done)->
|
|
|
|
@req.query = {pdfng:false}
|
|
|
|
@RateLimiter.addCount.callsArgWith(1, null, true)
|
|
|
|
@CompileController.proxyToClsi = (project_id, url)=>
|
2015-11-25 06:08:04 -05:00
|
|
|
@RateLimiter.addCount.args[0][0].throttle.should.equal 1000
|
2015-10-30 07:35:11 -04:00
|
|
|
done()
|
|
|
|
@CompileController.downloadPdf @req, @res
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "proxyToClsi", ->
|
|
|
|
beforeEach ->
|
2014-03-27 13:00:41 -04:00
|
|
|
@request.returns(@proxy = {
|
2014-02-12 05:23:40 -05:00
|
|
|
pipe: sinon.stub()
|
|
|
|
on: sinon.stub()
|
|
|
|
})
|
2014-03-27 13:00:41 -04:00
|
|
|
@upstream =
|
|
|
|
statusCode: 204
|
|
|
|
headers: { "mock": "header" }
|
|
|
|
@req.method = "mock-method"
|
2014-12-12 10:43:21 -05:00
|
|
|
@req.headers = {
|
|
|
|
'Mock': 'Headers',
|
2014-12-16 04:44:25 -05:00
|
|
|
'Range': '123-456'
|
|
|
|
'If-Range': 'abcdef'
|
|
|
|
'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
|
2014-12-12 10:43:21 -05:00
|
|
|
}
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
describe "old pdf viewer", ->
|
|
|
|
describe "user with standard priority", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
it "should open a request to the CLSI", ->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
it "should pass the request on to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should bind an error handle to the request proxy", ->
|
|
|
|
@proxy.on.calledWith("error").should.equal true
|
|
|
|
|
|
|
|
describe "user with priority compile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should proxy to the priority url if the user has the feature", ()->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "user with standard priority via query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.query = {compileGroup: 'standard'}
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should open a request to the CLSI", ->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should pass the request on to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should bind an error handle to the request proxy", ->
|
|
|
|
@proxy.on.calledWith("error").should.equal true
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
describe "user with priority compile via query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.query = {compileGroup: 'priority'}
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
it "should proxy to the priority url if the user has the feature", ()->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-12-16 04:44:25 -05:00
|
|
|
describe "user with non-existent priority via query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.query = {compileGroup: 'foobar'}
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should proxy to the standard url", ()->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
|
|
|
|
2015-02-26 11:19:36 -05:00
|
|
|
describe "user with build parameter via query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
|
|
|
|
@req.query = {build: 1234}
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
2015-03-02 10:33:45 -05:00
|
|
|
it "should proxy to the standard url without the build parameter", ()->
|
2015-02-26 11:19:36 -05:00
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-12-16 04:44:25 -05:00
|
|
|
|
|
|
|
describe "new pdf viewer", ->
|
2014-10-21 04:34:58 -04:00
|
|
|
beforeEach ->
|
2014-12-16 04:44:25 -05:00
|
|
|
@req.query = {pdfng: true}
|
|
|
|
describe "user with standard priority", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should open a request to the CLSI", ->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
headers: {
|
|
|
|
'Range': '123-456'
|
|
|
|
'If-Range': 'abcdef'
|
|
|
|
'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should pass the request on to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should bind an error handle to the request proxy", ->
|
|
|
|
@proxy.on.calledWith("error").should.equal true
|
|
|
|
|
|
|
|
describe "user with priority compile", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should proxy to the priority url if the user has the feature", ()->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
url: "#{@settings.apis.clsi_priority.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
headers: {
|
|
|
|
'Range': '123-456'
|
|
|
|
'If-Range': 'abcdef'
|
|
|
|
'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2015-03-02 10:33:45 -05:00
|
|
|
describe "user with build parameter via query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
|
|
|
|
@req.query = {build: 1234, pdfng: true}
|
|
|
|
@CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
|
|
|
|
|
|
|
|
it "should proxy to the standard url with the build parameter", ()->
|
|
|
|
@request
|
|
|
|
.calledWith(
|
|
|
|
method: @req.method
|
|
|
|
qs: {build: 1234}
|
|
|
|
url: "#{@settings.apis.clsi.url}#{@url}",
|
|
|
|
timeout: 60 * 1000
|
|
|
|
headers: {
|
|
|
|
'Range': '123-456'
|
|
|
|
'If-Range': 'abcdef'
|
|
|
|
'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-03-27 13:00:41 -04:00
|
|
|
describe "deleteAuxFiles", ->
|
|
|
|
beforeEach ->
|
2014-12-01 07:19:01 -05:00
|
|
|
@CompileManager.deleteAuxFiles = sinon.stub().callsArg(1)
|
2014-03-27 13:00:41 -04:00
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = sinon.stub()
|
2014-03-27 13:00:41 -04:00
|
|
|
@CompileController.deleteAuxFiles @req, @res, @next
|
|
|
|
|
|
|
|
it "should proxy to the CLSI", ->
|
2014-12-01 07:19:01 -05:00
|
|
|
@CompileManager.deleteAuxFiles
|
2014-05-19 11:10:41 -04:00
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a 200", ->
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus
|
2014-05-19 11:10:41 -04:00
|
|
|
.calledWith(200)
|
2014-03-27 13:00:41 -04:00
|
|
|
.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "compileAndDownloadPdf", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req =
|
|
|
|
params:
|
|
|
|
project_id:@project_id
|
|
|
|
@CompileManager.compile.callsArgWith(3)
|
|
|
|
@CompileController.proxyToClsi = sinon.stub()
|
|
|
|
@res =
|
|
|
|
send:=>
|
2014-10-21 04:34:58 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it "should call compile in the compile manager", (done)->
|
|
|
|
@CompileController.compileAndDownloadPdf @req, @res
|
|
|
|
@CompileManager.compile.calledWith(@project_id).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it "should proxy the res to the clsi with correct url", (done)->
|
|
|
|
@CompileController.compileAndDownloadPdf @req, @res
|
2014-11-28 09:26:21 -05:00
|
|
|
@CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
2015-09-11 09:21:05 -04:00
|
|
|
|
|
|
|
describe "wordCount", ->
|
|
|
|
beforeEach ->
|
|
|
|
@CompileManager.wordCount = sinon.stub().callsArgWith(2, null, {content:"body"})
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
@res.send = sinon.stub()
|
|
|
|
@res.contentType = sinon.stub()
|
|
|
|
@CompileController.wordCount @req, @res, @next
|
|
|
|
|
|
|
|
it "should proxy to the CLSI", ->
|
|
|
|
@CompileManager.wordCount
|
|
|
|
.calledWith(@project_id, false)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a 200 and body", ->
|
|
|
|
@res.send
|
2015-09-14 10:28:34 -04:00
|
|
|
.calledWith({content:"body"})
|
2015-09-11 09:21:05 -04:00
|
|
|
.should.equal true
|