CompileControllerTests.coffee 12 KB

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