ClsiManagerTests.coffee 11 KB

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