ClsiManagerTests.coffee 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. @ClsiManager = SandboxedModule.require modulePath, requires:
  10. "settings-sharelatex": @settings =
  11. apis:
  12. filestore:
  13. url: "filestore.example.com"
  14. secret: "secret"
  15. clsi:
  16. url: "http://clsi.example.com"
  17. clsi_priority:
  18. url: "https://clsipremium.example.com"
  19. "../../models/Project": Project: @Project = {}
  20. "../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
  21. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  22. "request": @request = {}
  23. @project_id = "project-id"
  24. @callback = sinon.stub()
  25. describe "sendRequest", ->
  26. beforeEach ->
  27. @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, @request = "mock-request")
  28. describe "with a successful compile", ->
  29. beforeEach ->
  30. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  31. compile:
  32. status: @status = "success"
  33. outputFiles: [{
  34. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.pdf"
  35. type: "pdf"
  36. build: 1234
  37. },{
  38. url: "#{@settings.apis.clsi.url}/project/#{@project_id}/output/output.log"
  39. type: "log"
  40. build: 1234
  41. }]
  42. })
  43. @ClsiManager.sendRequest @project_id, {compileGroup:"standard"}, @callback
  44. it "should build the request", ->
  45. @ClsiManager._buildRequest
  46. .calledWith(@project_id)
  47. .should.equal true
  48. it "should send the request to the CLSI", ->
  49. @ClsiManager._postToClsi
  50. .calledWith(@project_id, @request, "standard")
  51. .should.equal true
  52. it "should call the callback with the status and output files", ->
  53. outputFiles = [{
  54. path: "output.pdf"
  55. type: "pdf"
  56. build: 1234
  57. },{
  58. path: "output.log"
  59. type: "log"
  60. build: 1234
  61. }]
  62. @callback.calledWith(null, @status, outputFiles).should.equal true
  63. describe "with a failed compile", ->
  64. beforeEach ->
  65. @ClsiManager._postToClsi = sinon.stub().callsArgWith(3, null, {
  66. compile:
  67. status: @status = "failure"
  68. })
  69. @ClsiManager.sendRequest @project_id, {}, @callback
  70. it "should call the callback with a failure statue", ->
  71. @callback.calledWith(null, @status).should.equal true
  72. describe "deleteAuxFiles", ->
  73. beforeEach ->
  74. @request.del = sinon.stub().callsArg(1)
  75. describe "with the standard compileGroup", ->
  76. beforeEach ->
  77. @ClsiManager.deleteAuxFiles @project_id, {compileGroup: "standard"}, @callback
  78. it "should call the delete method in the standard CLSI", ->
  79. @request.del
  80. .calledWith("#{@settings.apis.clsi.url}/project/#{@project_id}")
  81. .should.equal true
  82. it "should call the callback", ->
  83. @callback.called.should.equal true
  84. describe "with the priority compileGroup", ->
  85. beforeEach ->
  86. @ClsiManager.deleteAuxFiles @project_id, {compileGroup: "priority"}, @callback
  87. it "should call the delete method in the CLSI", ->
  88. @request.del
  89. .calledWith("#{@settings.apis.clsi_priority.url}/project/#{@project_id}")
  90. .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. @docs = {
  98. "/main.tex": @doc_1 = {
  99. name: "main.tex"
  100. _id: "mock-doc-id-1"
  101. lines: ["Hello", "world"]
  102. },
  103. "/chapters/chapter1.tex": @doc_2 = {
  104. name: "chapter1.tex"
  105. _id: "mock-doc-id-2"
  106. lines: [
  107. "Chapter 1"
  108. ]
  109. }
  110. }
  111. @files = {
  112. "/images/image.png": @file_1 = {
  113. name: "image.png"
  114. _id: "mock-file-id-1"
  115. created: new Date()
  116. }
  117. }
  118. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  119. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  120. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  121. describe "with a valid project", ->
  122. beforeEach (done) ->
  123. @ClsiManager._buildRequest @project_id, {timeout:100}, (error, request) =>
  124. @request = request
  125. done()
  126. it "should get the project with the required fields", ->
  127. @Project.findById
  128. .calledWith(@project_id, {compiler:1, rootDoc_id: 1})
  129. .should.equal true
  130. it "should get all the docs", ->
  131. @ProjectEntityHandler.getAllDocs
  132. .calledWith(@project_id)
  133. .should.equal true
  134. it "should get all the files", ->
  135. @ProjectEntityHandler.getAllFiles
  136. .calledWith(@project_id)
  137. .should.equal true
  138. it "should build up the CLSI request", ->
  139. expect(@request).to.deep.equal(
  140. compile:
  141. options:
  142. compiler: @compiler
  143. timeout : 100
  144. rootResourcePath: "main.tex"
  145. resources: [{
  146. path: "main.tex"
  147. content: @doc_1.lines.join("\n")
  148. }, {
  149. path: "chapters/chapter1.tex"
  150. content: @doc_2.lines.join("\n")
  151. }, {
  152. path: "images/image.png"
  153. url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
  154. modified: @file_1.created.getTime()
  155. }]
  156. )
  157. describe "when root doc override is valid", ->
  158. beforeEach (done) ->
  159. @ClsiManager._buildRequest @project_id, {rootDoc_id:"mock-doc-id-2"}, (error, request) =>
  160. @request = request
  161. done()
  162. it "should change root path", ->
  163. @request.compile.rootResourcePath.should.equal "chapters/chapter1.tex"
  164. describe "when root doc override is invalid", ->
  165. beforeEach (done) ->
  166. @ClsiManager._buildRequest @project_id, {rootDoc_id:"invalid-id"}, (error, request) =>
  167. @request = request
  168. done()
  169. it "should fallback to default root doc", ->
  170. @request.compile.rootResourcePath.should.equal "main.tex"
  171. describe "when the project has an invalid compiler", ->
  172. beforeEach (done) ->
  173. @project.compiler = "context"
  174. @ClsiManager._buildRequest @project, null, (error, request) =>
  175. @request = request
  176. done()
  177. it "should set the compiler to pdflatex", ->
  178. @request.compile.options.compiler.should.equal "pdflatex"
  179. describe "when there is no valid root document", ->
  180. beforeEach (done) ->
  181. @project.rootDoc_id = "not-valid"
  182. @ClsiManager._buildRequest @project, null, (@error, @request) =>
  183. done()
  184. it "should return an error", ->
  185. expect(@error).to.exist
  186. describe '_postToClsi', ->
  187. beforeEach ->
  188. @req = { mock: "req" }
  189. describe "successfully", ->
  190. beforeEach ->
  191. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, @body = { mock: "foo" })
  192. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  193. it 'should send the request to the CLSI', ->
  194. url = "#{@settings.apis.clsi.url}/project/#{@project_id}/compile"
  195. @request.post.calledWith({
  196. url: url
  197. json: @req
  198. jar: false
  199. }).should.equal true
  200. it "should call the callback with the body and no error", ->
  201. @callback.calledWith(null, @body).should.equal true
  202. describe "when the CLSI returns an error", ->
  203. beforeEach ->
  204. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
  205. @ClsiManager._postToClsi @project_id, @req, "standard", @callback
  206. it "should call the callback with the body and the error", ->
  207. @callback.calledWith(new Error("CLSI returned non-success code: 500"), @body).should.equal true
  208. describe "when the compiler is priority", ->
  209. beforeEach ->
  210. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 500}, @body = { mock: "foo" })
  211. @ClsiManager._postToClsi @project_id, @req, "priority", @callback
  212. it "should use the clsi_priority url", ->
  213. url = "#{@settings.apis.clsi_priority.url}/project/#{@project_id}/compile"
  214. @request.post.calledWith({
  215. url: url
  216. json: @req
  217. jar: false
  218. }).should.equal true