ClsiManagerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. @ClsiFormatChecker =
  15. checkRecoursesForProblems:sinon.stub().callsArgWith(1)
  16. @ClsiManager = SandboxedModule.require modulePath, requires:
  17. "settings-sharelatex": @settings =
  18. apis:
  19. filestore:
  20. url: "filestore.example.com"
  21. secret: "secret"
  22. clsi:
  23. url: "http://clsi.example.com"
  24. clsi_priority:
  25. url: "https://clsipremium.example.com"
  26. "../../models/Project": Project: @Project = {}
  27. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  28. "./ClsiCookieManager": @ClsiCookieManager
  29. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() }
  30. "request": @request = sinon.stub()
  31. "./ClsiFormatChecker": @ClsiFormatChecker
  32. @project_id = "project-id"
  33. @callback = sinon.stub()
  34. describe "sendRequest", ->
  35. beforeEach ->
  36. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
  37. @ClsiCookieManager._getServerId.callsArgWith(1, null, "clsi3")
  38. describe "with a successful compile", ->
  39. beforeEach ->
  40. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  41. compile:
  42. status: @status = "success"
  43. outputFiles: [{
  44. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.pdf"
  45. type: "pdf"
  46. build: 1234
  47. },{
  48. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.log"
  49. type: "log"
  50. build: 1234
  51. }]
  52. })
  53. @ClsiManager.sendRequest @project_id, {compileGroup:"standard"}, @callback
  54. it "should build the request", ->
  55. @ClsiManager._buildRequest
  56. .calledWith(@project_id)
  57. .should.equal true
  58. it "should send the request to the CLSI", ->
  59. @ClsiManager._postToClsi
  60. .calledWith(@project_id, @request, "standard")
  61. .should.equal true
  62. it "should call the callback with the status and output files", ->
  63. outputFiles = [{
  64. path: "output.pdf"
  65. type: "pdf"
  66. build: 1234
  67. },{
  68. path: "output.log"
  69. type: "log"
  70. build: 1234
  71. }]
  72. @callback.calledWith(null, @status, outputFiles).should.equal true
  73. describe "with a failed compile", ->
  74. beforeEach ->
  75. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  76. compile:
  77. status: @status = "failure"
  78. })
  79. @ClsiManager.sendRequest @project_id, {}, @callback
  80. it "should call the callback with a failure statue", ->
  81. @callback.calledWith(null, @status).should.equal true
  82. describe "deleteAuxFiles", ->
  83. beforeEach ->
  84. @ClsiManager._makeRequest = sinon.stub().callsArg(2)
  85. describe "with the standard compileGroup", ->
  86. beforeEach ->
  87. @ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
  88. it "should call the delete method in the standard CLSI", ->
  89. @ClsiManager._makeRequest
  90. .calledWith(@project_id, { method:"DELETE", url:"#{@settings.apis.clsi.url}/project/#{@project_id}"})
  91. .should.equal true
  92. it "should call the callback", ->
  93. @callback.called.should.equal true
  94. describe "_buildRequest", ->
  95. beforeEach ->
  96. @project =
  97. _id: @project_id
  98. compiler: @compiler = "latex"
  99. rootDoc_id: "mock-doc-id-1"
  100. imageName: @image = "mock-image-name"
  101. @docs = {
  102. "/main.tex": @doc_1 = {
  103. name: "main.tex"
  104. _id: "mock-doc-id-1"
  105. lines: ["Hello", "world"]
  106. },
  107. "/chapters/chapter1.tex": @doc_2 = {
  108. name: "chapter1.tex"
  109. _id: "mock-doc-id-2"
  110. lines: [
  111. "Chapter 1"
  112. ]
  113. }
  114. }
  115. @files = {
  116. "/images/image.png": @file_1 = {
  117. name: "image.png"
  118. _id: "mock-file-id-1"
  119. created: new Date()
  120. }
  121. }
  122. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  123. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  124. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  125. describe "with a valid project", ->
  126. beforeEach (done) ->
  127. @ClsiManager._buildRequest @project_id, {timeout:100}, (error, request) =>
  128. @request = request
  129. done()
  130. it "should get the project with the required fields", ->
  131. @Project.findById
  132. .calledWith(@project_id, {compiler:1, rootDoc_id: 1, imageName: 1})
  133. .should.equal true
  134. it "should get all the docs", ->
  135. @ProjectEntityHandler.getAllDocs
  136. .calledWith(@project_id)
  137. .should.equal true
  138. it "should get all the files", ->
  139. @ProjectEntityHandler.getAllFiles
  140. .calledWith(@project_id)
  141. .should.equal true
  142. it "should build up the CLSI request", ->
  143. expect(@request).to.deep.equal(
  144. compile:
  145. options:
  146. compiler: @compiler
  147. timeout : 100
  148. imageName: @image
  149. draft: false
  150. rootResourcePath: "main.tex"
  151. resources: [{
  152. path: "main.tex"
  153. content: @doc_1.lines.join("\n")
  154. }, {
  155. path: "chapters/chapter1.tex"
  156. content: @doc_2.lines.join("\n")
  157. }, {
  158. path: "images/image.png"
  159. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  160. modified: @file_1.created.getTime()
  161. }]
  162. )
  163. describe "when root doc override is valid", ->
  164. beforeEach (done) ->
  165. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  166. @request = request
  167. done()
  168. it "should change root path", ->
  169. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  170. describe "when root doc override is invalid", ->
  171. beforeEach (done) ->
  172. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  173. @request = request
  174. done()
  175. it "should fallback to default root doc", ->
  176. @request.compile.rootResourcePath.should.equal "main.tex"
  177. describe "when the project has an invalid compiler", ->
  178. beforeEach (done) ->
  179. @project.compiler = "context"
  180. @ClsiManager._buildRequest @project, null, (error, request) =>
  181. @request = request
  182. done()
  183. it "should set the compiler to pdflatex", ->
  184. @request.compile.options.compiler.should.equal "pdflatex"
  185. describe "when there is no valid root document", ->
  186. beforeEach (done) ->
  187. @project.rootDoc_id = "not-valid"
  188. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  189. done()
  190. it "should set to main.tex", ->
  191. @request.compile.rootResourcePath.should.equal "main.tex"
  192. describe "with the draft option", ->
  193. it "should add the draft option into the request", (done) ->
  194. @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>
  195. request.compile.options.draft.should.equal true
  196. done()
  197. describe '_postToClsi', ->
  198. beforeEach ->
  199. @req = { mock: "req" }
  200. describe "successfully", ->
  201. beforeEach ->
  202. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 204}, @body = { mock: "foo" })
  203. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  204. it 'should send the request to the CLSI', ->
  205. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
  206. @ClsiManager._makeRequest.calledWith(@project_id, {
  207. method: "POST",
  208. url: url
  209. json: @req
  210. }).should.equal true
  211. it "should call the callback with the body and no error", ->
  212. @callback.calledWith(null, @body).should.equal true
  213. describe "when the CLSI returns an error", ->
  214. beforeEach ->
  215. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 500}, @body = { mock: "foo" })
  216. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  217. it "should call the callback with the body and the error", ->
  218. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  219. describe "wordCount", ->
  220. beforeEach ->
  221. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 200}, @body = { mock: "foo" })
  222. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
  223. @ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url"
  224. describe "with root file", ->
  225. beforeEach ->
  226. @ClsiManager.wordCount @project_id, false, {}, @callback
  227. it "should call wordCount with root file", ->
  228. @ClsiManager._makeRequest
  229. .calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" })
  230. .should.equal true
  231. it "should call the callback", ->
  232. @callback.called.should.equal true
  233. describe "with param file", ->
  234. beforeEach ->
  235. @ClsiManager.wordCount @project_id, "main.tex", {}, @callback
  236. it "should call wordCount with param file", ->
  237. @ClsiManager._makeRequest
  238. .calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" })
  239. .should.equal true
  240. describe "with image", ->
  241. beforeEach ->
  242. @req.compile.options.imageName = @image = "example.com/mock/image"
  243. @ClsiManager.wordCount @project_id, "main.tex", {}, @callback
  244. it "should call wordCount with file and image", ->
  245. @ClsiManager._makeRequest
  246. .calledWith(@project_id, { method: "GET", url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex&image=#{encodeURIComponent(@image)}" })
  247. .should.equal true
  248. describe "_makeRequest", ->
  249. beforeEach ->
  250. @response = {there:"something"}
  251. @request.callsArgWith(1, null, @response)
  252. @opts =
  253. method: "SOMETHIGN"
  254. url: "http://a place on the web"
  255. it "should process a request with a cookie jar", (done)->
  256. @ClsiManager._makeRequest @project_id, @opts, =>
  257. args = @request.args[0]
  258. args[0].method.should.equal @opts.method
  259. args[0].url.should.equal @opts.url
  260. args[0].jar.should.equal @jar
  261. done()
  262. it "should set the cookie again on response as it might have changed", (done)->
  263. @ClsiManager._makeRequest @project_id, @opts, =>
  264. @ClsiCookieManager.setServerId.calledWith(@project_id, @response).should.equal true
  265. done()