flushAndDeleteProjectTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS206: Consider reworking classes to avoid initClass
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon');
  16. const chai = require('chai');
  17. const should = chai.should();
  18. const modulePath = "../../../../app/js/ProjectManager.js";
  19. const SandboxedModule = require('sandboxed-module');
  20. describe("ProjectManager - flushAndDeleteProject", function() {
  21. beforeEach(function() {
  22. let Timer;
  23. this.ProjectManager = SandboxedModule.require(modulePath, { requires: {
  24. "./RedisManager": (this.RedisManager = {}),
  25. "./ProjectHistoryRedisManager": (this.ProjectHistoryRedisManager = {}),
  26. "./DocumentManager": (this.DocumentManager = {}),
  27. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  28. "./HistoryManager": (this.HistoryManager =
  29. {flushProjectChanges: sinon.stub().callsArg(2)}),
  30. "./Metrics": (this.Metrics = {
  31. Timer: (Timer = (function() {
  32. Timer = class Timer {
  33. static initClass() {
  34. this.prototype.done = sinon.stub();
  35. }
  36. };
  37. Timer.initClass();
  38. return Timer;
  39. })())
  40. })
  41. }
  42. }
  43. );
  44. this.project_id = "project-id-123";
  45. return this.callback = sinon.stub();
  46. });
  47. describe("successfully", function() {
  48. beforeEach(function(done) {
  49. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  50. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  51. this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().callsArg(3);
  52. return this.ProjectManager.flushAndDeleteProjectWithLocks(this.project_id, {}, error => {
  53. this.callback(error);
  54. return done();
  55. });
  56. });
  57. it("should get the doc ids in the project", function() {
  58. return this.RedisManager.getDocIdsInProject
  59. .calledWith(this.project_id)
  60. .should.equal(true);
  61. });
  62. it("should delete each doc in the project", function() {
  63. return Array.from(this.doc_ids).map((doc_id) =>
  64. this.DocumentManager.flushAndDeleteDocWithLock
  65. .calledWith(this.project_id, doc_id, {})
  66. .should.equal(true));
  67. });
  68. it("should flush project history", function() {
  69. return this.HistoryManager.flushProjectChanges
  70. .calledWith(this.project_id, {})
  71. .should.equal(true);
  72. });
  73. it("should call the callback without error", function() {
  74. return this.callback.calledWith(null).should.equal(true);
  75. });
  76. return it("should time the execution", function() {
  77. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  78. });
  79. });
  80. return describe("when a doc errors", function() {
  81. beforeEach(function(done) {
  82. this.doc_ids = ["doc-id-1", "doc-id-2", "doc-id-3"];
  83. this.RedisManager.getDocIdsInProject = sinon.stub().callsArgWith(1, null, this.doc_ids);
  84. this.DocumentManager.flushAndDeleteDocWithLock = sinon.spy((project_id, doc_id, options, callback) => {
  85. if (doc_id === "doc-id-1") {
  86. return callback(this.error = new Error("oops, something went wrong"));
  87. } else {
  88. return callback();
  89. }
  90. });
  91. return this.ProjectManager.flushAndDeleteProjectWithLocks(this.project_id, {}, error => {
  92. this.callback(error);
  93. return done();
  94. });
  95. });
  96. it("should still flush each doc in the project", function() {
  97. return Array.from(this.doc_ids).map((doc_id) =>
  98. this.DocumentManager.flushAndDeleteDocWithLock
  99. .calledWith(this.project_id, doc_id, {})
  100. .should.equal(true));
  101. });
  102. it("should still flush project history", function() {
  103. return this.HistoryManager.flushProjectChanges
  104. .calledWith(this.project_id, {})
  105. .should.equal(true);
  106. });
  107. it("should record the error", function() {
  108. return this.logger.error
  109. .calledWith({err: this.error, project_id: this.project_id, doc_id: "doc-id-1"}, "error deleting doc")
  110. .should.equal(true);
  111. });
  112. it("should call the callback with an error", function() {
  113. return this.callback.calledWith(new Error()).should.equal(true);
  114. });
  115. return it("should time the execution", function() {
  116. return this.Metrics.Timer.prototype.done.called.should.equal(true);
  117. });
  118. });
  119. });