CompileManagerTests.coffee 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "../../infrastructure/RedisWrapper":
  18. client: () => @rclient = { auth: () -> }
  19. "../Project/ProjectRootDocManager": @ProjectRootDocManager = {}
  20. "../../models/Project": Project: @Project = {}
  21. "../User/UserGetter": @UserGetter = {}
  22. "./ClsiManager": @ClsiManager = {}
  23. "../../infrastructure/RateLimiter": @ratelimiter
  24. "metrics-sharelatex": @Metrics =
  25. Timer: class Timer
  26. done: sinon.stub()
  27. inc: sinon.stub()
  28. "logger-sharelatex": @logger = { log: sinon.stub(), warn: sinon.stub() }
  29. @project_id = "mock-project-id-123"
  30. @user_id = "mock-user-id-123"
  31. @callback = sinon.stub()
  32. @limits = {
  33. timeout: 42
  34. }
  35. describe "compile", ->
  36. beforeEach ->
  37. @CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, false)
  38. @CompileManager._ensureRootDocumentIsSet = sinon.stub().callsArgWith(1, null)
  39. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith(1, null, @limits)
  40. @ClsiManager.sendRequest = sinon.stub().callsArgWith(3, null, @status = "mock-status", @outputFiles = "mock output files", @output = "mock output")
  41. describe "succesfully", ->
  42. beforeEach ->
  43. @CompileManager._checkIfAutoCompileLimitHasBeenHit = (isAutoCompile, compileGroup, cb)-> cb(null, true)
  44. @CompileManager.compile @project_id, @user_id, {}, @callback
  45. it "should check the project has not been recently compiled", ->
  46. @CompileManager._checkIfRecentlyCompiled
  47. .calledWith(@project_id, @user_id)
  48. .should.equal true
  49. it "should ensure that the root document is set", ->
  50. @CompileManager._ensureRootDocumentIsSet
  51. .calledWith(@project_id)
  52. .should.equal true
  53. it "should get the project compile limits", ->
  54. @CompileManager.getProjectCompileLimits
  55. .calledWith(@project_id)
  56. .should.equal true
  57. it "should run the compile with the compile limits", ->
  58. @ClsiManager.sendRequest
  59. .calledWith(@project_id, @user_id, {
  60. timeout: @limits.timeout
  61. })
  62. .should.equal true
  63. it "should call the callback with the output", ->
  64. @callback
  65. .calledWith(null, @status, @outputFiles, @output)
  66. .should.equal true
  67. it "should time the compile", ->
  68. @Metrics.Timer::done.called.should.equal true
  69. it "should log out the compile", ->
  70. @logger.log
  71. .calledWith(project_id: @project_id, user_id: @user_id, "compiling project")
  72. .should.equal true
  73. describe "when the project has been recently compiled", ->
  74. it "should return", (done)->
  75. @CompileManager._checkIfAutoCompileLimitHasBeenHit = (isAutoCompile, compileGroup, cb)-> cb(null, true)
  76. @CompileManager._checkIfRecentlyCompiled = sinon.stub().callsArgWith(2, null, true)
  77. @CompileManager.compile @project_id, @user_id, {}, (err, status)->
  78. status.should.equal "too-recently-compiled"
  79. done()
  80. describe "should check the rate limit", ->
  81. it "should return", (done)->
  82. @CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon.stub().callsArgWith(2, null, false)
  83. @CompileManager.compile @project_id, @user_id, {}, (err, status)->
  84. status.should.equal "autocompile-backoff"
  85. done()
  86. describe "getProjectCompileLimits", ->
  87. beforeEach ->
  88. @features = {
  89. compileTimeout: @timeout = 42
  90. compileGroup: @group = "priority"
  91. }
  92. @Project.findById = sinon.stub().callsArgWith(2, null, @project = { owner_ref: @owner_id = "owner-id-123" })
  93. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user = { features: @features })
  94. @CompileManager.getProjectCompileLimits @project_id, @callback
  95. it "should look up the owner of the project", ->
  96. @Project.findById
  97. .calledWith(@project_id, { owner_ref: 1 })
  98. .should.equal true
  99. it "should look up the owner's features", ->
  100. @UserGetter.getUser
  101. .calledWith(@project.owner_ref, { features: 1 })
  102. .should.equal true
  103. it "should return the limits", ->
  104. @callback
  105. .calledWith(null, {
  106. timeout: @timeout
  107. compileGroup: @group
  108. })
  109. .should.equal true
  110. describe "deleteAuxFiles", ->
  111. beforeEach ->
  112. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith 1, null, @limits = { compileGroup: "mock-compile-group" }
  113. @ClsiManager.deleteAuxFiles = sinon.stub().callsArg(3)
  114. @CompileManager.deleteAuxFiles @project_id, @user_id, @callback
  115. it "should look up the compile group to use", ->
  116. @CompileManager.getProjectCompileLimits
  117. .calledWith(@project_id)
  118. .should.equal true
  119. it "should delete the aux files", ->
  120. @ClsiManager.deleteAuxFiles
  121. .calledWith(@project_id, @user_id, @limits)
  122. .should.equal true
  123. it "should call the callback", ->
  124. @callback.called.should.equal true
  125. describe "_checkIfRecentlyCompiled", ->
  126. describe "when the key exists in redis", ->
  127. beforeEach ->
  128. @rclient.set = sinon.stub().callsArgWith(5, null, null)
  129. @CompileManager._checkIfRecentlyCompiled(@project_id, @user_id, @callback)
  130. it "should try to set the key", ->
  131. @rclient.set
  132. .calledWith("compile:#{@project_id}:#{@user_id}", true, "EX", @CompileManager.COMPILE_DELAY, "NX")
  133. .should.equal true
  134. it "should call the callback with true", ->
  135. @callback.calledWith(null, true).should.equal true
  136. describe "when the key does not exist in redis", ->
  137. beforeEach ->
  138. @rclient.set = sinon.stub().callsArgWith(5, null, "OK")
  139. @CompileManager._checkIfRecentlyCompiled(@project_id, @user_id, @callback)
  140. it "should try to set the key", ->
  141. @rclient.set
  142. .calledWith("compile:#{@project_id}:#{@user_id}", true, "EX", @CompileManager.COMPILE_DELAY, "NX")
  143. .should.equal true
  144. it "should call the callback with false", ->
  145. @callback.calledWith(null, false).should.equal true
  146. describe "_ensureRootDocumentIsSet", ->
  147. beforeEach ->
  148. @project = {}
  149. @Project.findById = sinon.stub().callsArgWith(2, null, @project)
  150. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  151. describe "when the root doc is set", ->
  152. beforeEach ->
  153. @project.rootDoc_id = "root-doc-id"
  154. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  155. it "should find the project with only the rootDoc_id fiel", ->
  156. @Project.findById
  157. .calledWith(@project_id, "rootDoc_id")
  158. .should.equal true
  159. it "should not try to update the project rootDoc_id", ->
  160. @ProjectRootDocManager.setRootDocAutomatically
  161. .called.should.equal false
  162. it "should call the callback", ->
  163. @callback.called.should.equal true
  164. describe "when the root doc is not set", ->
  165. beforeEach ->
  166. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  167. it "should find the project with only the rootDoc_id fiel", ->
  168. @Project.findById
  169. .calledWith(@project_id, "rootDoc_id")
  170. .should.equal true
  171. it "should update the project rootDoc_id", ->
  172. @ProjectRootDocManager.setRootDocAutomatically
  173. .calledWith(@project_id)
  174. .should.equal true
  175. it "should call the callback", ->
  176. @callback.called.should.equal true
  177. describe "when the project does not exist", ->
  178. beforeEach ->
  179. @Project.findById = sinon.stub().callsArgWith(2, null, null)
  180. @CompileManager._ensureRootDocumentIsSet(@project_id, @callback)
  181. it "should call the callback with an error", ->
  182. @callback.calledWith(new Error("project not found")).should.equal true
  183. describe "_checkIfAutoCompileLimitHasBeenHit", ->
  184. it "should be able to compile if it is not an autocompile", (done)->
  185. @ratelimiter.addCount.callsArgWith(2, null, true)
  186. @CompileManager._checkIfAutoCompileLimitHasBeenHit false, "everyone", (err, canCompile)=>
  187. canCompile.should.equal true
  188. done()
  189. it "should be able to compile if rate limit has remianing", (done)->
  190. @ratelimiter.addCount.callsArgWith(1, null, true)
  191. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, "everyone", (err, canCompile)=>
  192. args = @ratelimiter.addCount.args[0][0]
  193. args.throttle.should.equal 25
  194. args.subjectName.should.equal "everyone"
  195. args.timeInterval.should.equal 20
  196. args.endpointName.should.equal "auto_compile"
  197. canCompile.should.equal true
  198. done()
  199. it "should be not able to compile if rate limit has no remianing", (done)->
  200. @ratelimiter.addCount.callsArgWith(1, null, false)
  201. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, "everyone", (err, canCompile)=>
  202. canCompile.should.equal false
  203. done()
  204. it "should return false if there is an error in the rate limit", (done)->
  205. @ratelimiter.addCount.callsArgWith(1, "error")
  206. @CompileManager._checkIfAutoCompileLimitHasBeenHit true, "everyone", (err, canCompile)=>
  207. canCompile.should.equal false
  208. done()
  209. describe "wordCount", ->
  210. beforeEach ->
  211. @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith 1, null, @limits = { compileGroup: "mock-compile-group" }
  212. @ClsiManager.wordCount = sinon.stub().callsArg(4)
  213. @CompileManager.wordCount @project_id, @user_id, false, @callback
  214. it "should look up the compile group to use", ->
  215. @CompileManager.getProjectCompileLimits
  216. .calledWith(@project_id)
  217. .should.equal true
  218. it "should call wordCount for project", ->
  219. @ClsiManager.wordCount
  220. .calledWith(@project_id, @user_id, false, @limits)
  221. .should.equal true
  222. it "should call the callback", ->
  223. @callback.called.should.equal true