ClsiManagerTests.coffee 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/ClsiManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "ClsiManager", ->
  8. beforeEach ->
  9. @jar = {cookie:"stuff"}
  10. @ClsiCookieManager =
  11. getCookieJar: sinon.stub().callsArgWith(1, null, @jar)
  12. setServerId: sinon.stub().callsArgWith(2)
  13. _getServerId:sinon.stub()
  14. @ClsiStateManager =
  15. computeHash: sinon.stub().callsArgWith(2, null, "01234567890abcdef")
  16. @ClsiFormatChecker =
  17. checkRecoursesForProblems:sinon.stub().callsArgWith(1)
  18. @ClsiManager = SandboxedModule.require modulePath, requires:
  19. "settings-sharelatex": @settings =
  20. apis:
  21. filestore:
  22. url: "filestore.example.com"
  23. secret: "secret"
  24. clsi:
  25. url: "http://clsi.example.com"
  26. clsi_priority:
  27. url: "https://clsipremium.example.com"
  28. "../../models/Project": Project: @Project = {}
  29. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  30. "../Project/ProjectGetter": @ProjectGetter = {}
  31. "../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler =
  32. getProjectDocsIfMatch: sinon.stub().callsArgWith(2,null,null)
  33. "./ClsiCookieManager": @ClsiCookieManager
  34. "./ClsiStateManager": @ClsiStateManager
  35. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), err: sinon.stub(), warn: sinon.stub() }
  36. "request": @request = sinon.stub()
  37. "./ClsiFormatChecker": @ClsiFormatChecker
  38. "metrics-sharelatex": @Metrics =
  39. Timer: class Timer
  40. done: sinon.stub()
  41. inc: sinon.stub()
  42. @project_id = "project-id"
  43. @user_id = "user-id"
  44. @callback = sinon.stub()
  45. describe "sendRequest", ->
  46. beforeEach ->
  47. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
  48. @ClsiCookieManager._getServerId.callsArgWith(1, null, "clsi3")
  49. describe "with a successful compile", ->
  50. beforeEach ->
  51. @ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  52. compile:
  53. status: @status = "success"
  54. outputFiles: [{
  55. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}/build/1234/output/output.pdf"
  56. path: "output.pdf"
  57. type: "pdf"
  58. build: 1234
  59. },{
  60. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}/build/1234/output/output.log"
  61. path: "output.log"
  62. type: "log"
  63. build: 1234
  64. }]
  65. })
  66. @ClsiManager.sendRequest @project_id, @user_id, {compileGroup:"standard"}, @callback
  67. it "should build the request", ->
  68. @ClsiManager._buildRequest
  69. .calledWith(@project_id)
  70. .should.equal true
  71. it "should send the request to the CLSI", ->
  72. @ClsiManager._postToClsi
  73. .calledWith(@project_id, @user_id, @request, "standard")
  74. .should.equal true
  75. it "should call the callback with the status and output files", ->
  76. outputFiles = [{
  77. url: "/project/#{@project_id}/user/#{@user_id}/build/1234/output/output.pdf"
  78. path: "output.pdf"
  79. type: "pdf"
  80. build: 1234
  81. },{
  82. url: "/project/#{@project_id}/user/#{@user_id}/build/1234/output/output.log"
  83. path: "output.log"
  84. type: "log"
  85. build: 1234
  86. }]
  87. @callback.calledWith(null, @status, outputFiles).should.equal true
  88. describe "with a failed compile", ->
  89. beforeEach ->
  90. @ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  91. compile:
  92. status: @status = "failure"
  93. })
  94. @ClsiManager.sendRequest @project_id, @user_id, {}, @callback
  95. it "should call the callback with a failure statue", ->
  96. @callback.calledWith(null, @status).should.equal true
  97. describe "with a sync conflict", ->
  98. beforeEach ->
  99. @ClsiManager.sendRequestOnce = sinon.stub()
  100. @ClsiManager.sendRequestOnce.withArgs(@project_id, @user_id, {syncType:"full"}).callsArgWith(3, null, @status = "success")
  101. @ClsiManager.sendRequestOnce.withArgs(@project_id, @user_id, {}).callsArgWith(3, null, "conflict")
  102. @ClsiManager.sendRequest @project_id, @user_id, {}, @callback
  103. it "should call the sendRequestOnce method twice", ->
  104. @ClsiManager.sendRequestOnce.calledTwice.should.equal true
  105. it "should call the sendRequestOnce method with syncType:full", ->
  106. @ClsiManager.sendRequestOnce.calledWith(@project_id, @user_id, {syncType:"full"}).should.equal true
  107. it "should call the sendRequestOnce method without syncType:full", ->
  108. @ClsiManager.sendRequestOnce.calledWith(@project_id, @user_id, {}).should.equal true
  109. it "should call the callback with a success status", ->
  110. @callback.calledWith(null, @status, ).should.equal true
  111. describe "when the resources fail the precompile check", ->
  112. beforeEach ->
  113. @ClsiFormatChecker.checkRecoursesForProblems = sinon.stub().callsArgWith(1, new Error("failed"))
  114. @ClsiManager._postToClsi = sinon.stub().callsArgWith(4, null, {
  115. compile:
  116. status: @status = "failure"
  117. })
  118. @ClsiManager.sendRequest @project_id, @user_id, {}, @callback
  119. it "should call the callback only once", ->
  120. @callback.calledOnce.should.equal true
  121. it "should call the callback with an error", ->
  122. @callback.calledWithExactly(new Error("failed")).should.equal true
  123. describe "deleteAuxFiles", ->
  124. beforeEach ->
  125. @ClsiManager._makeRequest = sinon.stub().callsArg(2)
  126. @DocumentUpdaterHandler.clearProjectState = sinon.stub().callsArg(1)
  127. describe "with the standard compileGroup", ->
  128. beforeEach ->
  129. @ClsiManager.deleteAuxFiles @project_id, @user_id, {compileGroup: "standard"}, @callback
  130. it "should call the delete method in the standard CLSI", ->
  131. @ClsiManager._makeRequest
  132. .calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}"})
  133. .should.equal true
  134. it "should clear the project state from the docupdater", ->
  135. @DocumentUpdaterHandler.clearProjectState
  136. .calledWith(@project_id)
  137. .should.equal true
  138. it "should call the callback", ->
  139. @callback.called.should.equal true
  140. describe "_buildRequest", ->
  141. beforeEach ->
  142. @project =
  143. _id: @project_id
  144. compiler: @compiler = "latex"
  145. rootDoc_id: "mock-doc-id-1"
  146. imageName: @image = "mock-image-name"
  147. @docs = {
  148. "/main.tex": @doc_1 = {
  149. name: "main.tex"
  150. _id: "mock-doc-id-1"
  151. lines: ["Hello", "world"]
  152. },
  153. "/chapters/chapter1.tex": @doc_2 = {
  154. name: "chapter1.tex"
  155. _id: "mock-doc-id-2"
  156. lines: [
  157. "Chapter 1"
  158. ]
  159. }
  160. }
  161. @files = {
  162. "/images/image.png": @file_1 = {
  163. name: "image.png"
  164. _id: "mock-file-id-1"
  165. created: new Date()
  166. }
  167. }
  168. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  169. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  170. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  171. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  172. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
  173. describe "with a valid project", ->
  174. beforeEach (done) ->
  175. @ClsiManager._buildRequest @project_id, {timeout:100}, (error, request) =>
  176. @request = request
  177. done()
  178. it "should get the project with the required fields", ->
  179. @ProjectGetter.getProject
  180. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1, rootFolder: 1})
  181. .should.equal true
  182. it "should flush the project to the database", ->
  183. @DocumentUpdaterHandler.flushProjectToMongo
  184. .calledWith(@project_id)
  185. .should.equal true
  186. it "should get all the docs", ->
  187. @ProjectEntityHandler.getAllDocs
  188. .calledWith(@project_id)
  189. .should.equal true
  190. it "should get all the files", ->
  191. @ProjectEntityHandler.getAllFiles
  192. .calledWith(@project_id)
  193. .should.equal true
  194. it "should build up the CLSI request", ->
  195. expect(@request).to.deep.equal(
  196. compile:
  197. options:
  198. compiler: @compiler
  199. timeout : 100
  200. imageName: @image
  201. draft: false
  202. check: undefined
  203. syncType: undefined # "full"
  204. syncState: undefined # "01234567890abcdef"
  205. rootResourcePath: "main.tex"
  206. resources: [{
  207. path: "main.tex"
  208. content: @doc_1.lines.join("\n")
  209. }, {
  210. path: "chapters/chapter1.tex"
  211. content: @doc_2.lines.join("\n")
  212. }, {
  213. path: "images/image.png"
  214. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  215. modified: @file_1.created.getTime()
  216. }]
  217. )
  218. describe "with the incremental compile option", ->
  219. beforeEach (done) ->
  220. @ClsiStateManager.computeHash = sinon.stub().callsArgWith(2, null, @project_state_hash = "01234567890abcdef")
  221. @DocumentUpdaterHandler.getProjectDocsIfMatch = sinon.stub().callsArgWith(2, null, [{_id:@doc_1._id, lines: @doc_1.lines, v: 123}])
  222. @ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, {"mock-doc-id-1":"main.tex"})
  223. @ClsiManager._buildRequest @project_id, {timeout:100, incrementalCompilesEnabled:true}, (error, request) =>
  224. @request = request
  225. done()
  226. it "should get the project with the required fields", ->
  227. @ProjectGetter.getProject
  228. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1, rootFolder: 1})
  229. .should.equal true
  230. it "should not explicitly flush the project to the database", ->
  231. @DocumentUpdaterHandler.flushProjectToMongo
  232. .calledWith(@project_id)
  233. .should.equal false
  234. it "should get only the live docs from the docupdater with a background flush in docupdater", ->
  235. @DocumentUpdaterHandler.getProjectDocsIfMatch
  236. .calledWith(@project_id)
  237. .should.equal true
  238. it "should not get any of the files", ->
  239. @ProjectEntityHandler.getAllFiles
  240. .called.should.equal false
  241. it "should build up the CLSI request", ->
  242. expect(@request).to.deep.equal(
  243. compile:
  244. options:
  245. compiler: @compiler
  246. timeout : 100
  247. imageName: @image
  248. draft: false
  249. check: undefined
  250. syncType: "incremental"
  251. syncState: "01234567890abcdef"
  252. rootResourcePath: "main.tex"
  253. resources: [{
  254. path: "main.tex"
  255. content: @doc_1.lines.join("\n")
  256. }]
  257. )
  258. describe "when the root doc is set and not in the docupdater", ->
  259. beforeEach (done) ->
  260. @ClsiStateManager.computeHash = sinon.stub().callsArgWith(2, null, @project_state_hash = "01234567890abcdef")
  261. @DocumentUpdaterHandler.getProjectDocsIfMatch = sinon.stub().callsArgWith(2, null, [{_id:@doc_1._id, lines: @doc_1.lines, v: 123}])
  262. @ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, {"mock-doc-id-1":"main.tex", "mock-doc-id-2":"/chapters/chapter1.tex"})
  263. @ClsiManager._buildRequest @project_id, {timeout:100, incrementalCompilesEnabled:true, rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  264. @request = request
  265. done()
  266. it "should still change the root path", ->
  267. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  268. describe "when root doc override is valid", ->
  269. beforeEach (done) ->
  270. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  271. @request = request
  272. done()
  273. it "should change root path", ->
  274. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  275. describe "when root doc override is invalid", ->
  276. beforeEach (done) ->
  277. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  278. @request = request
  279. done()
  280. it "should fallback to default root doc", ->
  281. @request.compile.rootResourcePath.should.equal "main.tex"
  282. describe "when the project has an invalid compiler", ->
  283. beforeEach (done) ->
  284. @project.compiler = "context"
  285. @ClsiManager._buildRequest @project, null, (error, request) =>
  286. @request = request
  287. done()
  288. it "should set the compiler to pdflatex", ->
  289. @request.compile.options.compiler.should.equal "pdflatex"
  290. describe "when there is no valid root document", ->
  291. beforeEach (done) ->
  292. @project.rootDoc_id = "not-valid"
  293. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  294. done()
  295. it "should set to main.tex", ->
  296. @request.compile.rootResourcePath.should.equal "main.tex"
  297. describe "when there is no valid root document and no main.tex document", ->
  298. beforeEach () ->
  299. @project.rootDoc_id = "not-valid"
  300. @docs = {
  301. "/other.tex": @doc_1 = {
  302. name: "other.tex"
  303. _id: "mock-doc-id-1"
  304. lines: ["Hello", "world"]
  305. },
  306. "/chapters/chapter1.tex": @doc_2 = {
  307. name: "chapter1.tex"
  308. _id: "mock-doc-id-2"
  309. lines: [
  310. "Chapter 1"
  311. ]
  312. }
  313. }
  314. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  315. @ClsiManager._buildRequest @project, null, @callback
  316. it "should report an error", ->
  317. @callback.calledWith(new Error("no main file specified")).should.equal true
  318. describe "when there is no valid root document and a single document which is not main.tex", ->
  319. beforeEach (done) ->
  320. @project.rootDoc_id = "not-valid"
  321. @docs = {
  322. "/other.tex": @doc_1 = {
  323. name: "other.tex"
  324. _id: "mock-doc-id-1"
  325. lines: ["Hello", "world"]
  326. }
  327. }
  328. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  329. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  330. done()
  331. it "should set io to the only file", ->
  332. @request.compile.rootResourcePath.should.equal "other.tex"
  333. describe "with the draft option", ->
  334. it "should add the draft option into the request", (done) ->
  335. @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>
  336. request.compile.options.draft.should.equal true
  337. done()
  338. describe '_postToClsi', ->
  339. beforeEach ->
  340. @req = { mock: "req" }
  341. describe "successfully", ->
  342. beforeEach ->
  343. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 204}, @body = { mock: "foo" })
  344. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  345. it 'should send the request to the CLSI', ->
  346. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}/compile"
  347. @ClsiManager._makeRequest.calledWith(@project_id, {
  348. method: "POST",
  349. url: url
  350. json: @req
  351. }).should.equal true
  352. it "should call the callback with the body and no error", ->
  353. @callback.calledWith(null, @body).should.equal true
  354. describe "when the CLSI returns an error", ->
  355. beforeEach ->
  356. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 500}, @body = { mock: "foo" })
  357. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  358. it "should call the callback with the body and the error", ->
  359. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  360. describe "wordCount", ->
  361. beforeEach ->
  362. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 200}, @body = { mock: "foo" })
  363. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
  364. describe "with root file", ->
  365. beforeEach ->
  366. @ClsiManager.wordCount @project_id, @user_id, false, {}, @callback
  367. it "should call wordCount with root file", ->
  368. @ClsiManager._makeRequest
  369. .calledWith(@project_id, {method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs: {file: "rootfile.text",image:undefined}})
  370. .should.equal true
  371. it "should call the callback", ->
  372. @callback.called.should.equal true
  373. describe "with param file", ->
  374. beforeEach ->
  375. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  376. it "should call wordCount with param file", ->
  377. @ClsiManager._makeRequest
  378. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:undefined}})
  379. .should.equal true
  380. describe "with image", ->
  381. beforeEach ->
  382. @req.compile.options.imageName = @image = "example.com/mock/image"
  383. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  384. it "should call wordCount with file and image", ->
  385. @ClsiManager._makeRequest
  386. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:@image}})
  387. .should.equal true
  388. describe "_makeRequest", ->
  389. beforeEach ->
  390. @response = {there:"something"}
  391. @request.callsArgWith(1, null, @response)
  392. @opts =
  393. method: "SOMETHIGN"
  394. url: "http://a place on the web"
  395. it "should process a request with a cookie jar", (done)->
  396. @ClsiManager._makeRequest @project_id, @opts, =>
  397. args = @request.args[0]
  398. args[0].method.should.equal @opts.method
  399. args[0].url.should.equal @opts.url
  400. args[0].jar.should.equal @jar
  401. done()
  402. it "should set the cookie again on response as it might have changed", (done)->
  403. @ClsiManager._makeRequest @project_id, @opts, =>
  404. @ClsiCookieManager.setServerId.calledWith(@project_id, @response).should.equal true
  405. done()