2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/js/MongoAWS.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-09-10 07:58:06 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-17 12:35:16 -05:00
|
|
|
const MemoryStream = require('memorystream')
|
|
|
|
const zlib = require('zlib')
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('MongoAWS', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.MongoAWS = SandboxedModule.require(modulePath, {
|
2020-03-23 06:34:57 -04:00
|
|
|
singleOnly: true,
|
2020-02-17 12:35:16 -05:00
|
|
|
requires: {
|
2021-07-12 12:47:16 -04:00
|
|
|
'@overleaf/settings': (this.settings = {
|
2020-02-17 12:35:16 -05:00
|
|
|
trackchanges: {
|
|
|
|
s3: {
|
|
|
|
secret: 's3-secret',
|
2021-07-13 07:04:43 -04:00
|
|
|
key: 's3-key',
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
stores: {
|
2021-07-13 07:04:43 -04:00
|
|
|
doc_history: 's3-bucket',
|
|
|
|
},
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
}),
|
|
|
|
child_process: (this.child_process = {}),
|
|
|
|
'mongo-uri': (this.mongouri = {}),
|
|
|
|
'aws-sdk': (this.awssdk = {}),
|
|
|
|
fs: (this.fs = {}),
|
|
|
|
's3-streams': (this.S3S = {}),
|
2020-09-10 07:58:06 -04:00
|
|
|
'./mongodb': { db: (this.db = {}), ObjectId },
|
2020-02-17 12:35:16 -05:00
|
|
|
JSONStream: (this.JSONStream = {}),
|
|
|
|
'readline-stream': (this.readline = sinon.stub()),
|
2021-07-13 07:04:43 -04:00
|
|
|
'@overleaf/metrics': { inc() {} },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-02-17 12:35:16 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.doc_id = ObjectId().toString()
|
|
|
|
this.pack_id = ObjectId()
|
|
|
|
this.update = { v: 123 }
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('archivePack', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.awssdk.config = { update: sinon.stub() }
|
|
|
|
this.awssdk.S3 = sinon.stub()
|
|
|
|
this.S3S.WriteStream = () => MemoryStream.createWriteStream()
|
|
|
|
this.db.docHistory = {}
|
|
|
|
this.db.docHistory.findOne = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { pack: 'hello' })
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoAWS.archivePack(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.pack_id,
|
|
|
|
(err, result) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-02-17 12:35:16 -05:00
|
|
|
this.callback()
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('unArchivePack', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 12:35:16 -05:00
|
|
|
return zlib.gzip('{"pack":"123"}', (err, zbuf) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-02-17 12:35:16 -05:00
|
|
|
this.awssdk.config = { update: sinon.stub() }
|
|
|
|
this.awssdk.S3 = sinon.stub()
|
|
|
|
this.S3S.ReadStream = () =>
|
|
|
|
MemoryStream.createReadStream(zbuf, { readable: true })
|
|
|
|
this.db.docHistory = {}
|
2020-09-10 07:58:06 -04:00
|
|
|
this.db.docHistory.insertOne = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, { insertedId: ObjectId() })
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.MongoAWS.unArchivePack(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.pack_id,
|
|
|
|
(err, result) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return done(err)
|
2020-02-17 12:35:16 -05:00
|
|
|
this.callback()
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2015-08-31 17:13:18 -04:00
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should call db.docHistory.insert', function () {
|
2020-09-10 07:58:06 -04:00
|
|
|
return this.db.docHistory.insertOne.called.should.equal(true)
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|