HttpControllerTests.js 5.8 KB

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