CompileControllerTests.coffee 13 KB

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