flushAndDeleteProjectTests.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "./HistoryManager": @HistoryManager =
  13. flushProjectChangesAsync: sinon.stub()
  14. "./Metrics": @Metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. @project_id = "project-id-123"
  18. @callback = sinon.stub()
  19. describe "successfully", ->
  20. beforeEach (done) ->
  21. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  22. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  23. @DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(2)
  24. @ProjectManager.flushAndDeleteProjectWithLocks @project_id, (error) =>
  25. @callback(error)
  26. done()
  27. it "should get the doc ids in the project", ->
  28. @RedisManager.getDocIdsInProject
  29. .calledWith(@project_id)
  30. .should.equal true
  31. it "should delete each doc in the project", ->
  32. for doc_id in @doc_ids
  33. @DocumentManager.flushAndDeleteDocWithLock
  34. .calledWith(@project_id, doc_id)
  35. .should.equal true
  36. it "should flush project history", ->
  37. @HistoryManager.flushProjectChangesAsync
  38. .calledWithExactly(@project_id)
  39. .should.equal true
  40. it "should call the callback without error", ->
  41. @callback.calledWith(null).should.equal true
  42. it "should time the execution", ->
  43. @Metrics.Timer::done.called.should.equal true
  44. describe "when a doc errors", ->
  45. beforeEach (done) ->
  46. @doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"]
  47. @RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, @doc_ids)
  48. @DocumentManager.flushAndDeleteDocWithLock = sinon.spy (project_id, doc_id, callback = (error) ->) =>
  49. if doc_id == "doc-id-1"
  50. callback(@error = new Error("oops, something went wrong"))
  51. else
  52. callback()
  53. @ProjectManager.flushAndDeleteProjectWithLocks @project_id, (error) =>
  54. @callback(error)
  55. done()
  56. it "should still flush each doc in the project", ->
  57. for doc_id in @doc_ids
  58. @DocumentManager.flushAndDeleteDocWithLock
  59. .calledWith(@project_id, doc_id)
  60. .should.equal true
  61. it "should still flush project history", ->
  62. @HistoryManager.flushProjectChangesAsync
  63. .calledWithExactly(@project_id)
  64. .should.equal true
  65. it "should record the error", ->
  66. @logger.error
  67. .calledWith(err: @error, project_id: @project_id, doc_id: "doc-id-1", "error deleting doc")
  68. .should.equal true
  69. it "should call the callback with an error", ->
  70. @callback.calledWith(new Error()).should.equal true
  71. it "should time the execution", ->
  72. @Metrics.Timer::done.called.should.equal true