HttpControllerTests.js 5.0 KB

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