CompileControllerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. assert = require("chai").assert
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/Features/Compile/CompileController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. MockRequest = require "../helpers/MockRequest"
  9. MockResponse = require "../helpers/MockResponse"
  10. describe "CompileController", ->
  11. beforeEach ->
  12. @CompileManager =
  13. compile: sinon.stub()
  14. @ClsiManager = {}
  15. @UserGetter =
  16. getUser:sinon.stub()
  17. @CompileController = SandboxedModule.require modulePath, requires:
  18. "settings-sharelatex": @settings =
  19. apis:
  20. clsi:
  21. url: "clsi.example.com"
  22. clsi_priority:
  23. url: "clsi-priority.example.com"
  24. "request": @request = sinon.stub()
  25. "../../models/Project": Project: @Project = {}
  26. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  27. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  28. "./CompileManager":@CompileManager
  29. "../User/UserGetter":@UserGetter
  30. "./ClsiManager": @ClsiManager
  31. "../Authentication/AuthenticationController": @AuthenticationController = {}
  32. @project_id = "project-id"
  33. @user =
  34. features:
  35. compileGroup: "premium"
  36. compileTimeout: 100
  37. @next = sinon.stub()
  38. @req = new MockRequest()
  39. @res = new MockResponse()
  40. describe "compile", ->
  41. describe "when not an auto compile", ->
  42. beforeEach ->
  43. @req.params =
  44. Project_id: @project_id
  45. @req.session = {}
  46. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  47. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"], @output = "mock-output")
  48. @CompileController.compile @req, @res, @next
  49. it "should look up the user id", ->
  50. @AuthenticationController.getLoggedInUserId
  51. .calledWith(@req)
  52. .should.equal true
  53. it "should do the compile without the auto compile flag", ->
  54. @CompileManager.compile
  55. .calledWith(@project_id, @user_id, { isAutoCompile: false })
  56. .should.equal true
  57. it "should set the content-type of the response to application/json", ->
  58. @res.contentType
  59. .calledWith("application/json")
  60. .should.equal true
  61. it "should send a successful response reporting the status and files", ->
  62. @res.statusCode.should.equal 200
  63. @res.body.should.equal JSON.stringify({
  64. status: @status
  65. outputFiles: @outputFiles
  66. })
  67. describe "when an auto compile", ->
  68. beforeEach ->
  69. @req.params =
  70. Project_id: @project_id
  71. @req.query =
  72. auto_compile: "true"
  73. @AuthenticationController.getLoggedInUserId = sinon.stub().callsArgWith(1, null, @user_id = "mock-user-id")
  74. @CompileManager.compile = sinon.stub().callsArgWith(3, null, @status = "success", @outputFiles = ["mock-output-files"])
  75. @CompileController.compile @req, @res, @next
  76. it "should do the compile with the auto compile flag", ->
  77. @CompileManager.compile
  78. .calledWith(@project_id, @user_id, { isAutoCompile: true })
  79. .should.equal true
  80. describe "downloadPdf", ->
  81. beforeEach ->
  82. @req.params =
  83. Project_id: @project_id
  84. @project =
  85. getSafeProjectName: () => @safe_name = "safe-name"
  86. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  87. describe "when downloading for embedding", ->
  88. beforeEach ->
  89. @project.useClsi2 = true
  90. @CompileController.proxyToClsi = sinon.stub()
  91. @CompileController.downloadPdf(@req, @res, @next)
  92. it "should look up the project", ->
  93. @Project.findById
  94. .calledWith(@project_id, {name: 1})
  95. .should.equal true
  96. it "should set the content-type of the response to application/pdf", ->
  97. @res.contentType
  98. .calledWith("application/pdf")
  99. .should.equal true
  100. it "should set the content-disposition header with the project name", ->
  101. @res.header
  102. .calledWith("Content-Disposition", "filename=#{@safe_name}.pdf")
  103. .should.equal true
  104. it "should increment the pdf-downloads metric", ->
  105. @Metrics.inc
  106. .calledWith("pdf-downloads")
  107. .should.equal true
  108. it "should proxy the PDF from the CLSI", ->
  109. @CompileController.proxyToClsi
  110. .calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res, @next)
  111. .should.equal true
  112. describe "proxyToClsi", ->
  113. beforeEach ->
  114. @request.returns(@proxy = {
  115. pipe: sinon.stub()
  116. on: sinon.stub()
  117. })
  118. @upstream =
  119. statusCode: 204
  120. headers: { "mock": "header" }
  121. @req.method = "mock-method"
  122. @req.headers = {
  123. 'Mock': 'Headers',
  124. 'Range': '123-456'
  125. 'If-Range': 'abcdef'
  126. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  127. }
  128. describe "old pdf viewer", ->
  129. describe "user with standard priority", ->
  130. beforeEach ->
  131. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  132. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  133. it "should open a request to the CLSI", ->
  134. @request
  135. .calledWith(
  136. method: @req.method
  137. url: "#{@settings.apis.clsi.url}#{@url}",
  138. timeout: 60 * 1000
  139. )
  140. .should.equal true
  141. it "should pass the request on to the client", ->
  142. @proxy.pipe
  143. .calledWith(@res)
  144. .should.equal true
  145. it "should bind an error handle to the request proxy", ->
  146. @proxy.on.calledWith("error").should.equal true
  147. describe "user with priority compile", ->
  148. beforeEach ->
  149. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  150. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  151. it "should proxy to the priority url if the user has the feature", ()->
  152. @request
  153. .calledWith(
  154. method: @req.method
  155. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  156. timeout: 60 * 1000
  157. )
  158. .should.equal true
  159. describe "user with standard priority via query string", ->
  160. beforeEach ->
  161. @req.query = {compileGroup: 'standard'}
  162. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  163. it "should open a request to the CLSI", ->
  164. @request
  165. .calledWith(
  166. method: @req.method
  167. url: "#{@settings.apis.clsi.url}#{@url}",
  168. timeout: 60 * 1000
  169. )
  170. .should.equal true
  171. it "should pass the request on to the client", ->
  172. @proxy.pipe
  173. .calledWith(@res)
  174. .should.equal true
  175. it "should bind an error handle to the request proxy", ->
  176. @proxy.on.calledWith("error").should.equal true
  177. describe "user with priority compile via query string", ->
  178. beforeEach ->
  179. @req.query = {compileGroup: 'priority'}
  180. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  181. it "should proxy to the priority url if the user has the feature", ()->
  182. @request
  183. .calledWith(
  184. method: @req.method
  185. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  186. timeout: 60 * 1000
  187. )
  188. .should.equal true
  189. describe "user with non-existent priority via query string", ->
  190. beforeEach ->
  191. @req.query = {compileGroup: 'foobar'}
  192. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  193. it "should proxy to the standard url", ()->
  194. @request
  195. .calledWith(
  196. method: @req.method
  197. url: "#{@settings.apis.clsi.url}#{@url}",
  198. timeout: 60 * 1000
  199. )
  200. .should.equal true
  201. describe "user with build parameter via query string", ->
  202. beforeEach ->
  203. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  204. @req.query = {build: 1234}
  205. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  206. it "should proxy to the standard url without the build parameter", ()->
  207. @request
  208. .calledWith(
  209. method: @req.method
  210. url: "#{@settings.apis.clsi.url}#{@url}",
  211. timeout: 60 * 1000
  212. )
  213. .should.equal true
  214. describe "new pdf viewer", ->
  215. beforeEach ->
  216. @req.query = {pdfng: true}
  217. describe "user with standard priority", ->
  218. beforeEach ->
  219. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  220. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  221. it "should open a request to the CLSI", ->
  222. @request
  223. .calledWith(
  224. method: @req.method
  225. url: "#{@settings.apis.clsi.url}#{@url}",
  226. timeout: 60 * 1000
  227. headers: {
  228. 'Range': '123-456'
  229. 'If-Range': 'abcdef'
  230. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  231. }
  232. )
  233. .should.equal true
  234. it "should pass the request on to the client", ->
  235. @proxy.pipe
  236. .calledWith(@res)
  237. .should.equal true
  238. it "should bind an error handle to the request proxy", ->
  239. @proxy.on.calledWith("error").should.equal true
  240. describe "user with priority compile", ->
  241. beforeEach ->
  242. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  243. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  244. it "should proxy to the priority url if the user has the feature", ()->
  245. @request
  246. .calledWith(
  247. method: @req.method
  248. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  249. timeout: 60 * 1000
  250. headers: {
  251. 'Range': '123-456'
  252. 'If-Range': 'abcdef'
  253. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  254. }
  255. )
  256. .should.equal true
  257. describe "user with build parameter via query string", ->
  258. beforeEach ->
  259. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  260. @req.query = {build: 1234, pdfng: true}
  261. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  262. it "should proxy to the standard url with the build parameter", ()->
  263. @request
  264. .calledWith(
  265. method: @req.method
  266. qs: {build: 1234}
  267. url: "#{@settings.apis.clsi.url}#{@url}",
  268. timeout: 60 * 1000
  269. headers: {
  270. 'Range': '123-456'
  271. 'If-Range': 'abcdef'
  272. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  273. }
  274. )
  275. .should.equal true
  276. describe "deleteAuxFiles", ->
  277. beforeEach ->
  278. @CompileManager.deleteAuxFiles = sinon.stub().callsArg(1)
  279. @req.params =
  280. Project_id: @project_id
  281. @res.sendStatus = sinon.stub()
  282. @CompileController.deleteAuxFiles @req, @res, @next
  283. it "should proxy to the CLSI", ->
  284. @CompileManager.deleteAuxFiles
  285. .calledWith(@project_id)
  286. .should.equal true
  287. it "should return a 200", ->
  288. @res.sendStatus
  289. .calledWith(200)
  290. .should.equal true
  291. describe "compileAndDownloadPdf", ->
  292. beforeEach ->
  293. @req =
  294. params:
  295. project_id:@project_id
  296. @CompileManager.compile.callsArgWith(3)
  297. @CompileController.proxyToClsi = sinon.stub()
  298. @res =
  299. send:=>
  300. it "should call compile in the compile manager", (done)->
  301. @CompileController.compileAndDownloadPdf @req, @res
  302. @CompileManager.compile.calledWith(@project_id).should.equal true
  303. done()
  304. it "should proxy the res to the clsi with correct url", (done)->
  305. @CompileController.compileAndDownloadPdf @req, @res
  306. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  307. done()