HttpControllerTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/HttpController.js";
  17. const SandboxedModule = require('sandboxed-module');
  18. describe("HttpController", function() {
  19. beforeEach(function() {
  20. this.HttpController = SandboxedModule.require(modulePath, { requires: {
  21. "logger-sharelatex": { log: sinon.stub() },
  22. "./UpdatesManager": (this.UpdatesManager = {}),
  23. "./DiffManager": (this.DiffManager = {}),
  24. "./RestoreManager": (this.RestoreManager = {}),
  25. "./PackManager": (this.PackManager = {}),
  26. "./DocArchiveManager": (this.DocArchiveManager = {}),
  27. "./HealthChecker": (this.HealthChecker = {})
  28. }
  29. });
  30. this.doc_id = "doc-id-123";
  31. this.project_id = "project-id-123";
  32. this.next = sinon.stub();
  33. this.user_id = "mock-user-123";
  34. return this.now = Date.now();
  35. });
  36. describe("flushDoc", function() {
  37. beforeEach(function() {
  38. this.req = {
  39. params: {
  40. doc_id: this.doc_id,
  41. project_id: this.project_id
  42. }
  43. };
  44. this.res =
  45. {send: sinon.stub()};
  46. this.UpdatesManager.processUncompressedUpdatesWithLock = sinon.stub().callsArg(2);
  47. return this.HttpController.flushDoc(this.req, this.res, this.next);
  48. });
  49. it("should process the updates", function() {
  50. return this.UpdatesManager.processUncompressedUpdatesWithLock
  51. .calledWith(this.project_id, this.doc_id)
  52. .should.equal(true);
  53. });
  54. return it("should return a success code", function() {
  55. return this.res.send.calledWith(204).should.equal(true);
  56. });
  57. });
  58. describe("flushProject", function() {
  59. beforeEach(function() {
  60. this.req = {
  61. params: {
  62. project_id: this.project_id
  63. }
  64. };
  65. this.res =
  66. {send: sinon.stub()};
  67. this.UpdatesManager.processUncompressedUpdatesForProject = sinon.stub().callsArg(1);
  68. return this.HttpController.flushProject(this.req, this.res, this.next);
  69. });
  70. it("should process the updates", function() {
  71. return this.UpdatesManager.processUncompressedUpdatesForProject
  72. .calledWith(this.project_id)
  73. .should.equal(true);
  74. });
  75. return it("should return a success code", function() {
  76. return this.res.send.calledWith(204).should.equal(true);
  77. });
  78. });
  79. describe("getDiff", function() {
  80. beforeEach(function() {
  81. this.from = 42;
  82. this.to = 45;
  83. this.req = {
  84. params: {
  85. doc_id: this.doc_id,
  86. project_id: this.project_id
  87. },
  88. query: {
  89. from: this.from.toString(),
  90. to: this.to.toString()
  91. }
  92. };
  93. this.res =
  94. {json: sinon.stub()};
  95. this.diff = [ {u: "mock-diff"} ];
  96. this.DiffManager.getDiff = sinon.stub().callsArgWith(4, null, this.diff);
  97. return this.HttpController.getDiff(this.req, this.res, this.next);
  98. });
  99. it("should get the diff", function() {
  100. return this.DiffManager.getDiff
  101. .calledWith(this.project_id, this.doc_id, parseInt(this.from, 10), parseInt(this.to, 10))
  102. .should.equal(true);
  103. });
  104. return it("should return the diff", function() {
  105. return this.res.json.calledWith({diff: this.diff}).should.equal(true);
  106. });
  107. });
  108. describe("getUpdates", function() {
  109. beforeEach(function() {
  110. this.before = Date.now();
  111. this.nextBeforeTimestamp = this.before - 100;
  112. this.min_count = 10;
  113. this.req = {
  114. params: {
  115. project_id: this.project_id
  116. },
  117. query: {
  118. before: this.before.toString(),
  119. min_count: this.min_count.toString()
  120. }
  121. };
  122. this.res =
  123. {json: sinon.stub()};
  124. this.updates = ["mock-summarized-updates"];
  125. this.UpdatesManager.getSummarizedProjectUpdates = sinon.stub().callsArgWith(2, null, this.updates, this.nextBeforeTimestamp);
  126. return this.HttpController.getUpdates(this.req, this.res, this.next);
  127. });
  128. it("should get the updates", function() {
  129. return this.UpdatesManager.getSummarizedProjectUpdates
  130. .calledWith(this.project_id, {before: this.before, min_count: this.min_count})
  131. .should.equal(true);
  132. });
  133. return it("should return the formatted updates", function() {
  134. return this.res.json.calledWith({updates: this.updates, nextBeforeTimestamp: this.nextBeforeTimestamp}).should.equal(true);
  135. });
  136. });
  137. return describe("RestoreManager", function() {
  138. beforeEach(function() {
  139. this.version = "42";
  140. this.req = {
  141. params: {
  142. doc_id: this.doc_id,
  143. project_id: this.project_id,
  144. version: this.version
  145. },
  146. headers: {
  147. "x-user-id": this.user_id
  148. }
  149. };
  150. this.res =
  151. {send: sinon.stub()};
  152. this.RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4);
  153. return this.HttpController.restore(this.req, this.res, this.next);
  154. });
  155. it("should restore the document", function() {
  156. return this.RestoreManager.restoreToBeforeVersion
  157. .calledWith(this.project_id, this.doc_id, parseInt(this.version, 10), this.user_id)
  158. .should.equal(true);
  159. });
  160. return it("should return a success code", function() {
  161. return this.res.send.calledWith(204).should.equal(true);
  162. });
  163. });
  164. });