updateProjectTests.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS206: Consider reworking classes to avoid initClass
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon');
  14. const chai = require('chai');
  15. const should = chai.should();
  16. const modulePath = "../../../../app/js/ProjectManager.js";
  17. const SandboxedModule = require('sandboxed-module');
  18. const _ = require('lodash');
  19. describe("ProjectManager", function() {
  20. beforeEach(function() {
  21. let Timer;
  22. this.ProjectManager = SandboxedModule.require(modulePath, { requires: {
  23. "./RedisManager": (this.RedisManager = {}),
  24. "./ProjectHistoryRedisManager": (this.ProjectHistoryRedisManager = {}),
  25. "./DocumentManager": (this.DocumentManager = {}),
  26. "logger-sharelatex": (this.logger = { log: sinon.stub(), error: sinon.stub() }),
  27. "./HistoryManager": (this.HistoryManager = {}),
  28. "./Metrics": (this.Metrics = {
  29. Timer: (Timer = (function() {
  30. Timer = class Timer {
  31. static initClass() {
  32. this.prototype.done = sinon.stub();
  33. }
  34. };
  35. Timer.initClass();
  36. return Timer;
  37. })())
  38. })
  39. }
  40. }
  41. );
  42. this.project_id = "project-id-123";
  43. this.projectHistoryId = 'history-id-123';
  44. this.user_id = "user-id-123";
  45. this.version = 1234567;
  46. this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(false);
  47. this.HistoryManager.flushProjectChangesAsync = sinon.stub();
  48. return this.callback = sinon.stub();
  49. });
  50. return describe("updateProjectWithLocks", function() {
  51. describe("rename operations", function() {
  52. beforeEach(function() {
  53. this.firstDocUpdate = {
  54. id: 1,
  55. pathname: 'foo',
  56. newPathname: 'foo'
  57. };
  58. this.secondDocUpdate = {
  59. id: 2,
  60. pathname: 'bar',
  61. newPathname: 'bar2'
  62. };
  63. this.docUpdates = [ this.firstDocUpdate, this.secondDocUpdate ];
  64. this.firstFileUpdate = {
  65. id: 2,
  66. pathname: 'bar',
  67. newPathname: 'bar2'
  68. };
  69. this.fileUpdates = [ this.firstFileUpdate ];
  70. this.DocumentManager.renameDocWithLock = sinon.stub().yields();
  71. return this.ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields();
  72. });
  73. describe("successfully", function() {
  74. beforeEach(function() {
  75. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  76. });
  77. it("should rename the docs in the updates", function() {
  78. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {version: `${this.version}.0`});
  79. const secondDocUpdateWithVersion = _.extend({}, this.secondDocUpdate, {version: `${this.version}.1`});
  80. this.DocumentManager.renameDocWithLock
  81. .calledWith(this.project_id, this.firstDocUpdate.id, this.user_id, firstDocUpdateWithVersion, this.projectHistoryId)
  82. .should.equal(true);
  83. return this.DocumentManager.renameDocWithLock
  84. .calledWith(this.project_id, this.secondDocUpdate.id, this.user_id, secondDocUpdateWithVersion, this.projectHistoryId)
  85. .should.equal(true);
  86. });
  87. it("should rename the files in the updates", function() {
  88. const firstFileUpdateWithVersion = _.extend({}, this.firstFileUpdate, {version: `${this.version}.2`});
  89. return this.ProjectHistoryRedisManager.queueRenameEntity
  90. .calledWith(this.project_id, this.projectHistoryId, 'file', this.firstFileUpdate.id, this.user_id, firstFileUpdateWithVersion)
  91. .should.equal(true);
  92. });
  93. it("should not flush the history", function() {
  94. return this.HistoryManager.flushProjectChangesAsync
  95. .calledWith(this.project_id)
  96. .should.equal(false);
  97. });
  98. return it("should call the callback", function() {
  99. return this.callback.called.should.equal(true);
  100. });
  101. });
  102. describe("when renaming a doc fails", function() {
  103. beforeEach(function() {
  104. this.error = new Error('error');
  105. this.DocumentManager.renameDocWithLock = sinon.stub().yields(this.error);
  106. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  107. });
  108. return it("should call the callback with the error", function() {
  109. return this.callback.calledWith(this.error).should.equal(true);
  110. });
  111. });
  112. describe("when renaming a file fails", function() {
  113. beforeEach(function() {
  114. this.error = new Error('error');
  115. this.ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields(this.error);
  116. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  117. });
  118. return it("should call the callback with the error", function() {
  119. return this.callback.calledWith(this.error).should.equal(true);
  120. });
  121. });
  122. return describe("with enough ops to flush", function() {
  123. beforeEach(function() {
  124. this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true);
  125. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  126. });
  127. return it("should flush the history", function() {
  128. return this.HistoryManager.flushProjectChangesAsync
  129. .calledWith(this.project_id)
  130. .should.equal(true);
  131. });
  132. });
  133. });
  134. return describe("add operations", function() {
  135. beforeEach(function() {
  136. this.firstDocUpdate = {
  137. id: 1,
  138. docLines: "a\nb"
  139. };
  140. this.secondDocUpdate = {
  141. id: 2,
  142. docLines: "a\nb"
  143. };
  144. this.docUpdates = [ this.firstDocUpdate, this.secondDocUpdate ];
  145. this.firstFileUpdate = {
  146. id: 3,
  147. url: 'filestore.example.com/2'
  148. };
  149. this.secondFileUpdate = {
  150. id: 4,
  151. url: 'filestore.example.com/3'
  152. };
  153. this.fileUpdates = [ this.firstFileUpdate, this.secondFileUpdate ];
  154. return this.ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields();
  155. });
  156. describe("successfully", function() {
  157. beforeEach(function() {
  158. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  159. });
  160. it("should add the docs in the updates", function() {
  161. const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {version: `${this.version}.0`});
  162. const secondDocUpdateWithVersion = _.extend({}, this.secondDocUpdate, {version: `${this.version}.1`});
  163. this.ProjectHistoryRedisManager.queueAddEntity.getCall(0)
  164. .calledWith(this.project_id, this.projectHistoryId, 'doc', this.firstDocUpdate.id, this.user_id, firstDocUpdateWithVersion)
  165. .should.equal(true);
  166. return this.ProjectHistoryRedisManager.queueAddEntity.getCall(1)
  167. .calledWith(this.project_id, this.projectHistoryId, 'doc', this.secondDocUpdate.id, this.user_id, secondDocUpdateWithVersion)
  168. .should.equal(true);
  169. });
  170. it("should add the files in the updates", function() {
  171. const firstFileUpdateWithVersion = _.extend({}, this.firstFileUpdate, {version: `${this.version}.2`});
  172. const secondFileUpdateWithVersion = _.extend({}, this.secondFileUpdate, {version: `${this.version}.3`});
  173. this.ProjectHistoryRedisManager.queueAddEntity.getCall(2)
  174. .calledWith(this.project_id, this.projectHistoryId, 'file', this.firstFileUpdate.id, this.user_id, firstFileUpdateWithVersion)
  175. .should.equal(true);
  176. return this.ProjectHistoryRedisManager.queueAddEntity.getCall(3)
  177. .calledWith(this.project_id, this.projectHistoryId, 'file', this.secondFileUpdate.id, this.user_id, secondFileUpdateWithVersion)
  178. .should.equal(true);
  179. });
  180. it("should not flush the history", function() {
  181. return this.HistoryManager.flushProjectChangesAsync
  182. .calledWith(this.project_id)
  183. .should.equal(false);
  184. });
  185. return it("should call the callback", function() {
  186. return this.callback.called.should.equal(true);
  187. });
  188. });
  189. describe("when adding a doc fails", function() {
  190. beforeEach(function() {
  191. this.error = new Error('error');
  192. this.ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(this.error);
  193. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  194. });
  195. return it("should call the callback with the error", function() {
  196. return this.callback.calledWith(this.error).should.equal(true);
  197. });
  198. });
  199. describe("when adding a file fails", function() {
  200. beforeEach(function() {
  201. this.error = new Error('error');
  202. this.ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(this.error);
  203. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  204. });
  205. return it("should call the callback with the error", function() {
  206. return this.callback.calledWith(this.error).should.equal(true);
  207. });
  208. });
  209. return describe("with enough ops to flush", function() {
  210. beforeEach(function() {
  211. this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true);
  212. return this.ProjectManager.updateProjectWithLocks(this.project_id, this.projectHistoryId, this.user_id, this.docUpdates, this.fileUpdates, this.version, this.callback);
  213. });
  214. return it("should flush the history", function() {
  215. return this.HistoryManager.flushProjectChangesAsync
  216. .calledWith(this.project_id)
  217. .should.equal(true);
  218. });
  219. });
  220. });
  221. });
  222. });