ClsiManagerTests.coffee 11 KB

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