HttpControllerTests.js 5.8 KB

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