ClsiManagerTests.coffee 7.2 KB

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