CompileControllerTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. it "should check the rate limiter", ->
  115. @RateLimiter.addCount.args[0][0].throttle.should.equal 500
  116. describe "when the pdf is not going to be used in pdfjs viewer", ->
  117. it "should check the rate limiter when pdfng is not set", (done)->
  118. @req.query = {}
  119. @RateLimiter.addCount.callsArgWith(1, null, true)
  120. @CompileController.proxyToClsi = (project_id, url)=>
  121. @RateLimiter.addCount.args[0][0].throttle.should.equal 30
  122. done()
  123. @CompileController.downloadPdf @req, @res
  124. it "should check the rate limiter when pdfng is false", (done)->
  125. @req.query = {pdfng:false}
  126. @RateLimiter.addCount.callsArgWith(1, null, true)
  127. @CompileController.proxyToClsi = (project_id, url)=>
  128. @RateLimiter.addCount.args[0][0].throttle.should.equal 30
  129. done()
  130. @CompileController.downloadPdf @req, @res
  131. describe "proxyToClsi", ->
  132. beforeEach ->
  133. @request.returns(@proxy = {
  134. pipe: sinon.stub()
  135. on: sinon.stub()
  136. })
  137. @upstream =
  138. statusCode: 204
  139. headers: { "mock": "header" }
  140. @req.method = "mock-method"
  141. @req.headers = {
  142. 'Mock': 'Headers',
  143. 'Range': '123-456'
  144. 'If-Range': 'abcdef'
  145. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  146. }
  147. describe "old pdf viewer", ->
  148. describe "user with standard priority", ->
  149. beforeEach ->
  150. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  151. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  152. it "should open a request to the CLSI", ->
  153. @request
  154. .calledWith(
  155. method: @req.method
  156. url: "#{@settings.apis.clsi.url}#{@url}",
  157. timeout: 60 * 1000
  158. )
  159. .should.equal true
  160. it "should pass the request on to the client", ->
  161. @proxy.pipe
  162. .calledWith(@res)
  163. .should.equal true
  164. it "should bind an error handle to the request proxy", ->
  165. @proxy.on.calledWith("error").should.equal true
  166. describe "user with priority compile", ->
  167. beforeEach ->
  168. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  169. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  170. it "should proxy to the priority url if the user has the feature", ()->
  171. @request
  172. .calledWith(
  173. method: @req.method
  174. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  175. timeout: 60 * 1000
  176. )
  177. .should.equal true
  178. describe "user with standard priority via query string", ->
  179. beforeEach ->
  180. @req.query = {compileGroup: 'standard'}
  181. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  182. it "should open a request to the CLSI", ->
  183. @request
  184. .calledWith(
  185. method: @req.method
  186. url: "#{@settings.apis.clsi.url}#{@url}",
  187. timeout: 60 * 1000
  188. )
  189. .should.equal true
  190. it "should pass the request on to the client", ->
  191. @proxy.pipe
  192. .calledWith(@res)
  193. .should.equal true
  194. it "should bind an error handle to the request proxy", ->
  195. @proxy.on.calledWith("error").should.equal true
  196. describe "user with priority compile via query string", ->
  197. beforeEach ->
  198. @req.query = {compileGroup: 'priority'}
  199. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  200. it "should proxy to the priority url if the user has the feature", ()->
  201. @request
  202. .calledWith(
  203. method: @req.method
  204. url: "#{@settings.apis.clsi_priority.url}#{@url}",
  205. timeout: 60 * 1000
  206. )
  207. .should.equal true
  208. describe "user with non-existent priority via query string", ->
  209. beforeEach ->
  210. @req.query = {compileGroup: 'foobar'}
  211. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  212. it "should proxy to the standard url", ()->
  213. @request
  214. .calledWith(
  215. method: @req.method
  216. url: "#{@settings.apis.clsi.url}#{@url}",
  217. timeout: 60 * 1000
  218. )
  219. .should.equal true
  220. describe "user with build parameter via query string", ->
  221. beforeEach ->
  222. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  223. @req.query = {build: 1234}
  224. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  225. it "should proxy to the standard url without the build parameter", ()->
  226. @request
  227. .calledWith(
  228. method: @req.method
  229. url: "#{@settings.apis.clsi.url}#{@url}",
  230. timeout: 60 * 1000
  231. )
  232. .should.equal true
  233. describe "new pdf viewer", ->
  234. beforeEach ->
  235. @req.query = {pdfng: true}
  236. describe "user with standard priority", ->
  237. beforeEach ->
  238. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  239. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  240. it "should open a request to the CLSI", ->
  241. @request
  242. .calledWith(
  243. method: @req.method
  244. url: "#{@settings.apis.clsi.url}#{@url}",
  245. timeout: 60 * 1000
  246. headers: {
  247. 'Range': '123-456'
  248. 'If-Range': 'abcdef'
  249. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  250. }
  251. )
  252. .should.equal true
  253. it "should pass the request on to the client", ->
  254. @proxy.pipe
  255. .calledWith(@res)
  256. .should.equal true
  257. it "should bind an error handle to the request proxy", ->
  258. @proxy.on.calledWith("error").should.equal true
  259. describe "user with priority compile", ->
  260. beforeEach ->
  261. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "priority"})
  262. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  263. it "should proxy to the priority url if the user has the feature", ()->
  264. @request
  265. .calledWith(
  266. method: @req.method
  267. url: "#{@settings.apis.clsi_priority.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 "user with build parameter via query string", ->
  277. beforeEach ->
  278. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, {compileGroup: "standard"})
  279. @req.query = {build: 1234, pdfng: true}
  280. @CompileController.proxyToClsi(@project_id, @url = "/test", @req, @res, @next)
  281. it "should proxy to the standard url with the build parameter", ()->
  282. @request
  283. .calledWith(
  284. method: @req.method
  285. qs: {build: 1234}
  286. url: "#{@settings.apis.clsi.url}#{@url}",
  287. timeout: 60 * 1000
  288. headers: {
  289. 'Range': '123-456'
  290. 'If-Range': 'abcdef'
  291. 'If-Modified-Since': 'Mon, 15 Dec 2014 15:23:56 GMT'
  292. }
  293. )
  294. .should.equal true
  295. describe "deleteAuxFiles", ->
  296. beforeEach ->
  297. @CompileManager.deleteAuxFiles = sinon.stub().callsArg(1)
  298. @req.params =
  299. Project_id: @project_id
  300. @res.sendStatus = sinon.stub()
  301. @CompileController.deleteAuxFiles @req, @res, @next
  302. it "should proxy to the CLSI", ->
  303. @CompileManager.deleteAuxFiles
  304. .calledWith(@project_id)
  305. .should.equal true
  306. it "should return a 200", ->
  307. @res.sendStatus
  308. .calledWith(200)
  309. .should.equal true
  310. describe "compileAndDownloadPdf", ->
  311. beforeEach ->
  312. @req =
  313. params:
  314. project_id:@project_id
  315. @CompileManager.compile.callsArgWith(3)
  316. @CompileController.proxyToClsi = sinon.stub()
  317. @res =
  318. send:=>
  319. it "should call compile in the compile manager", (done)->
  320. @CompileController.compileAndDownloadPdf @req, @res
  321. @CompileManager.compile.calledWith(@project_id).should.equal true
  322. done()
  323. it "should proxy the res to the clsi with correct url", (done)->
  324. @CompileController.compileAndDownloadPdf @req, @res
  325. @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true
  326. done()
  327. describe "wordCount", ->
  328. beforeEach ->
  329. @CompileManager.wordCount = sinon.stub().callsArgWith(2, null, {content:"body"})
  330. @req.params =
  331. Project_id: @project_id
  332. @res.send = sinon.stub()
  333. @res.contentType = sinon.stub()
  334. @CompileController.wordCount @req, @res, @next
  335. it "should proxy to the CLSI", ->
  336. @CompileManager.wordCount
  337. .calledWith(@project_id, false)
  338. .should.equal true
  339. it "should return a 200 and body", ->
  340. @res.send
  341. .calledWith({content:"body"})
  342. .should.equal true