GettingProjectDocsTests.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const { expect } = require('chai')
  2. const MockWebApi = require('./helpers/MockWebApi')
  3. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  4. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  5. const { RequestFailedError } = require('@overleaf/fetch-utils')
  6. describe('Getting documents for project', function () {
  7. before(async function () {
  8. this.lines = ['one', 'two', 'three']
  9. this.version = 42
  10. await DocUpdaterApp.ensureRunning()
  11. })
  12. describe('when project state hash does not match', function () {
  13. it('should return a 409 Conflict response', async function () {
  14. const projectStateHash = DocUpdaterClient.randomId()
  15. const projectId = DocUpdaterClient.randomId()
  16. const docId = DocUpdaterClient.randomId()
  17. MockWebApi.insertDoc(projectId, docId, {
  18. lines: this.lines,
  19. version: this.version,
  20. })
  21. await DocUpdaterClient.preloadDoc(projectId, docId)
  22. await expect(DocUpdaterClient.getProjectDocs(projectId, projectStateHash))
  23. .to.be.rejectedWith(RequestFailedError)
  24. .and.eventually.have.nested.property('response.status', 409)
  25. })
  26. })
  27. describe('when project state hash matches', function () {
  28. it('should return the documents', async function () {
  29. const projectStateHash = DocUpdaterClient.randomId()
  30. const projectId = DocUpdaterClient.randomId()
  31. const docId = DocUpdaterClient.randomId()
  32. MockWebApi.insertDoc(projectId, docId, {
  33. lines: this.lines,
  34. version: this.version,
  35. })
  36. await DocUpdaterClient.preloadDoc(projectId, docId)
  37. // set the hash
  38. await expect(DocUpdaterClient.getProjectDocs(projectId, projectStateHash))
  39. .to.be.rejectedWith(RequestFailedError)
  40. .and.eventually.have.nested.property('response.status', 409)
  41. const returnedDocs1 = await DocUpdaterClient.getProjectDocs(
  42. projectId,
  43. projectStateHash
  44. )
  45. // the hash should now match
  46. returnedDocs1.should.deep.equal([
  47. { _id: docId, lines: this.lines, v: this.version },
  48. ])
  49. })
  50. })
  51. describe('when the doc has been removed', function () {
  52. it('should return a 409 Conflict response', async function () {
  53. const projectStateHash = DocUpdaterClient.randomId()
  54. const projectId = DocUpdaterClient.randomId()
  55. const docId = DocUpdaterClient.randomId()
  56. MockWebApi.insertDoc(projectId, docId, {
  57. lines: this.lines,
  58. version: this.version,
  59. })
  60. await DocUpdaterClient.preloadDoc(projectId, docId)
  61. await expect(DocUpdaterClient.getProjectDocs(projectId, projectStateHash))
  62. .to.be.rejectedWith(RequestFailedError)
  63. .and.eventually.have.nested.property('response.status', 409)
  64. await DocUpdaterClient.deleteDoc(projectId, docId)
  65. // the hash would match, but the doc has been deleted
  66. await expect(DocUpdaterClient.getProjectDocs(projectId, projectStateHash))
  67. .to.be.rejectedWith(RequestFailedError)
  68. .and.eventually.have.nested.property('response.status', 409)
  69. })
  70. })
  71. })