CompileControllerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "new pdf viewer", ->
  202. beforeEach ->
  203. @req.query = {pdfng: true}
  204. describe "user with standard priority", ->
  205. beforeEach ->
  206. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  207. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  208. it "should open a request to the CLSI", ->
  209. @request
  210. .calledWith(
  211. method: @req.method
  212. url: "#{@settings.apis.clsi.url}#{@url}",
  213. timeout: 60 * 1000
  214. headers: {
  215. 'Range': '123-456'
  216. 'If-Range': 'abcdef'
  217. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  218. }
  219. )
  220. .should.equal true
  221. it "should pass the request on to the client", ->
  222. @proxy.pipe
  223. .calledWith(@res)
  224. .should.equal true
  225. it "should bind an error handle to the request proxy", ->
  226. @proxy.on.calledWith("error").should.equal true
  227. describe "user with priority compile", ->
  228. beforeEach ->
  229. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  230. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  231. it "should proxy to the priority url if the user has the feature", ()->
  232. @request
  233. .calledWith(
  234. method: @req.method
  235. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  236. timeout: 60 * 1000
  237. headers: {
  238. 'Range': '123-456'
  239. 'If-Range': 'abcdef'
  240. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  241. }
  242. )
  243. .should.equal true
  244. describe "deleteAuxFiles", ->
  245. beforeEach ->
  246. @CompileManager.deleteAuxFiles = sinon.stub().callsArg(1)
  247. @req.params =
  248. Project_id: @project_id
  249. @res.send = sinon.stub()
  250. @CompileController.deleteAuxFiles @req, @res, @next
  251. it "should proxy to the CLSI", ->
  252. @CompileManager.deleteAuxFiles
  253. .calledWith(@project_id)
  254. .should.equal true
  255. it "should return a 200", ->
  256. @res.send
  257. .calledWith(200)
  258. .should.equal true
  259. describe "compileAndDownloadPdf", ->
  260. beforeEach ->
  261. @req =
  262. params:
  263. project_id:@project_id
  264. @CompileManager.compile.callsArgWith(3)
  265. @CompileController.proxyToClsi = sinon.stub()
  266. @res =
  267. send:=>
  268. it "should call compile in the compile manager", (done)->
  269. @CompileController.compileAndDownloadPdf @req, @res
  270. @CompileManager.compile.calledWith(@project_id).should.equal true
  271. done()
  272. it "should proxy the res to the clsi with correct url", (done)->
  273. @CompileController.compileAndDownloadPdf @req, @res
  274. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  275. done()