ClsiManagerTests.coffee 6.6 KB

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