overleaf/services/track-changes/test/unit/coffee/DocArchive/MongoAWS.coffee

69 lines
2 KiB
CoffeeScript
Raw Normal View History

2015-08-31 17:13:18 -04:00
chai = require('chai')
chai.should()
sinon = require("sinon")
modulePath = "../../../../app/js/MongoAWS.js"
SandboxedModule = require('sandboxed-module')
{ObjectId} = require("mongojs")
2016-03-04 10:44:38 -05:00
MemoryStream = require('memorystream')
zlib = require "zlib"
2015-08-31 17:13:18 -04:00
describe "MongoAWS", ->
beforeEach ->
@MongoAWS = SandboxedModule.require modulePath, requires:
"settings-sharelatex": @settings =
trackchanges:
2015-08-31 17:13:18 -04:00
s3:
secret: "s3-secret"
key: "s3-key"
stores:
doc_history: "s3-bucket"
2015-08-31 17:13:18 -04:00
"child_process": @child_process = {}
"mongo-uri": @mongouri = {}
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
"aws-sdk": @awssdk = {}
"fs": @fs = {}
2016-03-04 10:44:38 -05:00
"s3-streams": @S3S = {}
2015-09-17 09:41:53 -04:00
"./mongojs" : { db: @db = {}, ObjectId: ObjectId }
2015-08-31 17:13:18 -04:00
"JSONStream": @JSONStream = {}
"readline-stream": @readline = sinon.stub()
@project_id = ObjectId().toString()
@doc_id = ObjectId().toString()
2016-03-04 10:44:38 -05:00
@pack_id = ObjectId()
2015-09-23 10:34:36 -04:00
@update = { v:123 }
2015-08-31 17:13:18 -04:00
@callback = sinon.stub()
2016-03-04 10:44:38 -05:00
describe "archivePack", ->
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
beforeEach (done) ->
@awssdk.config = { update: sinon.stub() }
@awssdk.S3 = sinon.stub()
@S3S.WriteStream = MemoryStream.createWriteStream
@db.docHistory = {}
@db.docHistory.findOne = sinon.stub().callsArgWith(1, null, {"pack":"hello"})
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
@MongoAWS.archivePack @project_id, @doc_id, @pack_id, (err, result) =>
@callback()
done()
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
it "should call the callback", ->
@callback.called.should.equal true
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
describe "unArchivePack", ->
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
beforeEach (done) ->
zlib.gzip '{"pack":"123"}', (err, zbuf) =>
@awssdk.config = { update: sinon.stub() }
@awssdk.S3 = sinon.stub()
@S3S.ReadStream = () ->
MemoryStream.createReadStream(zbuf, {readable:true})
@db.docHistory = {}
@db.docHistory.insert = sinon.stub().callsArgWith(1, null, "pack")
2015-08-31 17:13:18 -04:00
2016-03-04 10:44:38 -05:00
@MongoAWS.unArchivePack @project_id, @doc_id, @pack_id, (err, result) =>
@callback()
done()
2015-09-02 14:39:19 -04:00
2016-03-04 10:44:38 -05:00
it "should call db.docHistory.insert", ->
@db.docHistory.insert.called.should.equal true