CompileControllerTests.coffee 12 KB

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