CompileControllerTests.coffee 13 KB

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