From da9e7dc7e1db69ac9023f09081edf98f45ee1d02 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 2 Sep 2015 18:47:34 -0300 Subject: [PATCH] init archive acceptance tests --- .../app/coffee/DocArchiveManager.coffee | 4 +- .../coffee/ArchivingUpdatesTests.coffee | 88 +++++++++++++++++++ .../coffee/helpers/MockDocStoreApi.coffee | 24 +++++ .../coffee/helpers/TrackChangesClient.coffee | 16 +++- 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee create mode 100644 services/track-changes/test/acceptance/coffee/helpers/MockDocStoreApi.coffee diff --git a/services/track-changes/app/coffee/DocArchiveManager.coffee b/services/track-changes/app/coffee/DocArchiveManager.coffee index e0d0d1bf1a..ab54a83039 100644 --- a/services/track-changes/app/coffee/DocArchiveManager.coffee +++ b/services/track-changes/app/coffee/DocArchiveManager.coffee @@ -36,7 +36,7 @@ module.exports = DocArchiveManager = else MongoManager.getLastCompressedUpdate doc_id, (error, update) -> MongoAWS.archiveDocHistory project_id, doc_id, (error) -> - logger.log doc_id:doc_id, error: error, "mongoexport" + logger.log doc_id:doc_id, error: error, "export to S3" MongoManager.markDocHistoryAsArchived doc_id, update, (error) -> return callback(error) if error? callback() @@ -59,7 +59,7 @@ module.exports = DocArchiveManager = return callback() else MongoAWS.unArchiveDocHistory project_id, doc_id, (error) -> - logger.log doc_id:doc_id, error: error, "mongoimport" + logger.log doc_id:doc_id, error: error, "import from S3" MongoManager.markDocHistoryAsUnarchived doc_id, (error) -> return callback(error) if error? callback() diff --git a/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee b/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee new file mode 100644 index 0000000000..4859d28761 --- /dev/null +++ b/services/track-changes/test/acceptance/coffee/ArchivingUpdatesTests.coffee @@ -0,0 +1,88 @@ +sinon = require "sinon" +chai = require("chai") +chai.should() +expect = chai.expect +mongojs = require "../../../app/js/mongojs" +db = mongojs.db +ObjectId = mongojs.ObjectId +Settings = require "settings-sharelatex" +request = require "request" +rclient = require("redis").createClient() # Only works locally for now + +TrackChangesClient = require "./helpers/TrackChangesClient" +MockDocStoreApi = require "./helpers/MockDocStoreApi" +MockWebApi = require "./helpers/MockWebApi" + +describe "Archiving updates", -> + before (done) -> + @now = Date.now() + @to = @now + @user_id = ObjectId().toString() + @doc_id = ObjectId().toString() + @project_id = ObjectId().toString() + + @minutes = 60 * 1000 + @hours = 60 * @minutes + + MockWebApi.projects[@project_id] = + features: + versioning: true + + MockWebApi.users[@user_id] = @user = + email: "user@sharelatex.com" + first_name: "Leo" + last_name: "Lion" + id: @user_id + sinon.spy MockWebApi, "getUser" + + MockDocStoreApi.docs[@doc_id] = @doc = + _id: @doc_id + project_id: @project_id + sinon.spy MockDocStoreApi, "getAllDoc" + + @updates = [] + for i in [0..9] + @updates.push { + op: [{ i: "a", p: 0 }] + meta: { ts: @now - (9 - i) * @hours - 2 * @minutes, user_id: @user_id } + v: 2 * i + 1 + } + @updates.push { + op: [{ i: "b", p: 0 }] + meta: { ts: @now - (9 - i) * @hours, user_id: @user_id } + v: 2 * i + 2 + } + + TrackChangesClient.pushRawUpdates @project_id, @doc_id, @updates, (error) => + throw error if error? + TrackChangesClient.flushDoc @project_id, @doc_id, (error) -> + throw error if error? + done() + + after: () -> + MockWebApi.getUser.restore() + + describe "archiving a doc's updates", -> + before (done) -> + + TrackChangesClient.archiveProject @project_id, (error) -> + throw error if error? + done() + + it "should remain one doc", (done) -> + db.docHistory.count { doc_id: ObjectId(@doc_id) }, (error, count) -> + throw error if error? + count.should.equal 1 + done() + + it "should remained doc marked as inS3", (done) -> + db.docHistory.findOne { doc_id: ObjectId(@doc_id) }, (error, doc) -> + throw error if error? + doc.inS3.should.equal true + done() + + it "should remained doc have last version", (done) -> + 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 diff --git a/services/track-changes/test/acceptance/coffee/helpers/MockDocStoreApi.coffee b/services/track-changes/test/acceptance/coffee/helpers/MockDocStoreApi.coffee new file mode 100644 index 0000000000..29864479a4 --- /dev/null +++ b/services/track-changes/test/acceptance/coffee/helpers/MockDocStoreApi.coffee @@ -0,0 +1,24 @@ +express = require("express") +app = express() + +module.exports = MockDocUpdaterApi = + docs: {} + + getAllDoc: (project_id, callback = (error) ->) -> + callback null, @docs + + run: () -> + app.get "/project/:project_id/doc", (req, res, next) => + @getAllDoc req.params.project_id, (error, docs) -> + if error? + res.send 500 + if !docs? + res.send 404 + else + res.send JSON.stringify docs + + app.listen 3016, (error) -> + throw error if error? + +MockDocUpdaterApi.run() + diff --git a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee index 661b0eafb8..552d455d31 100644 --- a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee +++ b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee @@ -71,4 +71,18 @@ module.exports = TrackChangesClient = "X-User-Id": user_id }, (error, response, body) => response.statusCode.should.equal 204 - callback null \ No newline at end of file + callback null + + archiveProject: (project_id, callback = (error) ->) -> + request.get { + url: "http://localhost:3015/project/#{project_id}/archive" + }, (error, response, body) => + response.statusCode.should.equal 204 + callback(error) + + unarchiveProject: (project_id, callback = (error) ->) -> + request.get { + url: "http://localhost:3015/project/#{project_id}/unarchive" + }, (error, response, body) => + response.statusCode.should.equal 204 + callback(error) \ No newline at end of file