HttpControllerTests.js 5.8 KB

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