MongoAWS.js 3.1 KB

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