ClsiManagerTests.coffee 11 KB

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