MongoAWS.js 3.3 KB

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