MongoAWS.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 chai = require('chai')
  13. chai.should()
  14. const sinon = require('sinon')
  15. const modulePath = '../../../../app/js/MongoAWS.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. const { ObjectId } = require('mongodb')
  18. const MemoryStream = require('memorystream')
  19. const zlib = require('zlib')
  20. describe('MongoAWS', function () {
  21. beforeEach(function () {
  22. this.MongoAWS = SandboxedModule.require(modulePath, {
  23. singleOnly: true,
  24. requires: {
  25. 'settings-sharelatex': (this.settings = {
  26. trackchanges: {
  27. s3: {
  28. secret: 's3-secret',
  29. key: 's3-key'
  30. },
  31. stores: {
  32. doc_history: 's3-bucket'
  33. }
  34. }
  35. }),
  36. child_process: (this.child_process = {}),
  37. 'mongo-uri': (this.mongouri = {}),
  38. 'logger-sharelatex': (this.logger = {
  39. log: sinon.stub(),
  40. error: sinon.stub(),
  41. err() {}
  42. }),
  43. 'aws-sdk': (this.awssdk = {}),
  44. fs: (this.fs = {}),
  45. 's3-streams': (this.S3S = {}),
  46. './mongodb': { db: (this.db = {}), ObjectId },
  47. JSONStream: (this.JSONStream = {}),
  48. 'readline-stream': (this.readline = sinon.stub()),
  49. 'metrics-sharelatex': { inc() {} }
  50. }
  51. })
  52. this.project_id = ObjectId().toString()
  53. this.doc_id = ObjectId().toString()
  54. this.pack_id = ObjectId()
  55. this.update = { v: 123 }
  56. return (this.callback = sinon.stub())
  57. })
  58. describe('archivePack', function () {
  59. beforeEach(function (done) {
  60. this.awssdk.config = { update: sinon.stub() }
  61. this.awssdk.S3 = sinon.stub()
  62. this.S3S.WriteStream = () => MemoryStream.createWriteStream()
  63. this.db.docHistory = {}
  64. this.db.docHistory.findOne = sinon
  65. .stub()
  66. .callsArgWith(1, null, { pack: 'hello' })
  67. return this.MongoAWS.archivePack(
  68. this.project_id,
  69. this.doc_id,
  70. this.pack_id,
  71. (err, result) => {
  72. this.callback()
  73. return done()
  74. }
  75. )
  76. })
  77. return it('should call the callback', function () {
  78. return this.callback.called.should.equal(true)
  79. })
  80. })
  81. return describe('unArchivePack', function () {
  82. beforeEach(function (done) {
  83. return zlib.gzip('{"pack":"123"}', (err, zbuf) => {
  84. this.awssdk.config = { update: sinon.stub() }
  85. this.awssdk.S3 = sinon.stub()
  86. this.S3S.ReadStream = () =>
  87. MemoryStream.createReadStream(zbuf, { readable: true })
  88. this.db.docHistory = {}
  89. this.db.docHistory.insertOne = sinon
  90. .stub()
  91. .yields(null, { insertedId: ObjectId() })
  92. return this.MongoAWS.unArchivePack(
  93. this.project_id,
  94. this.doc_id,
  95. this.pack_id,
  96. (err, result) => {
  97. this.callback()
  98. return done()
  99. }
  100. )
  101. })
  102. })
  103. return it('should call db.docHistory.insert', function () {
  104. return this.db.docHistory.insertOne.called.should.equal(true)
  105. })
  106. })
  107. })