ClsiManagerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. check: undefined
  156. rootResourcePath: "main.tex"
  157. resources: [{
  158. path: "main.tex"
  159. content: @doc_1.lines.join("\n")
  160. }, {
  161. path: "chapters/chapter1.tex"
  162. content: @doc_2.lines.join("\n")
  163. }, {
  164. path: "images/image.png"
  165. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  166. modified: @file_1.created.getTime()
  167. }]
  168. )
  169. describe "when root doc override is valid", ->
  170. beforeEach (done) ->
  171. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  172. @request = request
  173. done()
  174. it "should change root path", ->
  175. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  176. describe "when root doc override is invalid", ->
  177. beforeEach (done) ->
  178. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  179. @request = request
  180. done()
  181. it "should fallback to default root doc", ->
  182. @request.compile.rootResourcePath.should.equal "main.tex"
  183. describe "when the project has an invalid compiler", ->
  184. beforeEach (done) ->
  185. @project.compiler = "context"
  186. @ClsiManager._buildRequest @project, null, (error, request) =>
  187. @request = request
  188. done()
  189. it "should set the compiler to pdflatex", ->
  190. @request.compile.options.compiler.should.equal "pdflatex"
  191. describe "when there is no valid root document", ->
  192. beforeEach (done) ->
  193. @project.rootDoc_id = "not-valid"
  194. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  195. done()
  196. it "should set to main.tex", ->
  197. @request.compile.rootResourcePath.should.equal "main.tex"
  198. describe "with the draft option", ->
  199. it "should add the draft option into the request", (done) ->
  200. @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>
  201. request.compile.options.draft.should.equal true
  202. done()
  203. describe '_postToClsi', ->
  204. beforeEach ->
  205. @req = { mock: "req" }
  206. describe "successfully", ->
  207. beforeEach ->
  208. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 204}, @body = { mock: "foo" })
  209. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  210. it 'should send the request to the CLSI', ->
  211. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/user/#{@user_id}/compile"
  212. @ClsiManager._makeRequest.calledWith(@project_id, {
  213. method: "POST",
  214. url: url
  215. json: @req
  216. }).should.equal true
  217. it "should call the callback with the body and no error", ->
  218. @callback.calledWith(null, @body).should.equal true
  219. describe "when the CLSI returns an error", ->
  220. beforeEach ->
  221. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 500}, @body = { mock: "foo" })
  222. @ClsiManager._postToClsi @project_id, @user_id, @req, "standard", @callback
  223. it "should call the callback with the body and the error", ->
  224. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  225. describe "wordCount", ->
  226. beforeEach ->
  227. @ClsiManager._makeRequest = sinon.stub().callsArgWith(2, null, {statusCode: 200}, @body = { mock: "foo" })
  228. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @req = { compile: { rootResourcePath: "rootfile.text", options: {} } })
  229. describe "with root file", ->
  230. beforeEach ->
  231. @ClsiManager.wordCount @project_id, @user_id, false, {}, @callback
  232. it "should call wordCount with root file", ->
  233. @ClsiManager._makeRequest
  234. .calledWith(@project_id, {method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs: {file: "rootfile.text",image:undefined}})
  235. .should.equal true
  236. it "should call the callback", ->
  237. @callback.called.should.equal true
  238. describe "with param file", ->
  239. beforeEach ->
  240. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  241. it "should call wordCount with param file", ->
  242. @ClsiManager._makeRequest
  243. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:undefined}})
  244. .should.equal true
  245. describe "with image", ->
  246. beforeEach ->
  247. @req.compile.options.imageName = @image = "example.com/mock/image"
  248. @ClsiManager.wordCount @project_id, @user_id, "main.tex", {}, @callback
  249. it "should call wordCount with file and image", ->
  250. @ClsiManager._makeRequest
  251. .calledWith(@project_id, { method: "GET", url: "http://clsi.example.com/project/#{@project_id}/user/#{@user_id}/wordcount", qs:{file:"main.tex",image:@image}})
  252. .should.equal true
  253. describe "_makeRequest", ->
  254. beforeEach ->
  255. @response = {there:"something"}
  256. @request.callsArgWith(1, null, @response)
  257. @opts =
  258. method: "SOMETHIGN"
  259. url: "http://a place on the web"
  260. it "should process a request with a cookie jar", (done)->
  261. @ClsiManager._makeRequest @project_id, @opts, =>
  262. args = @request.args[0]
  263. args[0].method.should.equal @opts.method
  264. args[0].url.should.equal @opts.url
  265. args[0].jar.should.equal @jar
  266. done()
  267. it "should set the cookie again on response as it might have changed", (done)->
  268. @ClsiManager._makeRequest @project_id, @opts, =>
  269. @ClsiCookieManager.setServerId.calledWith(@project_id, @response).should.equal true
  270. done()