HttpControllerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. import sinon from 'sinon'
  2. import { strict as esmock } from 'esmock'
  3. import mongodb from 'mongodb-legacy'
  4. const { ObjectId } = mongodb
  5. const MODULE_PATH = '../../../../app/js/HttpController.js'
  6. describe('HttpController', function () {
  7. beforeEach(async function () {
  8. this.UpdatesProcessor = {
  9. processUpdatesForProject: sinon.stub().yields(),
  10. }
  11. this.SummarizedUpdatesManager = {
  12. getSummarizedProjectUpdates: sinon.stub(),
  13. }
  14. this.DiffManager = {
  15. getDiff: sinon.stub(),
  16. }
  17. this.HistoryStoreManager = {
  18. deleteProject: sinon.stub().yields(),
  19. getMostRecentVersion: sinon.stub(),
  20. getProjectBlobStream: sinon.stub(),
  21. initializeProject: sinon.stub(),
  22. }
  23. this.SnapshotManager = {
  24. getFileSnapshotStream: sinon.stub(),
  25. getProjectSnapshot: sinon.stub(),
  26. }
  27. this.HealthChecker = {}
  28. this.SyncManager = {
  29. clearResyncState: sinon.stub().yields(),
  30. startResync: sinon.stub().yields(),
  31. }
  32. this.WebApiManager = {
  33. getHistoryId: sinon.stub(),
  34. }
  35. this.RedisManager = {
  36. destroyDocUpdatesQueue: sinon.stub().yields(),
  37. clearFirstOpTimestamp: sinon.stub().yields(),
  38. clearCachedHistoryId: sinon.stub().yields(),
  39. }
  40. this.ErrorRecorder = {
  41. clearError: sinon.stub().yields(),
  42. }
  43. this.LabelsManager = {
  44. createLabel: sinon.stub(),
  45. deleteLabel: sinon.stub().yields(),
  46. deleteLabelForUser: sinon.stub().yields(),
  47. getLabels: sinon.stub(),
  48. }
  49. this.HistoryApiManager = {
  50. shouldUseProjectHistory: sinon.stub(),
  51. }
  52. this.RetryManager = {}
  53. this.FlushManager = {}
  54. this.pipeline = sinon.stub()
  55. this.HttpController = await esmock(MODULE_PATH, {
  56. stream: { pipeline: this.pipeline },
  57. '../../../../app/js/UpdatesProcessor.js': this.UpdatesProcessor,
  58. '../../../../app/js/SummarizedUpdatesManager.js':
  59. this.SummarizedUpdatesManager,
  60. '../../../../app/js/DiffManager.js': this.DiffManager,
  61. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  62. '../../../../app/js/SnapshotManager.js': this.SnapshotManager,
  63. '../../../../app/js/HealthChecker.js': this.HealthChecker,
  64. '../../../../app/js/SyncManager.js': this.SyncManager,
  65. '../../../../app/js/WebApiManager.js': this.WebApiManager,
  66. '../../../../app/js/RedisManager.js': this.RedisManager,
  67. '../../../../app/js/ErrorRecorder.js': this.ErrorRecorder,
  68. '../../../../app/js/LabelsManager.js': this.LabelsManager,
  69. '../../../../app/js/HistoryApiManager.js': this.HistoryApiManager,
  70. '../../../../app/js/RetryManager.js': this.RetryManager,
  71. '../../../../app/js/FlushManager.js': this.FlushManager,
  72. })
  73. this.pathname = 'doc-id-123'
  74. this.projectId = new ObjectId().toString()
  75. this.projectOwnerId = new ObjectId().toString()
  76. this.next = sinon.stub()
  77. this.userId = new ObjectId().toString()
  78. this.now = Date.now()
  79. this.res = {
  80. json: sinon.stub(),
  81. send: sinon.stub(),
  82. sendStatus: sinon.stub(),
  83. setHeader: sinon.stub(),
  84. }
  85. })
  86. describe('getProjectBlob', function () {
  87. beforeEach(function () {
  88. this.blobHash = 'abcd'
  89. this.stream = {}
  90. this.historyId = new ObjectId().toString()
  91. this.HistoryStoreManager.getProjectBlobStream.yields(null, this.stream)
  92. this.HttpController.getProjectBlob(
  93. { params: { history_id: this.historyId, hash: this.blobHash } },
  94. this.res,
  95. this.next
  96. )
  97. })
  98. it('should get a blob stream', function () {
  99. this.HistoryStoreManager.getProjectBlobStream
  100. .calledWith(this.historyId, this.blobHash)
  101. .should.equal(true)
  102. this.pipeline.should.have.been.calledWith(this.stream, this.res)
  103. })
  104. it('should set caching header', function () {
  105. this.res.setHeader.should.have.been.calledWith(
  106. 'Cache-Control',
  107. 'private, max-age=86400'
  108. )
  109. })
  110. })
  111. describe('initializeProject', function () {
  112. beforeEach(function () {
  113. this.historyId = new ObjectId().toString()
  114. this.req = { body: { historyId: this.historyId } }
  115. this.HistoryStoreManager.initializeProject.yields(null, this.historyId)
  116. this.HttpController.initializeProject(this.req, this.res, this.next)
  117. })
  118. it('should initialize the project', function () {
  119. this.HistoryStoreManager.initializeProject.calledWith().should.equal(true)
  120. })
  121. it('should return the new overleaf id', function () {
  122. this.res.json
  123. .calledWith({ project: { id: this.historyId } })
  124. .should.equal(true)
  125. })
  126. })
  127. describe('flushProject', function () {
  128. beforeEach(function () {
  129. this.req = {
  130. params: {
  131. project_id: this.projectId,
  132. },
  133. query: {},
  134. }
  135. this.HttpController.flushProject(this.req, this.res, this.next)
  136. })
  137. it('should process the updates', function () {
  138. this.UpdatesProcessor.processUpdatesForProject
  139. .calledWith(this.projectId)
  140. .should.equal(true)
  141. })
  142. it('should return a success code', function () {
  143. this.res.sendStatus.calledWith(204).should.equal(true)
  144. })
  145. })
  146. describe('getDiff', function () {
  147. beforeEach(function () {
  148. this.from = 42
  149. this.to = 45
  150. this.req = {
  151. params: {
  152. project_id: this.projectId,
  153. },
  154. query: {
  155. pathname: this.pathname,
  156. from: this.from,
  157. to: this.to,
  158. },
  159. }
  160. this.diff = [{ u: 'mock-diff' }]
  161. this.DiffManager.getDiff.yields(null, this.diff)
  162. this.HttpController.getDiff(this.req, this.res, this.next)
  163. })
  164. it('should get the diff', function () {
  165. this.DiffManager.getDiff.should.have.been.calledWith(
  166. this.projectId,
  167. this.pathname,
  168. this.from,
  169. this.to
  170. )
  171. })
  172. it('should return the diff', function () {
  173. this.res.json.calledWith({ diff: this.diff }).should.equal(true)
  174. })
  175. })
  176. describe('getUpdates', function () {
  177. beforeEach(function () {
  178. this.before = Date.now()
  179. this.nextBeforeTimestamp = this.before - 100
  180. this.min_count = 10
  181. this.req = {
  182. params: {
  183. project_id: this.projectId,
  184. },
  185. query: {
  186. before: this.before,
  187. min_count: this.min_count,
  188. },
  189. }
  190. this.updates = [{ i: 'mock-summarized-updates', p: 10 }]
  191. this.SummarizedUpdatesManager.getSummarizedProjectUpdates.yields(
  192. null,
  193. this.updates,
  194. this.nextBeforeTimestamp
  195. )
  196. this.HttpController.getUpdates(this.req, this.res, this.next)
  197. })
  198. it('should get the updates', function () {
  199. this.SummarizedUpdatesManager.getSummarizedProjectUpdates.should.have.been.calledWith(
  200. this.projectId,
  201. {
  202. before: this.before,
  203. min_count: this.min_count,
  204. }
  205. )
  206. })
  207. it('should return the formatted updates', function () {
  208. this.res.json.should.have.been.calledWith({
  209. updates: this.updates,
  210. nextBeforeTimestamp: this.nextBeforeTimestamp,
  211. })
  212. })
  213. })
  214. describe('latestVersion', function () {
  215. beforeEach(function () {
  216. this.historyId = 1234
  217. this.req = {
  218. params: {
  219. project_id: this.projectId,
  220. },
  221. }
  222. this.version = 99
  223. this.lastChange = {
  224. v2Authors: ['1234'],
  225. timestamp: '2016-08-16T10:44:40.227Z',
  226. }
  227. this.versionInfo = {
  228. version: this.version,
  229. v2Authors: ['1234'],
  230. timestamp: '2016-08-16T10:44:40.227Z',
  231. }
  232. this.WebApiManager.getHistoryId.yields(null, this.historyId)
  233. this.HistoryStoreManager.getMostRecentVersion.yields(
  234. null,
  235. this.version,
  236. {},
  237. this.lastChange
  238. )
  239. this.HttpController.latestVersion(this.req, this.res, this.next)
  240. })
  241. it('should process the updates', function () {
  242. this.UpdatesProcessor.processUpdatesForProject
  243. .calledWith(this.projectId)
  244. .should.equal(true)
  245. })
  246. it('should get the ol project id', function () {
  247. this.WebApiManager.getHistoryId
  248. .calledWith(this.projectId)
  249. .should.equal(true)
  250. })
  251. it('should get the latest version', function () {
  252. this.HistoryStoreManager.getMostRecentVersion
  253. .calledWith(this.projectId, this.historyId)
  254. .should.equal(true)
  255. })
  256. it('should return version number', function () {
  257. this.res.json.calledWith(this.versionInfo).should.equal(true)
  258. })
  259. })
  260. describe('resyncProject', function () {
  261. beforeEach(function () {
  262. this.req = {
  263. params: {
  264. project_id: this.projectId,
  265. },
  266. query: {},
  267. body: {},
  268. }
  269. this.HttpController.resyncProject(this.req, this.res, this.next)
  270. })
  271. it('should resync the project', function () {
  272. this.SyncManager.startResync.calledWith(this.projectId).should.equal(true)
  273. })
  274. it('should flush the queue', function () {
  275. this.UpdatesProcessor.processUpdatesForProject
  276. .calledWith(this.projectId)
  277. .should.equal(true)
  278. })
  279. it('should return 204', function () {
  280. this.res.sendStatus.calledWith(204).should.equal(true)
  281. })
  282. })
  283. describe('getFileSnapshot', function () {
  284. beforeEach(function () {
  285. this.version = 42
  286. this.pathname = 'foo.tex'
  287. this.req = {
  288. params: {
  289. project_id: this.projectId,
  290. version: this.version,
  291. pathname: this.pathname,
  292. },
  293. }
  294. this.res = { mock: 'res' }
  295. this.stream = {}
  296. this.SnapshotManager.getFileSnapshotStream.yields(null, this.stream)
  297. this.HttpController.getFileSnapshot(this.req, this.res, this.next)
  298. })
  299. it('should get the snapshot', function () {
  300. this.SnapshotManager.getFileSnapshotStream.should.have.been.calledWith(
  301. this.projectId,
  302. this.version,
  303. this.pathname
  304. )
  305. })
  306. it('should pipe the returned stream into the response', function () {
  307. this.pipeline.should.have.been.calledWith(this.stream, this.res)
  308. })
  309. })
  310. describe('getProjectSnapshot', function () {
  311. beforeEach(function () {
  312. this.version = 42
  313. this.req = {
  314. params: {
  315. project_id: this.projectId,
  316. version: this.version,
  317. },
  318. }
  319. this.res = { json: sinon.stub() }
  320. this.snapshotData = { one: 1 }
  321. this.SnapshotManager.getProjectSnapshot.yields(null, this.snapshotData)
  322. this.HttpController.getProjectSnapshot(this.req, this.res, this.next)
  323. })
  324. it('should get the snapshot', function () {
  325. this.SnapshotManager.getProjectSnapshot.should.have.been.calledWith(
  326. this.projectId,
  327. this.version
  328. )
  329. })
  330. it('should send json response', function () {
  331. this.res.json.calledWith(this.snapshotData).should.equal(true)
  332. })
  333. })
  334. describe('getLabels', function () {
  335. beforeEach(function () {
  336. this.req = {
  337. params: {
  338. project_id: this.projectId,
  339. },
  340. }
  341. this.labels = ['label-1', 'label-2']
  342. this.LabelsManager.getLabels.yields(null, this.labels)
  343. })
  344. describe('project history is enabled', function () {
  345. beforeEach(function () {
  346. this.HistoryApiManager.shouldUseProjectHistory.yields(null, true)
  347. this.HttpController.getLabels(this.req, this.res, this.next)
  348. })
  349. it('should get the labels for a project', function () {
  350. this.LabelsManager.getLabels
  351. .calledWith(this.projectId)
  352. .should.equal(true)
  353. })
  354. it('should return the labels', function () {
  355. this.res.json.calledWith(this.labels).should.equal(true)
  356. })
  357. })
  358. describe('project history is not enabled', function () {
  359. beforeEach(function () {
  360. this.HistoryApiManager.shouldUseProjectHistory.yields(null, false)
  361. this.HttpController.getLabels(this.req, this.res, this.next)
  362. })
  363. it('should return 409', function () {
  364. this.res.sendStatus.calledWith(409).should.equal(true)
  365. })
  366. })
  367. })
  368. describe('createLabel', function () {
  369. beforeEach(function () {
  370. this.req = {
  371. params: {
  372. project_id: this.projectId,
  373. },
  374. body: {
  375. version: (this.version = 1),
  376. comment: (this.comment = 'a comment'),
  377. created_at: (this.created_at = Date.now().toString()),
  378. validate_exists: true,
  379. user_id: this.userId,
  380. },
  381. }
  382. this.label = { _id: new ObjectId() }
  383. this.LabelsManager.createLabel.yields(null, this.label)
  384. })
  385. describe('project history is enabled', function () {
  386. beforeEach(function () {
  387. this.HistoryApiManager.shouldUseProjectHistory.yields(null, true)
  388. this.HttpController.createLabel(this.req, this.res, this.next)
  389. })
  390. it('should create a label for a project', function () {
  391. this.LabelsManager.createLabel.should.have.been.calledWith(
  392. this.projectId,
  393. this.userId,
  394. this.version,
  395. this.comment,
  396. this.created_at,
  397. true
  398. )
  399. })
  400. it('should return the label', function () {
  401. this.res.json.calledWith(this.label).should.equal(true)
  402. })
  403. })
  404. describe('validate_exists = false is passed', function () {
  405. beforeEach(function () {
  406. this.req.body.validate_exists = false
  407. this.HistoryApiManager.shouldUseProjectHistory.yields(null, true)
  408. this.HttpController.createLabel(this.req, this.res, this.next)
  409. })
  410. it('should create a label for a project', function () {
  411. this.LabelsManager.createLabel
  412. .calledWith(
  413. this.projectId,
  414. this.userId,
  415. this.version,
  416. this.comment,
  417. this.created_at,
  418. false
  419. )
  420. .should.equal(true)
  421. })
  422. it('should return the label', function () {
  423. this.res.json.calledWith(this.label).should.equal(true)
  424. })
  425. })
  426. describe('project history is not enabled', function () {
  427. beforeEach(function () {
  428. this.HistoryApiManager.shouldUseProjectHistory.yields(null, false)
  429. this.HttpController.createLabel(this.req, this.res, this.next)
  430. })
  431. it('should return 409', function () {
  432. this.res.sendStatus.calledWith(409).should.equal(true)
  433. })
  434. })
  435. })
  436. describe('deleteLabelForUser', function () {
  437. beforeEach(function () {
  438. this.label_id = new ObjectId()
  439. this.req = {
  440. params: {
  441. project_id: this.projectId,
  442. user_id: this.userId,
  443. label_id: this.label_id.toString(),
  444. },
  445. }
  446. this.HttpController.deleteLabelForUser(this.req, this.res, this.next)
  447. })
  448. it('should delete a label for a project', function () {
  449. this.LabelsManager.deleteLabelForUser
  450. .calledWith(this.projectId, this.userId, this.label_id.toString())
  451. .should.equal(true)
  452. })
  453. it('should return 204', function () {
  454. this.res.sendStatus.calledWith(204).should.equal(true)
  455. })
  456. })
  457. describe('deleteLabel', function () {
  458. beforeEach(function () {
  459. this.label_id = new ObjectId()
  460. this.req = {
  461. params: {
  462. project_id: this.projectId,
  463. label_id: this.label_id.toString(),
  464. },
  465. }
  466. this.HttpController.deleteLabel(this.req, this.res, this.next)
  467. })
  468. it('should delete a label for a project', function () {
  469. this.LabelsManager.deleteLabel
  470. .calledWith(this.projectId, this.label_id.toString())
  471. .should.equal(true)
  472. })
  473. it('should return 204', function () {
  474. this.res.sendStatus.calledWith(204).should.equal(true)
  475. })
  476. })
  477. describe('deleteProject', function () {
  478. beforeEach(function () {
  479. this.req = {
  480. params: {
  481. project_id: this.projectId,
  482. },
  483. }
  484. this.WebApiManager.getHistoryId
  485. .withArgs(this.projectId)
  486. .yields(null, this.historyId)
  487. this.HttpController.deleteProject(this.req, this.res, this.next)
  488. })
  489. it('should delete the updates queue', function () {
  490. this.RedisManager.destroyDocUpdatesQueue.should.have.been.calledWith(
  491. this.projectId
  492. )
  493. })
  494. it('should clear the first op timestamp', function () {
  495. this.RedisManager.clearFirstOpTimestamp.should.have.been.calledWith(
  496. this.projectId
  497. )
  498. })
  499. it('should clear the cached history id', function () {
  500. this.RedisManager.clearCachedHistoryId.should.have.been.calledWith(
  501. this.projectId
  502. )
  503. })
  504. it('should clear the resync state', function () {
  505. this.SyncManager.clearResyncState.should.have.been.calledWith(
  506. this.projectId
  507. )
  508. })
  509. it('should clear any failure record', function () {
  510. this.ErrorRecorder.clearError.should.have.been.calledWith(this.projectId)
  511. })
  512. })
  513. })