ClsiManagerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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(), 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 "deleteAuxFiles", ->
  112. beforeEach ->
  113. @ClsiManager._makeRequest = sinon.stub().callsArg(2)
  114. @DocumentUpdaterHandler.clearProjectState = sinon.stub().callsArg(1)
  115. describe "with the standard compileGroup", ->
  116. beforeEach ->
  117. @ClsiManager.deleteAuxFiles @project_id, @user_id, {compileGroup: "standard"}, @callback
  118. it "should call the delete method in the standard CLSI", ->
  119. @ClsiManager._makeRequest
  120. .calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}"})
  121. .should.equal true
  122. it "should clear the project state from the docupdater", ->
  123. @DocumentUpdaterHandler.clearProjectState
  124. .calledWith(@project_id)
  125. .should.equal true
  126. it "should call the callback", ->
  127. @callback.called.should.equal true
  128. describe "_buildRequest", ->
  129. beforeEach ->
  130. @project =
  131. _id: @project_id
  132. compiler: @compiler = "latex"
  133. rootDoc_id: "mock-doc-id-1"
  134. imageName: @image = "mock-image-name"
  135. @docs = {
  136. "/main.tex": @doc_1 = {
  137. name: "main.tex"
  138. _id: "mock-doc-id-1"
  139. lines: ["Hello", "world"]
  140. },
  141. "/chapters/chapter1.tex": @doc_2 = {
  142. name: "chapter1.tex"
  143. _id: "mock-doc-id-2"
  144. lines: [
  145. "Chapter 1"
  146. ]
  147. }
  148. }
  149. @files = {
  150. "/images/image.png": @file_1 = {
  151. name: "image.png"
  152. _id: "mock-file-id-1"
  153. created: new Date()
  154. }
  155. }
  156. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  157. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  158. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  159. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  160. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
  161. describe "with a valid project", ->
  162. beforeEach (done) ->
  163. @ClsiManager._buildRequest @project_id, {timeout:100}, (error, request) =>
  164. @request = request
  165. done()
  166. it "should get the project with the required fields", ->
  167. @ProjectGetter.getProject
  168. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1, rootFolder: 1})
  169. .should.equal true
  170. it "should flush the project to the database", ->
  171. @DocumentUpdaterHandler.flushProjectToMongo
  172. .calledWith(@project_id)
  173. .should.equal true
  174. it "should get all the docs", ->
  175. @ProjectEntityHandler.getAllDocs
  176. .calledWith(@project_id)
  177. .should.equal true
  178. it "should get all the files", ->
  179. @ProjectEntityHandler.getAllFiles
  180. .calledWith(@project_id)
  181. .should.equal true
  182. it "should build up the CLSI request", ->
  183. expect(@request).to.deep.equal(
  184. compile:
  185. options:
  186. compiler: @compiler
  187. timeout : 100
  188. imageName: @image
  189. draft: false
  190. check: undefined
  191. syncType: undefined # "full"
  192. syncState: undefined # "01234567890abcdef"
  193. rootResourcePath: "main.tex"
  194. resources: [{
  195. path: "main.tex"
  196. content: @doc_1.lines.join("\n")
  197. }, {
  198. path: "chapters/chapter1.tex"
  199. content: @doc_2.lines.join("\n")
  200. }, {
  201. path: "images/image.png"
  202. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  203. modified: @file_1.created.getTime()
  204. }]
  205. )
  206. describe "with the incremental compile option", ->
  207. beforeEach (done) ->
  208. @ClsiStateManager.computeHash = sinon.stub().callsArgWith(2, null, @project_state_hash = "01234567890abcdef")
  209. @DocumentUpdaterHandler.getProjectDocsIfMatch = sinon.stub().callsArgWith(2, null, [{_id:@doc_1._id, lines: @doc_1.lines, v: 123}])
  210. @ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, {"mock-doc-id-1":"main.tex"})
  211. @ClsiManager._buildRequest @project_id, {timeout:100, incrementalCompilesEnabled:true}, (error, request) =>
  212. @request = request
  213. done()
  214. it "should get the project with the required fields", ->
  215. @ProjectGetter.getProject
  216. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1, rootFolder: 1})
  217. .should.equal true
  218. it "should flush the project to the database", ->
  219. @DocumentUpdaterHandler.flushProjectToMongo
  220. .calledWith(@project_id)
  221. .should.equal true
  222. it "should get only the live docs from the docupdater", ->
  223. @DocumentUpdaterHandler.getProjectDocsIfMatch
  224. .calledWith(@project_id)
  225. .should.equal true
  226. it "should not get any of the files", ->
  227. @ProjectEntityHandler.getAllFiles
  228. .called.should.equal false
  229. it "should build up the CLSI request", ->
  230. expect(@request).to.deep.equal(
  231. compile:
  232. options:
  233. compiler: @compiler
  234. timeout : 100
  235. imageName: @image
  236. draft: false
  237. check: undefined
  238. syncType: "incremental"
  239. syncState: "01234567890abcdef"
  240. rootResourcePath: "main.tex"
  241. resources: [{
  242. path: "main.tex"
  243. content: @doc_1.lines.join("\n")
  244. }]
  245. )
  246. describe "when the root doc is set and not in the docupdater", ->
  247. beforeEach (done) ->
  248. @ClsiStateManager.computeHash = sinon.stub().callsArgWith(2, null, @project_state_hash = "01234567890abcdef")
  249. @DocumentUpdaterHandler.getProjectDocsIfMatch = sinon.stub().callsArgWith(2, null, [{_id:@doc_1._id, lines: @doc_1.lines, v: 123}])
  250. @ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, {"mock-doc-id-1":"main.tex", "mock-doc-id-2":"/chapters/chapter1.tex"})
  251. @ClsiManager._buildRequest @project_id, {timeout:100, incrementalCompilesEnabled:true, rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  252. @request = request
  253. done()
  254. it "should still change the root path", ->
  255. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  256. describe "when root doc override is valid", ->
  257. beforeEach (done) ->
  258. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  259. @request = request
  260. done()
  261. it "should change root path", ->
  262. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  263. describe "when root doc override is invalid", ->
  264. beforeEach (done) ->
  265. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  266. @request = request
  267. done()
  268. it "should fallback to default root doc", ->
  269. @request.compile.rootResourcePath.should.equal "main.tex"
  270. describe "when the project has an invalid compiler", ->
  271. beforeEach (done) ->
  272. @project.compiler = "context"
  273. @ClsiManager._buildRequest @project, null, (error, request) =>
  274. @request = request
  275. done()
  276. it "should set the compiler to pdflatex", ->
  277. @request.compile.options.compiler.should.equal "pdflatex"
  278. describe "when there is no valid root document", ->
  279. beforeEach (done) ->
  280. @project.rootDoc_id = "not-valid"
  281. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  282. done()
  283. it "should set to main.tex", ->
  284. @request.compile.rootResourcePath.should.equal "main.tex"
  285. describe "with the draft option", ->
  286. it "should add the draft option into the request", (done) ->
  287. @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>
  288. request.compile.options.draft.should.equal true
  289. done()
  290. describe '_postToClsi', ->
  291. beforeEach ->
  292. @req = { mock: "req" }
  293. describe "successfully", ->
  294. beforeEach ->
  295. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 204}, @body = { mock: "foo" })
  296. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  297. it 'should send the request to the CLSI', ->
  298. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}/compile"
  299. @ClsiManager._makeRequest.calledWith(@project_id, {
  300. method: "POST",
  301. url: url
  302. json: @req
  303. }).should.equal true
  304. it "should call the callback with the body and no error", ->
  305. @callback.calledWith(null, @body).should.equal true
  306. describe "when the CLSI returns an error", ->
  307. beforeEach ->
  308. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 500}, @body = { mock: "foo" })
  309. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  310. it "should call the callback with the body and the error", ->
  311. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  312. describe "wordCount", ->
  313. beforeEach ->
  314. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 200}, @body = { mock: "foo" })
  315. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
  316. describe "with root file", ->
  317. beforeEach ->
  318. @ClsiManager.wordCount @project_id, @user_id, false, {}, @callback
  319. it "should call wordCount with root file", ->
  320. @ClsiManager._makeRequest
  321. .calledWith(@project_id, {method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs: {file: "rootfile.text",image:undefined}})
  322. .should.equal true
  323. it "should call the callback", ->
  324. @callback.called.should.equal true
  325. describe "with param file", ->
  326. beforeEach ->
  327. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  328. it "should call wordCount with param file", ->
  329. @ClsiManager._makeRequest
  330. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:undefined}})
  331. .should.equal true
  332. describe "with image", ->
  333. beforeEach ->
  334. @req.compile.options.imageName = @image = "example.com/mock/image"
  335. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  336. it "should call wordCount with file and image", ->
  337. @ClsiManager._makeRequest
  338. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:@image}})
  339. .should.equal true
  340. describe "_makeRequest", ->
  341. beforeEach ->
  342. @response = {there:"something"}
  343. @request.callsArgWith(1, null, @response)
  344. @opts =
  345. method: "SOMETHIGN"
  346. url: "http://a place on the web"
  347. it "should process a request with a cookie jar", (done)->
  348. @ClsiManager._makeRequest @project_id, @opts, =>
  349. args = @request.args[0]
  350. args[0].method.should.equal @opts.method
  351. args[0].url.should.equal @opts.url
  352. args[0].jar.should.equal @jar
  353. done()
  354. it "should set the cookie again on response as it might have changed", (done)->
  355. @ClsiManager._makeRequest @project_id, @opts, =>
  356. @ClsiCookieManager.setServerId.calledWith(@project_id, @response).should.equal true
  357. done()