ClsiManagerTests.coffee 14 KB

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