diff --git a/services/track-changes/app/coffee/MongoAWS.coffee b/services/track-changes/app/coffee/MongoAWS.coffee index aedd474c48..3fba799d22 100644 --- a/services/track-changes/app/coffee/MongoAWS.coffee +++ b/services/track-changes/app/coffee/MongoAWS.coffee @@ -72,10 +72,13 @@ module.exports = MongoAWS = op.doc_id = ObjectId(op.doc_id) op.project_id = ObjectId(op.project_id) bulk.find({_id:op._id}).upsert().updateOne(op) - - bulk.execute (err, result) -> - if err? - logger.error err:err, "error bulking ReadlineStream" - else - logger.log count:ops.length, result:result, "bulked ReadlineStream" - cb(err) + + if ops.length > 0 + bulk.execute (err, result) -> + if err? + logger.error err:err, "error bulking ReadlineStream" + else + logger.log count:ops.length, result:result, "bulked ReadlineStream" + cb(err) + else + cb() diff --git a/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee b/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee index 4859d28761..973d4bde06 100644 --- a/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee +++ b/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee @@ -59,17 +59,18 @@ describe "Archiving updates", -> throw error if error? done() - after: () -> + after (done) -> MockWebApi.getUser.restore() + db.docHistory.remove {project_id: ObjectId(@project_id)} + TrackChangesClient.removeS3Doc @project_id, @doc_id, done describe "archiving a doc's updates", -> before (done) -> - TrackChangesClient.archiveProject @project_id, (error) -> throw error if error? done() - it "should remain one doc", (done) -> + it "should remain one doc change", (done) -> db.docHistory.count { doc_id: ObjectId(@doc_id) }, (error, count) -> throw error if error? count.should.equal 1 @@ -85,4 +86,27 @@ describe "Archiving updates", -> db.docHistory.findOne { doc_id: ObjectId(@doc_id) }, (error, doc) -> throw error if error? doc.v.should.equal 20 - done() \ No newline at end of file + done() + + it "should store twenty doc changes in S3", (done) -> + TrackChangesClient.getS3Doc @project_id, @doc_id, (error, res, doc) => + doc.length.should.equal 20 + done() + + describe "unarchiving a doc's updates", -> + before (done) -> + TrackChangesClient.unarchiveProject @project_id, (error) -> + throw error if error? + done() + + it "should restore doc changes", (done) -> + db.docHistory.count { doc_id: ObjectId(@doc_id) }, (error, count) -> + throw error if error? + count.should.equal 20 + done() + + it "should remove doc marked as inS3", (done) -> + db.docHistory.count { doc_id: ObjectId(@doc_id), inS3 : true }, (error, count) -> + throw error if error? + count.should.equal 0 + done() diff --git a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee index 552d455d31..1e409908d9 100644 --- a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee +++ b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee @@ -1,6 +1,7 @@ request = require "request" rclient = require("redis").createClient() # Only works locally for now {db, ObjectId} = require "../../../../app/js/mongojs" +Settings = require "settings-sharelatex" module.exports = TrackChangesClient = flushAndGetCompressedUpdates: (project_id, doc_id, callback = (error, updates) ->) -> @@ -85,4 +86,23 @@ module.exports = TrackChangesClient = url: "http://localhost:3015/project/#{project_id}/unarchive" }, (error, response, body) => response.statusCode.should.equal 204 - callback(error) \ No newline at end of file + callback(error) + + buildS3Options: (content, key)-> + return { + aws: + key: Settings.filestore.s3.key + secret: Settings.filestore.s3.secret + bucket: Settings.filestore.stores.user_files + timeout: 30 * 1000 + json: content + uri:"https://#{Settings.filestore.stores.user_files}.s3.amazonaws.com/#{key}" + } + + getS3Doc: (project_id, doc_id, callback = (error, res, body) ->) -> + options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id) + request.get options, callback + + removeS3Doc: (project_id, doc_id, callback = (error, res, body) ->) -> + options = TrackChangesClient.buildS3Options(true, project_id+"/changes-"+doc_id) + request.del options, callback \ No newline at end of file