ClsiManagerTests.coffee 11 KB

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