GettingDocsFromArchiveTest.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const Settings = require('@overleaf/settings')
  2. const { ObjectId } = require('../../../app/js/mongodb')
  3. const DocstoreApp = require('./helpers/DocstoreApp')
  4. const DocstoreClient = require('./helpers/DocstoreClient')
  5. const { Storage } = require('@google-cloud/storage')
  6. describe('Getting A Doc from Archive', function () {
  7. before(function (done) {
  8. return DocstoreApp.ensureRunning(done)
  9. })
  10. before(async function () {
  11. const storage = new Storage(Settings.docstore.gcs.endpoint)
  12. await storage.createBucket(Settings.docstore.bucket)
  13. await storage.createBucket(`${Settings.docstore.bucket}-deleted`)
  14. })
  15. describe('for an archived doc', function () {
  16. before(function (done) {
  17. this.project_id = ObjectId()
  18. this.timeout(1000 * 30)
  19. this.doc = {
  20. _id: ObjectId(),
  21. lines: ['foo', 'bar'],
  22. ranges: {},
  23. version: 2,
  24. }
  25. DocstoreClient.createDoc(
  26. this.project_id,
  27. this.doc._id,
  28. this.doc.lines,
  29. this.doc.version,
  30. this.doc.ranges,
  31. error => {
  32. if (error) {
  33. return done(error)
  34. }
  35. DocstoreClient.archiveDoc(
  36. this.project_id,
  37. this.doc._id,
  38. (error, res) => {
  39. this.res = res
  40. if (error) {
  41. return done(error)
  42. }
  43. done()
  44. }
  45. )
  46. }
  47. )
  48. })
  49. it('should successully archive the doc', function (done) {
  50. this.res.statusCode.should.equal(204)
  51. done()
  52. })
  53. it('should return the doc lines and version from persistent storage', function (done) {
  54. return DocstoreClient.peekDoc(
  55. this.project_id,
  56. this.doc._id,
  57. {},
  58. (error, res, doc) => {
  59. if (error) return done(error)
  60. res.statusCode.should.equal(200)
  61. res.headers['x-doc-status'].should.equal('archived')
  62. doc.lines.should.deep.equal(this.doc.lines)
  63. doc.version.should.equal(this.doc.version)
  64. doc.ranges.should.deep.equal(this.doc.ranges)
  65. return done()
  66. }
  67. )
  68. })
  69. it('should return the doc lines and version from persistent storage on subsequent requests', function (done) {
  70. return DocstoreClient.peekDoc(
  71. this.project_id,
  72. this.doc._id,
  73. {},
  74. (error, res, doc) => {
  75. if (error) return done(error)
  76. res.statusCode.should.equal(200)
  77. res.headers['x-doc-status'].should.equal('archived')
  78. doc.lines.should.deep.equal(this.doc.lines)
  79. doc.version.should.equal(this.doc.version)
  80. doc.ranges.should.deep.equal(this.doc.ranges)
  81. return done()
  82. }
  83. )
  84. })
  85. describe('for an non-archived doc', function () {
  86. before(function (done) {
  87. this.project_id = ObjectId()
  88. this.timeout(1000 * 30)
  89. this.doc = {
  90. _id: ObjectId(),
  91. lines: ['foo', 'bar'],
  92. ranges: {},
  93. version: 2,
  94. }
  95. DocstoreClient.createDoc(
  96. this.project_id,
  97. this.doc._id,
  98. this.doc.lines,
  99. this.doc.version,
  100. this.doc.ranges,
  101. done
  102. )
  103. })
  104. it('should return the doc lines and version from mongo', function (done) {
  105. return DocstoreClient.peekDoc(
  106. this.project_id,
  107. this.doc._id,
  108. {},
  109. (error, res, doc) => {
  110. if (error) return done(error)
  111. res.statusCode.should.equal(200)
  112. res.headers['x-doc-status'].should.equal('active')
  113. doc.lines.should.deep.equal(this.doc.lines)
  114. doc.version.should.equal(this.doc.version)
  115. doc.ranges.should.deep.equal(this.doc.ranges)
  116. return done()
  117. }
  118. )
  119. })
  120. })
  121. })
  122. })