GettingDocsFromArchiveTest.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. after(async function () {
  16. // Tear down the buckets created above
  17. const storage = new Storage(Settings.docstore.gcs.endpoint)
  18. await storage.bucket(Settings.docstore.bucket).deleteFiles()
  19. await storage.bucket(Settings.docstore.bucket).delete()
  20. await storage.bucket(`${Settings.docstore.bucket}-deleted`).deleteFiles()
  21. await storage.bucket(`${Settings.docstore.bucket}-deleted`).delete()
  22. })
  23. describe('for an archived doc', function () {
  24. before(function (done) {
  25. this.project_id = new ObjectId()
  26. this.timeout(1000 * 30)
  27. this.doc = {
  28. _id: new ObjectId(),
  29. lines: ['foo', 'bar'],
  30. ranges: {},
  31. version: 2,
  32. }
  33. DocstoreClient.createDoc(
  34. this.project_id,
  35. this.doc._id,
  36. this.doc.lines,
  37. this.doc.version,
  38. this.doc.ranges,
  39. error => {
  40. if (error) {
  41. return done(error)
  42. }
  43. DocstoreClient.archiveDoc(
  44. this.project_id,
  45. this.doc._id,
  46. (error, res) => {
  47. this.res = res
  48. if (error) {
  49. return done(error)
  50. }
  51. done()
  52. }
  53. )
  54. }
  55. )
  56. })
  57. it('should successully archive the doc', function (done) {
  58. this.res.statusCode.should.equal(204)
  59. done()
  60. })
  61. it('should return the doc lines and version from persistent storage', function (done) {
  62. return DocstoreClient.peekDoc(
  63. this.project_id,
  64. this.doc._id,
  65. {},
  66. (error, res, doc) => {
  67. if (error) return done(error)
  68. res.statusCode.should.equal(200)
  69. res.headers['x-doc-status'].should.equal('archived')
  70. doc.lines.should.deep.equal(this.doc.lines)
  71. doc.version.should.equal(this.doc.version)
  72. doc.ranges.should.deep.equal(this.doc.ranges)
  73. return done()
  74. }
  75. )
  76. })
  77. it('should return the doc lines and version from persistent storage on subsequent requests', function (done) {
  78. return DocstoreClient.peekDoc(
  79. this.project_id,
  80. this.doc._id,
  81. {},
  82. (error, res, doc) => {
  83. if (error) return done(error)
  84. res.statusCode.should.equal(200)
  85. res.headers['x-doc-status'].should.equal('archived')
  86. doc.lines.should.deep.equal(this.doc.lines)
  87. doc.version.should.equal(this.doc.version)
  88. doc.ranges.should.deep.equal(this.doc.ranges)
  89. return done()
  90. }
  91. )
  92. })
  93. describe('for an non-archived doc', function () {
  94. before(function (done) {
  95. this.project_id = new ObjectId()
  96. this.timeout(1000 * 30)
  97. this.doc = {
  98. _id: new ObjectId(),
  99. lines: ['foo', 'bar'],
  100. ranges: {},
  101. version: 2,
  102. }
  103. DocstoreClient.createDoc(
  104. this.project_id,
  105. this.doc._id,
  106. this.doc.lines,
  107. this.doc.version,
  108. this.doc.ranges,
  109. done
  110. )
  111. })
  112. it('should return the doc lines and version from mongo', function (done) {
  113. return DocstoreClient.peekDoc(
  114. this.project_id,
  115. this.doc._id,
  116. {},
  117. (error, res, doc) => {
  118. if (error) return done(error)
  119. res.statusCode.should.equal(200)
  120. res.headers['x-doc-status'].should.equal('active')
  121. doc.lines.should.deep.equal(this.doc.lines)
  122. doc.version.should.equal(this.doc.version)
  123. doc.ranges.should.deep.equal(this.doc.ranges)
  124. return done()
  125. }
  126. )
  127. })
  128. })
  129. })
  130. })