CompileManagerTests.coffee 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Compile/CompileManager.js"
  6. assert = require("chai").assert
  7. SandboxedModule = require('sandboxed-module')
  8. describe "CompileManager", ->
  9. beforeEach ->
  10. @rateLimitGetStub = sinon.stub()
  11. rateLimitGetStub = @rateLimitGetStub
  12. @ratelimiter =
  13. addCount: sinon.stub()
  14. @CompileManager = SandboxedModule.require modulePath, requires:
  15. "settings-sharelatex": @settings =
  16. redis: web: {host: "localhost", port: 42}
  17. "redis":
  18. createClient: () => @rclient = { auth: () -> }
  19. "../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler = {}
  20. "../Project/ProjectRootDocManager": @ProjectRootDocManager = {}
  21. "../../models/Project": Project: @Project = {}
  22. "./ClsiManager": @ClsiManager = {}
  23. "../../managers/LatexManager": @LatexManager = {}
  24. "../../infrastructure/RateLimiter": @ratelimiter
  25. "../../infrastructure/Metrics": @Metrics =
  26. Timer: class Timer
  27. done: sinon.stub()
  28. inc: sinon.stub()
  29. "logger-sharelatex": @logger = { log: sinon.stub() }
  30. @project_id = "mock-project-id-123"
  31. @user_id = "mock-user-id-123"
  32. @callback = sinon.stub()
  33. describe "compile", ->
  34. beforeEach ->
  35. @CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, false)
  36. @CompileManager._ensureRootDocumentIsSet = sinon.stub().callsArgWith(1, null)
  37. @DocumentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArgWith(1, null)
  38. @ClsiManager.sendRequest = sinon.stub().callsArgWith(1, null, @status = "mock-status")
  39. @LatexManager.compile = sinon.stub().callsArgWith(1, null, @status = "mock-status")
  40. describe "succesfully", ->
  41. beforeEach ->
  42. @CompileManager._checkIfAutoCompileLimitHasBeenHit = (_, cb)-> cb(null, true)
  43. @CompileManager.compile @project_id, @user_id, {}, @callback
  44. it "should check the project has not been recently compiled", ->
  45. @CompileManager._checkIfRecentlyCompiled
  46. .calledWith(@project_id, @user_id)
  47. .should.equal true
  48. it "should flush the project to the database", ->
  49. @DocumentUpdaterHandler.flushProjectToMongo
  50. .calledWith(@project_id)
  51. .should.equal true
  52. it "should ensure that the root document is set", ->
  53. @CompileManager._ensureRootDocumentIsSet
  54. .calledWith(@project_id)
  55. .should.equal true
  56. it "should run the compile with the new compiler API", ->
  57. @ClsiManager.sendRequest
  58. .calledWith(@project_id)
  59. .should.equal true
  60. it "should call the callback", ->
  61. @callback
  62. .calledWith(null, @status)
  63. .should.equal true
  64. it "should time the compile", ->
  65. @Metrics.Timer::done.called.should.equal true
  66. it "should log out the compile", ->
  67. @logger.log
  68. .calledWith(project_id: @project_id, user_id: @user_id, "compiling project")
  69. .should.equal true
  70. describe "when the project has been recently compiled", ->
  71. beforeEach ->
  72. @CompileManager._checkIfAutoCompileLimitHasBeenHit = (_, cb)-> cb(null, true)
  73. @CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, true)
  74. @CompileManager.compile @project_id, @user_id, {}, @callback
  75. it "should return the callback with an error", ->
  76. @callback
  77. .calledWith(new Error("project was recently compiled so not continuing"))
  78. .should.equal true
  79. describe "should check the rate limit", ->
  80. it "should return", (done)->
  81. @CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon.stub().callsArgWith(1, null, false)
  82. @CompileManager.compile @project_id, @user_id, {}, (err)->
  83. done()
  84. it "should not error if the autocompile limit has not been hit", (done)->
  85. @CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon.stub().callsArgWith(1, null, true)
  86. @CompileManager.compile @project_id, @user_id, {}, (err)->
  87. assert.equal null, err
  88. done()
  89. describe "getLogLines", ->
  90. beforeEach ->
  91. @ClsiManager.getLogLines = sinon.stub().callsArgWith(1, null, @lines = ["log", "lines"])
  92. @CompileManager.getLogLines @project_id, @callback
  93. it "should call the new api", ->
  94. @ClsiManager.getLogLines
  95. .calledWith(@project_id)
  96. .should.equal true
  97. it "should call the callback with the lines", ->
  98. @callback
  99. .calledWith(null, @lines)
  100. .should.equal true
  101. it "should increase the log count metric", ->
  102. @Metrics.inc
  103. .calledWith("editor.raw-logs")
  104. .should.equal true
  105. describe "_checkIfRecentlyCompiled", ->
  106. describe "when the key exists in redis", ->
  107. beforeEach ->
  108. @rclient.set = sinon.stub().callsArgWith(5, null, null)
  109. @CompileManager._checkIfRecentlyCompiled(@project_id, @user_id, @callback)
  110. it "should try to set the key", ->
  111. @rclient.set
  112. .calledWith("compile:#{@project_id}:#{@user_id}", true, "EX", @CompileManager.COMPILE_DELAY, "NX")
  113. .should.equal true
  114. it "should call the callback with true", ->
  115. @callback.calledWith(null, true).should.equal true
  116. describe "when the key does not exist in redis", ->
  117. beforeEach ->
  118. @rclient.set = sinon.stub().callsArgWith(5, null, "OK")
  119. @CompileManager._checkIfRecentlyCompiled(@project_id, @user_id, @callback)
  120. it "should try to set the key", ->
  121. @rclient.set
  122. .calledWith("compile:#{@project_id}:#{@user_id}", true, "EX", @CompileManager.COMPILE_DELAY, "NX")
  123. .should.equal true
  124. it "should call the callback with false", ->
  125. @callback.calledWith(null, false).should.equal true
  126. describe "_ensureRootDocumentIsSet", ->
  127. beforeEach ->
  128. @project = {}
  129. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  130. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  131. describe "when the root doc is set", ->
  132. beforeEach ->
  133. @project.rootDoc_id = "root-doc-id"
  134. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  135. it "should find the project with only the rootDoc_id fiel", ->
  136. @Project.findById
  137. .calledWith(@project_id, "rootDoc_id")
  138. .should.equal true
  139. it "should not try to update the project rootDoc_id", ->
  140. @ProjectRootDocManager.setRootDocAutomatically
  141. .called.should.equal false
  142. it "should call the callback", ->
  143. @callback.called.should.equal true
  144. describe "when the root doc is not set", ->
  145. beforeEach ->
  146. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  147. it "should find the project with only the rootDoc_id fiel", ->
  148. @Project.findById
  149. .calledWith(@project_id, "rootDoc_id")
  150. .should.equal true
  151. it "should update the project rootDoc_id", ->
  152. @ProjectRootDocManager.setRootDocAutomatically
  153. .calledWith(@project_id)
  154. .should.equal true
  155. it "should call the callback", ->
  156. @callback.called.should.equal true
  157. describe "when the project does not exist", ->
  158. beforeEach ->
  159. @Project.findById = sinon.stub().callsArgWith(2, null, null)
  160. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  161. it "should call the callback with an error", ->
  162. @callback.calledWith(new Error("project not found")).should.equal true
  163. describe "_checkIfAutoCompileLimitHasBeenHit", ->
  164. it "should be able to compile if it is not an autocompile", (done)->
  165. @ratelimiter.addCount.callsArgWith(1, null, true)
  166. @CompileManager._checkIfAutoCompileLimitHasBeenHit false, (err, canCompile)=>
  167. canCompile.should.equal true
  168. done()
  169. it "should be able to compile if rate limit has remianing", (done)->
  170. @ratelimiter.addCount.callsArgWith(1, null, true)
  171. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, (err, canCompile)=>
  172. args = @ratelimiter.addCount.args[0][0]
  173. args.throttle.should.equal 10
  174. args.subjectName.should.equal "everyone"
  175. args.timeInterval.should.equal 15
  176. args.endpointName.should.equal "auto_compile"
  177. canCompile.should.equal true
  178. done()
  179. it "should be not able to compile if rate limit has no remianing", (done)->
  180. @ratelimiter.addCount.callsArgWith(1, null, false)
  181. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, (err, canCompile)=>
  182. canCompile.should.equal false
  183. done()
  184. it "should return false if there is an error in the rate limit", (done)->
  185. @ratelimiter.addCount.callsArgWith(1, "error")
  186. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, (err, canCompile)=>
  187. canCompile.should.equal false
  188. done()