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")
|
|
|
|
|
|
|
|
describe "MongoAWS", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoAWS = SandboxedModule.require modulePath, requires:
|
|
|
|
"settings-sharelatex": @settings =
|
|
|
|
filestore:
|
|
|
|
s3:
|
|
|
|
secret: "s3-secret"
|
|
|
|
key: "s3-key"
|
|
|
|
stores:
|
|
|
|
user_files: "s3-bucket"
|
|
|
|
"child_process": @child_process = {}
|
|
|
|
"mongo-uri": @mongouri = {}
|
|
|
|
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
|
|
|
|
"aws-sdk": @awssdk = {}
|
|
|
|
"fs": @fs = {}
|
|
|
|
"s3-streams": @s3streams = {}
|
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()
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
|
|
|
describe "archiveDocHistory", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@awssdk.config = { update: sinon.stub() }
|
|
|
|
@awssdk.S3 = sinon.stub()
|
|
|
|
@s3streams.WriteStream = sinon.stub()
|
|
|
|
@db.docHistory = {}
|
2015-09-16 17:39:07 -04:00
|
|
|
@db.docHistory.on = sinon.stub()
|
2015-08-31 17:13:18 -04:00
|
|
|
@db.docHistory.find = sinon.stub().returns @db.docHistory
|
2015-09-16 17:39:07 -04:00
|
|
|
@db.docHistory.on.returns
|
2015-08-31 17:13:18 -04:00
|
|
|
pipe:->
|
2015-09-16 17:39:07 -04:00
|
|
|
pipe:->
|
|
|
|
on: (type, cb)->
|
|
|
|
on: (type, cb)->
|
|
|
|
cb()
|
2015-08-31 17:13:18 -04:00
|
|
|
@JSONStream.stringify = sinon.stub()
|
|
|
|
|
|
|
|
@MongoAWS.archiveDocHistory @project_id, @doc_id, @callback
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
2015-09-16 17:39:07 -04:00
|
|
|
@callback.called.should.equal true
|
2015-08-31 17:13:18 -04:00
|
|
|
|
|
|
|
describe "unArchiveDocHistory", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@awssdk.config = { update: sinon.stub() }
|
|
|
|
@awssdk.S3 = sinon.stub()
|
|
|
|
@s3streams.ReadStream = sinon.stub()
|
|
|
|
|
|
|
|
@s3streams.ReadStream.returns
|
|
|
|
#describe on 'open' behavior
|
|
|
|
on: (type, cb)->
|
2015-09-16 17:39:07 -04:00
|
|
|
#describe on 'error' behavior
|
|
|
|
on: (type, cb)->
|
|
|
|
pipe:->
|
|
|
|
#describe on 'data' behavior
|
|
|
|
on: (type, cb)->
|
|
|
|
cb([])
|
|
|
|
#describe on 'end' behavior
|
|
|
|
on: (type, cb)->
|
|
|
|
cb()
|
|
|
|
#describe on 'error' behavior
|
|
|
|
on: sinon.stub()
|
2015-08-31 17:13:18 -04:00
|
|
|
|
|
|
|
@MongoAWS.handleBulk = sinon.stub()
|
|
|
|
@MongoAWS.unArchiveDocHistory @project_id, @doc_id, @callback
|
|
|
|
|
|
|
|
it "should call handleBulk", ->
|
2015-09-16 17:39:07 -04:00
|
|
|
@MongoAWS.handleBulk.called.should.equal true
|
2015-09-02 14:39:19 -04:00
|
|
|
|
|
|
|
describe "handleBulk", ->
|
|
|
|
beforeEach ->
|
2015-09-02 16:00:32 -04:00
|
|
|
@bulkOps = [{
|
2015-09-02 14:39:19 -04:00
|
|
|
_id: ObjectId()
|
|
|
|
doc_id: ObjectId()
|
|
|
|
project_id: ObjectId()
|
|
|
|
}, {
|
|
|
|
_id: ObjectId()
|
|
|
|
doc_id: ObjectId()
|
|
|
|
project_id: ObjectId()
|
|
|
|
}, {
|
|
|
|
_id: ObjectId()
|
|
|
|
doc_id: ObjectId()
|
|
|
|
project_id: ObjectId()
|
|
|
|
}]
|
|
|
|
@bulk =
|
|
|
|
find: sinon.stub().returns
|
|
|
|
upsert: sinon.stub().returns
|
|
|
|
updateOne: sinon.stub()
|
|
|
|
execute: sinon.stub().callsArgWith(0, null, {})
|
|
|
|
@db.docHistory = {}
|
|
|
|
@db.docHistory.initializeUnorderedBulkOp = sinon.stub().returns @bulk
|
2015-09-17 09:41:53 -04:00
|
|
|
@MongoAWS.handleBulk @bulkOps, @bulkOps.length, @callback
|
2015-09-02 14:39:19 -04:00
|
|
|
|
|
|
|
it "should call updateOne for each operation", ->
|
2015-09-02 16:00:32 -04:00
|
|
|
@bulk.find.calledWith({_id:@bulkOps[0]._id}).should.equal true
|
|
|
|
@bulk.find.calledWith({_id:@bulkOps[1]._id}).should.equal true
|
|
|
|
@bulk.find.calledWith({_id:@bulkOps[2]._id}).should.equal true
|
2015-09-02 14:39:19 -04:00
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.calledWith(null).should.equal true
|
|
|
|
|