flushAndDeleteProjectTests.js 2.9 KB

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