CompileManagerTests.coffee 9.9 KB

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