ClsiManagerTests.coffee 10 KB

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