GettingDocsFromArchiveTest.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. process.env.BACKEND = 'gcs'
  16. const Settings = require('@overleaf/settings')
  17. const { expect } = require('chai')
  18. const { db, ObjectId } = require('../../../app/js/mongodb')
  19. const async = require('async')
  20. const DocstoreApp = require('./helpers/DocstoreApp')
  21. const DocstoreClient = require('./helpers/DocstoreClient')
  22. const { Storage } = require('@google-cloud/storage')
  23. const Persistor = require('../../../app/js/PersistorManager')
  24. const Streamifier = require('streamifier')
  25. function uploadContent(path, json, callback) {
  26. const stream = Streamifier.createReadStream(JSON.stringify(json))
  27. Persistor.sendStream(Settings.docstore.bucket, path, stream)
  28. .then(() => callback())
  29. .catch(callback)
  30. }
  31. describe('Getting A Doc from Archive', function () {
  32. before(function (done) {
  33. return DocstoreApp.ensureRunning(done)
  34. })
  35. before(async function () {
  36. const storage = new Storage(Settings.docstore.gcs.endpoint)
  37. await storage.createBucket(Settings.docstore.bucket)
  38. await storage.createBucket(`${Settings.docstore.bucket}-deleted`)
  39. })
  40. describe('for an archived doc', function () {
  41. before(function (done) {
  42. this.project_id = ObjectId()
  43. this.timeout(1000 * 30)
  44. this.doc = {
  45. _id: ObjectId(),
  46. lines: ['foo', 'bar'],
  47. ranges: {},
  48. version: 2,
  49. }
  50. DocstoreClient.createDoc(
  51. this.project_id,
  52. this.doc._id,
  53. this.doc.lines,
  54. this.doc.version,
  55. this.doc.ranges,
  56. error => {
  57. if (error) {
  58. return done(error)
  59. }
  60. DocstoreClient.archiveDocById(
  61. this.project_id,
  62. this.doc._id,
  63. (error, res) => {
  64. this.res = res
  65. if (error) {
  66. return done(error)
  67. }
  68. done()
  69. }
  70. )
  71. }
  72. )
  73. })
  74. it('should successully archive the doc', function (done) {
  75. this.res.statusCode.should.equal(204)
  76. done()
  77. })
  78. it('should return the doc lines and version from persistent storage', function (done) {
  79. return DocstoreClient.peekDoc(
  80. this.project_id,
  81. this.doc._id,
  82. {},
  83. (error, res, doc) => {
  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. it('should return the doc lines and version from persistent storage on subsequent requests', function (done) {
  94. return DocstoreClient.peekDoc(
  95. this.project_id,
  96. this.doc._id,
  97. {},
  98. (error, res, doc) => {
  99. res.statusCode.should.equal(200)
  100. res.headers['x-doc-status'].should.equal('archived')
  101. doc.lines.should.deep.equal(this.doc.lines)
  102. doc.version.should.equal(this.doc.version)
  103. doc.ranges.should.deep.equal(this.doc.ranges)
  104. return done()
  105. }
  106. )
  107. })
  108. describe('for an non-archived doc', function () {
  109. before(function (done) {
  110. this.project_id = ObjectId()
  111. this.timeout(1000 * 30)
  112. this.doc = {
  113. _id: ObjectId(),
  114. lines: ['foo', 'bar'],
  115. ranges: {},
  116. version: 2,
  117. }
  118. DocstoreClient.createDoc(
  119. this.project_id,
  120. this.doc._id,
  121. this.doc.lines,
  122. this.doc.version,
  123. this.doc.ranges,
  124. done
  125. )
  126. })
  127. it('should return the doc lines and version from mongo', function (done) {
  128. return DocstoreClient.peekDoc(
  129. this.project_id,
  130. this.doc._id,
  131. {},
  132. (error, res, doc) => {
  133. res.statusCode.should.equal(200)
  134. res.headers['x-doc-status'].should.equal('active')
  135. doc.lines.should.deep.equal(this.doc.lines)
  136. doc.version.should.equal(this.doc.version)
  137. doc.ranges.should.deep.equal(this.doc.ranges)
  138. return done()
  139. }
  140. )
  141. })
  142. })
  143. })