more unit tests for archive feature

This commit is contained in:
Henrique Dias 2015-06-02 18:13:16 -03:00
parent 6add9a0da7
commit 0cd2120430
3 changed files with 133 additions and 27 deletions

View file

@ -12,7 +12,7 @@ module.exports = DocArchive =
archiveAllDocs: (project_id, callback = (error, docs) ->) ->
MongoManager.getProjectsDocs project_id, (error, docs) ->
if err?
if error?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
@ -27,14 +27,14 @@ module.exports = DocArchive =
request.put options, (err, res)->
if err? || res.statusCode != 200
logger.err err:err, res:res, "something went wrong archiving doc in aws"
callback(err)
return callback(err)
MongoManager.markDocAsArchived doc._id, doc.rev, (error) ->
return callback(error) if error?
callback()
unArchiveAllDocs: (project_id, callback = (error) ->) ->
MongoManager.getArchivedProjectDocs project_id, (error, docs) ->
if err?
if error?
return callback(error)
else if !docs?
return callback new Errors.NotFoundError("No docs for project #{project_id}")
@ -52,7 +52,7 @@ module.exports = DocArchive =
request.get options, (err, res, lines)->
if err? || res.statusCode != 200
logger.err err:err, res:res, "something went wrong unarchiving doc from aws"
callback(err)
return callback(err)
MongoManager.upsertIntoDocCollection project_id, doc_id.toString(), lines, (error) ->
return callback(error) if error?
callback()

View file

@ -22,13 +22,33 @@ describe "DocArchiveManager", ->
user_files:"sl_user_files"
@request =
put: sinon.stub().callsArgWith(1, null, statusCode:200)
put: {}
get: {}
@docs = [{
_id: ObjectId()
lines: ["one", "two", "three"]
rev: 2
inS3: true
}, {
_id: ObjectId()
lines: ["aaa", "bbb", "ccc"]
rev: 4
inS3: true
}, {
_id: ObjectId()
lines: ["111", "222", "333"]
rev: 6
inS3: true
}]
@MongoManager =
markDocAsArchived: sinon.stub().callsArgWith(2, null)
upsertIntoDocCollection: sinon.stub()
upsertIntoDocCollection: sinon.stub().callsArgWith(3, null)
getProjectsDocs: sinon.stub().callsArgWith(1, null, @docs)
getArchivedProjectDocs: sinon.stub().callsArgWith(1, null, @docs)
@DocArchiveManager = SandboxedModule.require modulePath, requires:
@requires =
"settings-sharelatex": @settings
"./MongoManager": @MongoManager
"request": @request
@ -36,35 +56,118 @@ describe "DocArchiveManager", ->
log:->
err:->
@key = "my/key"
@bucketName = "my-bucket"
@error = "my errror"
@docs = [{
_id: ObjectId()
lines: ["one", "two", "three"]
rev: 2
}, {
_id: ObjectId()
lines: ["aaa", "bbb", "ccc"]
rev: 4
}, {
_id: ObjectId()
lines: ["111", "222", "333"]
rev: 6
}]
@project_id = ObjectId().toString()
@callback = sinon.stub()
@stubbedError = new Error("blew up")
@stubbedError = new Errors.NotFoundError("blew up")
describe "archiveDoc", ->
it "should use correct options", (done)->
@request.put = sinon.stub().callsArgWith(1, null, statusCode:200)
@requires["request"] = @request
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.archiveDoc @project_id, @docs[0], (err)=>
opts = @request.put.args[0][0]
assert.deepEqual(opts.aws, {key:@settings.filestore.s3.key, secret:@settings.filestore.s3.secret, bucket:@settings.filestore.stores.user_files})
opts.json.should.equal @docs[0].lines
opts.timeout.should.equal (30*1000)
opts.uri.should.equal "https://#{@settings.filestore.stores.user_files}.s3.amazonaws.com/#{@project_id}/#{@docs[0]._id}"
done()
done()
it "should return the error", (done)->
@request.put = sinon.stub().callsArgWith(1, @error, {})
@requires["request"] = @request
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.archiveDoc @project_id, @docs[0], (err)=>
err.should.equal @error
done()
describe "unarchiveDoc", ->
it "should use correct options", (done)->
@request.get = sinon.stub().callsArgWith(1, null, statusCode:200, @docs[0].lines)
@requires["request"] = @request
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.unarchiveDoc @project_id, @docs[0]._id, (err)=>
opts = @request.get.args[0][0]
assert.deepEqual(opts.aws, {key:@settings.filestore.s3.key, secret:@settings.filestore.s3.secret, bucket:@settings.filestore.stores.user_files})
opts.json.should.equal true
opts.timeout.should.equal (30*1000)
opts.uri.should.equal "https://#{@settings.filestore.stores.user_files}.s3.amazonaws.com/#{@project_id}/#{@docs[0]._id}"
done()
it "should return the error", (done)->
@request.get = sinon.stub().callsArgWith(1, @error, {}, {})
@requires["request"] = @request
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.unarchiveDoc @project_id, @docs[0], (err)=>
err.should.equal @error
done()
describe "archiveAllDocs", ->
it "should archive all project docs", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.archiveDoc = sinon.stub().callsArgWith(2, null)
@DocArchiveManager.archiveAllDocs @project_id, (err)=>
for doc in @docs
@DocArchiveManager.archiveDoc.calledWith(@project_id, doc).should.equal true
should.not.exist err
done()
it "should return error if have no docs", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, null)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.archiveAllDocs @project_id, (err)=>
should.exist err
done()
it "should return the error", (done)->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, @error, null)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@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, @docs)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.unarchiveDoc = sinon.stub().callsArgWith(2, null)
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
for doc in @docs
@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)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
should.exist err
done()
it "should return the error", (done)->
@MongoManager.getArchivedProjectDocs = sinon.stub().callsArgWith(1, @error, null)
@requires["./MongoManager"] = @MongoManager
@DocArchiveManager = SandboxedModule.require modulePath, requires: @requires
@DocArchiveManager.unArchiveAllDocs @project_id, (err)=>
err.should.equal @error
done()

View file

@ -11,6 +11,7 @@ describe "DocManager", ->
beforeEach ->
@DocManager = SandboxedModule.require modulePath, requires:
"./MongoManager": @MongoManager = {}
"./DocArchiveManager": @DocArchiveManager = {}
"logger-sharelatex": @logger =
log: sinon.stub()
warn:->
@ -74,6 +75,7 @@ describe "DocManager", ->
beforeEach ->
@docs = [{ _id: @doc_id, lines: ["mock-lines"] }]
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, @docs)
@DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@DocManager.getAllDocs @project_id, @callback
it "should get the project from the database", ->
@ -87,6 +89,7 @@ describe "DocManager", ->
describe "when there are no docs for the project", ->
beforeEach ->
@MongoManager.getProjectsDocs = sinon.stub().callsArgWith(1, null, null)
@DocArchiveManager.unArchiveAllDocs = sinon.stub().callsArgWith(1, null, null)
@DocManager.getAllDocs @project_id, @callback
it "should return a NotFoundError", ->