MongoManagerTests.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const sinon = require('sinon');
  13. const chai = require('chai');
  14. const should = chai.should();
  15. const { expect } = chai;
  16. const modulePath = "../../../../app/js/MongoManager.js";
  17. const packModulePath = "../../../../app/js/PackManager.js";
  18. const SandboxedModule = require('sandboxed-module');
  19. const {ObjectId} = require("mongojs");
  20. const tk = require("timekeeper");
  21. describe("MongoManager", function() {
  22. beforeEach(function() {
  23. tk.freeze(new Date());
  24. this.MongoManager = SandboxedModule.require(modulePath, { requires: {
  25. "./mongojs" : { db: (this.db = {}), ObjectId },
  26. "./PackManager" : (this.PackManager = {}),
  27. 'metrics-sharelatex': {timeAsyncMethod(){}},
  28. 'logger-sharelatex': {log(){}}
  29. }
  30. });
  31. this.callback = sinon.stub();
  32. this.doc_id = ObjectId().toString();
  33. return this.project_id = ObjectId().toString();
  34. });
  35. afterEach(function() { return tk.reset(); });
  36. describe("getLastCompressedUpdate", function() {
  37. beforeEach(function() {
  38. this.update = "mock-update";
  39. this.db.docHistory = {};
  40. this.db.docHistory.find = sinon.stub().returns(this.db.docHistory);
  41. this.db.docHistory.findOne = sinon.stub().returns(this.db.docHistory);
  42. this.db.docHistory.sort = sinon.stub().returns(this.db.docHistory);
  43. this.db.docHistory.limit = sinon.stub().returns(this.db.docHistory);
  44. this.db.docHistory.toArray = sinon.stub().callsArgWith(0, null, [this.update]);
  45. return this.MongoManager.getLastCompressedUpdate(this.doc_id, this.callback);
  46. });
  47. it("should find the updates for the doc", function() {
  48. return this.db.docHistory.find
  49. .calledWith({doc_id: ObjectId(this.doc_id)})
  50. .should.equal(true);
  51. });
  52. it("should limit to one result", function() {
  53. return this.db.docHistory.limit
  54. .calledWith(1)
  55. .should.equal(true);
  56. });
  57. it("should sort in descending version order", function() {
  58. return this.db.docHistory.sort
  59. .calledWith({v: -1})
  60. .should.equal(true);
  61. });
  62. return it("should call the call back with the update", function() {
  63. return this.callback.calledWith(null, this.update).should.equal(true);
  64. });
  65. });
  66. describe("peekLastCompressedUpdate", function() {
  67. describe("when there is no last update", function() {
  68. beforeEach(function() {
  69. this.PackManager.getLastPackFromIndex = sinon.stub().callsArgWith(1, null, null);
  70. this.MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null);
  71. return this.MongoManager.peekLastCompressedUpdate(this.doc_id, this.callback);
  72. });
  73. it("should get the last update", function() {
  74. return this.MongoManager.getLastCompressedUpdate
  75. .calledWith(this.doc_id)
  76. .should.equal(true);
  77. });
  78. return it("should call the callback with no update", function() {
  79. return this.callback.calledWith(null, null).should.equal(true);
  80. });
  81. });
  82. describe("when there is an update", function() {
  83. beforeEach(function() {
  84. this.update = { _id: Object() };
  85. this.MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, this.update);
  86. return this.MongoManager.peekLastCompressedUpdate(this.doc_id, this.callback);
  87. });
  88. it("should get the last update", function() {
  89. return this.MongoManager.getLastCompressedUpdate
  90. .calledWith(this.doc_id)
  91. .should.equal(true);
  92. });
  93. return it("should call the callback with the update", function() {
  94. return this.callback.calledWith(null, this.update).should.equal(true);
  95. });
  96. });
  97. return describe("when there is a last update in S3", function() {
  98. beforeEach(function() {
  99. this.update = { _id: Object(), v: 12345, v_end: 12345, inS3:true};
  100. this.PackManager.getLastPackFromIndex = sinon.stub().callsArgWith(1, null, this.update);
  101. this.MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null);
  102. return this.MongoManager.peekLastCompressedUpdate(this.doc_id, this.callback);
  103. });
  104. it("should get the last update", function() {
  105. return this.MongoManager.getLastCompressedUpdate
  106. .calledWith(this.doc_id)
  107. .should.equal(true);
  108. });
  109. return it("should call the callback with a null update and the correct version", function() {
  110. return this.callback.calledWith(null, null, this.update.v_end).should.equal(true);
  111. });
  112. });
  113. });
  114. describe("backportProjectId", function() {
  115. beforeEach(function() {
  116. this.db.docHistory =
  117. {update: sinon.stub().callsArg(3)};
  118. return this.MongoManager.backportProjectId(this.project_id, this.doc_id, this.callback);
  119. });
  120. it("should insert the project_id into all entries for the doc_id which don't have it set", function() {
  121. return this.db.docHistory.update
  122. .calledWith({
  123. doc_id: ObjectId(this.doc_id),
  124. project_id: { $exists: false }
  125. }, {
  126. $set: { project_id: ObjectId(this.project_id) }
  127. }, {
  128. multi: true
  129. })
  130. .should.equal(true);
  131. });
  132. return it("should call the callback", function() {
  133. return this.callback.called.should.equal(true);
  134. });
  135. });
  136. describe("getProjectMetaData", function() {
  137. beforeEach(function() {
  138. this.metadata = { "mock": "metadata" };
  139. this.db.projectHistoryMetaData =
  140. {find: sinon.stub().callsArgWith(1, null, [this.metadata])};
  141. return this.MongoManager.getProjectMetaData(this.project_id, this.callback);
  142. });
  143. it("should look up the meta data in the db", function() {
  144. return this.db.projectHistoryMetaData.find
  145. .calledWith({ project_id: ObjectId(this.project_id) })
  146. .should.equal(true);
  147. });
  148. return it("should return the metadata", function() {
  149. return this.callback.calledWith(null, this.metadata).should.equal(true);
  150. });
  151. });
  152. return describe("setProjectMetaData", function() {
  153. beforeEach(function() {
  154. this.metadata = { "mock": "metadata" };
  155. this.db.projectHistoryMetaData =
  156. {update: sinon.stub().callsArgWith(3, null, [this.metadata])};
  157. return this.MongoManager.setProjectMetaData(this.project_id, this.metadata, this.callback);
  158. });
  159. it("should upsert the metadata into the DB", function() {
  160. return this.db.projectHistoryMetaData.update
  161. .calledWith({
  162. project_id: ObjectId(this.project_id)
  163. }, {
  164. $set: this.metadata
  165. }, {
  166. upsert: true
  167. })
  168. .should.equal(true);
  169. });
  170. return it("should call the callback", function() {
  171. return this.callback.called.should.equal(true);
  172. });
  173. });
  174. });