GettingDocsFromArchiveTest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.archiveDocById(
  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. res.statusCode.should.equal(200)
  60. res.headers['x-doc-status'].should.equal('archived')
  61. doc.lines.should.deep.equal(this.doc.lines)
  62. doc.version.should.equal(this.doc.version)
  63. doc.ranges.should.deep.equal(this.doc.ranges)
  64. return done()
  65. }
  66. )
  67. })
  68. it('should return the doc lines and version from persistent storage on subsequent requests', function (done) {
  69. return DocstoreClient.peekDoc(
  70. this.project_id,
  71. this.doc._id,
  72. {},
  73. (error, res, doc) => {
  74. res.statusCode.should.equal(200)
  75. res.headers['x-doc-status'].should.equal('archived')
  76. doc.lines.should.deep.equal(this.doc.lines)
  77. doc.version.should.equal(this.doc.version)
  78. doc.ranges.should.deep.equal(this.doc.ranges)
  79. return done()
  80. }
  81. )
  82. })
  83. describe('for an non-archived doc', function () {
  84. before(function (done) {
  85. this.project_id = ObjectId()
  86. this.timeout(1000 * 30)
  87. this.doc = {
  88. _id: ObjectId(),
  89. lines: ['foo', 'bar'],
  90. ranges: {},
  91. version: 2,
  92. }
  93. DocstoreClient.createDoc(
  94. this.project_id,
  95. this.doc._id,
  96. this.doc.lines,
  97. this.doc.version,
  98. this.doc.ranges,
  99. done
  100. )
  101. })
  102. it('should return the doc lines and version from mongo', function (done) {
  103. return DocstoreClient.peekDoc(
  104. this.project_id,
  105. this.doc._id,
  106. {},
  107. (error, res, doc) => {
  108. res.statusCode.should.equal(200)
  109. res.headers['x-doc-status'].should.equal('active')
  110. doc.lines.should.deep.equal(this.doc.lines)
  111. doc.version.should.equal(this.doc.version)
  112. doc.ranges.should.deep.equal(this.doc.ranges)
  113. return done()
  114. }
  115. )
  116. })
  117. })
  118. })
  119. })