overleaf/services/docstore/test/unit/coffee/DocArchiveManager.coffee

180 lines
5.8 KiB
CoffeeScript
Raw Normal View History

2015-06-02 16:12:11 -04:00
assert = require("chai").assert
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../app/js/DocArchiveManager.js"
SandboxedModule = require('sandboxed-module')
ObjectId = require("mongojs").ObjectId
Errors = require "../../../app/js/Errors"
2015-06-03 11:00:08 -04:00
crypto = require("crypto")
2015-06-02 16:12:11 -04:00
describe "DocArchiveManager", ->
beforeEach ->
@settings =
docstore:
2015-06-02 16:12:11 -04:00
s3:
secret: "secret"
key: "this_key"
bucket:"doc-archive-unit-test"
2015-06-02 16:12:11 -04:00
@request =
2015-06-02 17:13:16 -04:00
put: {}
get: {}
2015-06-03 19:05:15 -04:00
del: {}
2015-06-02 16:12:11 -04:00
@archivedDocs = [{
_id: ObjectId()
inS3:true
rev: 2
}, {
_id: ObjectId()
inS3:true
rev: 4
}, {
_id: ObjectId()
inS3:true
rev: 6
}]
@mongoDocs = [{
2015-06-02 16:12:11 -04:00
_id: ObjectId()
lines: ["one", "two", "three"]
rev: 2
}, {
_id: ObjectId()
lines: ["aaa", "bbb", "ccc"]
rev: 4
}, {
_id: ObjectId()
inS3: true
rev: 6
}, {
_id: ObjectId()
2015-06-02 17:13:16 -04:00
inS3: true
rev: 6
2015-06-02 16:12:11 -04:00
}, {
_id: ObjectId()
lines: ["111", "222", "333"]
rev: 6
}]
2015-06-02 17:13:16 -04:00
@MongoManager =
markDocAsArchived: sinon.stub().callsArgWith(2, null)
upsertIntoDocCollection: sinon.stub().callsArgWith(3, null)
getProjectsDocs: sinon.stub().callsArgWith(1, null, @mongoDocs)
getArchivedProjectDocs: sinon.stub().callsArgWith(1, null, @mongoDocs)
2015-06-02 17:13:16 -04:00
@requires =
"settings-sharelatex": @settings
"./MongoManager": @MongoManager
"request": @request
"logger-sharelatex":
log:->
err:->
@error = "my errror"
2015-06-02 16:12:11 -04:00
@project_id = ObjectId().toString()
2015-06-03 11:00:08 -04:00
@stubbedError = new Errors.NotFoundError("Error in S3 request")
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
2015-06-02 16:12:11 -04:00
describe "archiveDoc", ->
it "should use correct options", (done)->
2015-06-03 11:00:08 -04:00
@request.put = sinon.stub().callsArgWith(1, null, {statusCode:200,headers:{etag:""}})
@DocArchiveManager.archiveDoc @project_id, @mongoDocs[0], (err)=>
2015-06-02 16:12:11 -04:00
opts = @request.put.args[0][0]
assert.deepEqual(opts.aws, {key:@settings.docstore.s3.key, secret:@settings.docstore.s3.secret, bucket:@settings.docstore.s3.bucket})
opts.json.should.equal @mongoDocs[0].lines
2015-06-02 16:12:11 -04:00
opts.timeout.should.equal (30*1000)
opts.uri.should.equal "https://#{@settings.docstore.s3.bucket}.s3.amazonaws.com/#{@project_id}/#{@mongoDocs[0]._id}"
2015-06-02 17:13:16 -04:00
done()
2015-06-03 11:00:08 -04:00
it "should return no md5 error", (done)->
@md5 = crypto.createHash("md5").update(JSON.stringify(@mongoDocs[0].lines)).digest("hex")
2015-06-03 11:00:08 -04:00
@request.put = sinon.stub().callsArgWith(1, null, {statusCode:200,headers:{etag:@md5}})
@DocArchiveManager.archiveDoc @project_id, @mongoDocs[0], (err)=>
2015-06-03 11:00:08 -04:00
should.not.exist err
done()
2015-06-02 17:13:16 -04:00
it "should return the error", (done)->
2015-06-03 11:00:08 -04:00
@request.put = sinon.stub().callsArgWith(1, @stubbedError, {statusCode:400,headers:{etag:""}})
@DocArchiveManager.archiveDoc @project_id, @mongoDocs[0], (err)=>
2015-06-03 11:00:08 -04:00
should.exist err
2015-06-02 17:13:16 -04:00
done()
describe "unarchiveDoc", ->
it "should use correct options", (done)->
@request.get = sinon.stub().callsArgWith(1, null, statusCode:200, @mongoDocs[0].lines)
2015-06-03 19:05:15 -04:00
@request.del = sinon.stub().callsArgWith(1, null, statusCode:204, {})
@DocArchiveManager.unarchiveDoc @project_id, @mongoDocs[0]._id, (err)=>
2015-06-02 17:13:16 -04:00
opts = @request.get.args[0][0]
assert.deepEqual(opts.aws, {key:@settings.docstore.s3.key, secret:@settings.docstore.s3.secret, bucket:@settings.docstore.s3.bucket})
2015-06-02 17:13:16 -04:00
opts.json.should.equal true
opts.timeout.should.equal (30*1000)
opts.uri.should.equal "https://#{@settings.docstore.s3.bucket}.s3.amazonaws.com/#{@project_id}/#{@mongoDocs[0]._id}"
2015-06-02 17:13:16 -04:00
done()
it "should return the error", (done)->
2015-06-03 11:00:08 -04:00
@request.get = sinon.stub().callsArgWith(1, @stubbedError, {}, {})
@DocArchiveManager.unarchiveDoc @project_id, @mongoDocs[0], (err)=>
2015-06-03 11:00:08 -04:00
should.exist err
2015-06-02 17:13:16 -04:00
done()
describe "archiveAllDocs", ->
it "should archive all project docs which are not in s3", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
2015-06-02 17:13:16 -04:00
@DocArchiveManager.archiveDoc = sinon.stub().callsArgWith(2, null)
@DocArchiveManager.archiveAllDocs @project_id, (err)=>
@DocArchiveManager.archiveDoc.calledWith(@project_id, @mongoDocs[0]).should.equal true
@DocArchiveManager.archiveDoc.calledWith(@project_id, @mongoDocs[1]).should.equal true
@DocArchiveManager.archiveDoc.calledWith(@project_id, @mongoDocs[4]).should.equal true
@DocArchiveManager.archiveDoc.calledWith(@project_id, @mongoDocs[2]).should.equal false
@DocArchiveManager.archiveDoc.calledWith(@project_id, @mongoDocs[3]).should.equal false
2015-06-02 17:13:16 -04:00
should.not.exist err
done()
it "should return error if have no docs", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, null)
@DocArchiveManager.archiveAllDocs @project_id, (err)=>
should.exist err
done()
it "should return the error", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, @error, null)
@DocArchiveManager.archiveAllDocs @project_id, (err)=>
err.should.equal @error
done()
describe "unArchiveAllDocs", ->
it "should unarchive all inS3 docs", (done)->
@MongoManager.getArchivedProjectDocs = sinon.stub().callsArgWith(1, null, @archivedDocs)
2015-06-02 17:13:16 -04:00
@DocArchiveManager.unarchiveDoc = sinon.stub().callsArgWith(2, null)
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
for doc in @archivedDocs
2015-06-02 17:13:16 -04:00
@DocArchiveManager.unarchiveDoc.calledWith(@project_id, doc._id).should.equal true
should.not.exist err
done()
it "should return error if have no docs", (done)->
@MongoManager.getArchivedProjectDocs = sinon.stub().callsArgWith(1, null, null)
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
should.exist err
done()
it "should return the error", (done)->
@MongoManager.getArchivedProjectDocs = sinon.stub().callsArgWith(1, @error, null)
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
err.should.equal @error
done()