flushAndDeleteProjectTests.coffee 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/ProjectManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ProjectManager - flushAndDeleteProject", ->
  7. beforeEach ->
  8. @ProjectManager = SandboxedModule.require modulePath, requires:
  9. "./RedisManager": @RedisManager = {}
  10. "./DocumentManager": @DocumentManager = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. "./Metrics": @Metrics =
  13. Timer: class Timer
  14. done: sinon.stub()
  15. @project_id = "project-id-123"
  16. @callback = sinon.stub()
  17. describe "successfully", ->
  18. beforeEach (done) ->
  19. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  20. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  21. @DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(2)
  22. @ProjectManager.flushAndDeleteProjectWithLocks @project_id, (error) =>
  23. @callback(error)
  24. done()
  25. it "should get the doc ids in the project", ->
  26. @RedisManager.getDocIdsInProject
  27. .calledWith(@project_id)
  28. .should.equal true
  29. it "should delete each doc in the project", ->
  30. for doc_id in @doc_ids
  31. @DocumentManager.flushAndDeleteDocWithLock
  32. .calledWith(@project_id, doc_id)
  33. .should.equal true
  34. it "should call the callback without error", ->
  35. @callback.calledWith(null).should.equal true
  36. it "should time the execution", ->
  37. @Metrics.Timer::done.called.should.equal true
  38. describe "when a doc errors", ->
  39. beforeEach (done) ->
  40. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  41. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  42. @DocumentManager.flushAndDeleteDocWithLock = sinon.spy (project_id, doc_id, callback = (error) ->) =>
  43. if doc_id == "doc-id-1"
  44. callback(@error = new Error("oops, something went wrong"))
  45. else
  46. callback()
  47. @ProjectManager.flushAndDeleteProjectWithLocks @project_id, (error) =>
  48. @callback(error)
  49. done()
  50. it "should still flush each doc in the project", ->
  51. for doc_id in @doc_ids
  52. @DocumentManager.flushAndDeleteDocWithLock
  53. .calledWith(@project_id, doc_id)
  54. .should.equal true
  55. it "should record the error", ->
  56. @logger.error
  57. .calledWith(err: @error, project_id: @project_id, doc_id: "doc-id-1", "error deleting doc")
  58. .should.equal true
  59. it "should call the callback with an error", ->
  60. @callback.calledWith(new Error()).should.equal true
  61. it "should time the execution", ->
  62. @Metrics.Timer::done.called.should.equal true