MongoAWS.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const sinon = require('sinon')
  12. const modulePath = '../../../../app/js/MongoAWS.js'
  13. const SandboxedModule = require('sandboxed-module')
  14. const { ObjectId } = require('mongodb')
  15. const MemoryStream = require('memorystream')
  16. const zlib = require('zlib')
  17. describe('MongoAWS', function () {
  18. beforeEach(function () {
  19. this.MongoAWS = SandboxedModule.require(modulePath, {
  20. singleOnly: true,
  21. requires: {
  22. '@overleaf/settings': (this.settings = {
  23. trackchanges: {
  24. s3: {
  25. secret: 's3-secret',
  26. key: 's3-key',
  27. },
  28. stores: {
  29. doc_history: 's3-bucket',
  30. },
  31. },
  32. }),
  33. child_process: (this.child_process = {}),
  34. 'mongo-uri': (this.mongouri = {}),
  35. 'aws-sdk': (this.awssdk = {}),
  36. fs: (this.fs = {}),
  37. 's3-streams': (this.S3S = {}),
  38. './mongodb': { db: (this.db = {}), ObjectId },
  39. JSONStream: (this.JSONStream = {}),
  40. 'readline-stream': (this.readline = sinon.stub()),
  41. '@overleaf/metrics': { inc() {} },
  42. },
  43. })
  44. this.project_id = ObjectId().toString()
  45. this.doc_id = ObjectId().toString()
  46. this.pack_id = ObjectId()
  47. this.update = { v: 123 }
  48. return (this.callback = sinon.stub())
  49. })
  50. describe('archivePack', function () {
  51. beforeEach(function (done) {
  52. this.awssdk.config = { update: sinon.stub() }
  53. this.awssdk.S3 = sinon.stub()
  54. this.S3S.WriteStream = () => MemoryStream.createWriteStream()
  55. this.db.docHistory = {}
  56. this.db.docHistory.findOne = sinon
  57. .stub()
  58. .callsArgWith(1, null, { pack: 'hello' })
  59. return this.MongoAWS.archivePack(
  60. this.project_id,
  61. this.doc_id,
  62. this.pack_id,
  63. (err, result) => {
  64. if (err) return done(err)
  65. this.callback()
  66. return done()
  67. }
  68. )
  69. })
  70. return it('should call the callback', function () {
  71. return this.callback.called.should.equal(true)
  72. })
  73. })
  74. return describe('unArchivePack', function () {
  75. beforeEach(function (done) {
  76. return zlib.gzip('{"pack":"123"}', (err, zbuf) => {
  77. if (err) return done(err)
  78. this.awssdk.config = { update: sinon.stub() }
  79. this.awssdk.S3 = sinon.stub()
  80. this.S3S.ReadStream = () =>
  81. MemoryStream.createReadStream(zbuf, { readable: true })
  82. this.db.docHistory = {}
  83. this.db.docHistory.insertOne = sinon
  84. .stub()
  85. .yields(null, { insertedId: ObjectId() })
  86. return this.MongoAWS.unArchivePack(
  87. this.project_id,
  88. this.doc_id,
  89. this.pack_id,
  90. (err, result) => {
  91. if (err) return done(err)
  92. this.callback()
  93. return done()
  94. }
  95. )
  96. })
  97. })
  98. return it('should call db.docHistory.insert', function () {
  99. return this.db.docHistory.insertOne.called.should.equal(true)
  100. })
  101. })
  102. })