CompileControllerTests.coffee 13 KB

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