From d4295c20232c41a7e1c8e9252e8400978ca3d423 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 27 Jan 2014 18:09:37 +0000 Subject: [PATCH] Create HttpController for HTTP requestS --- services/track-changes/app.coffee | 17 +++------ .../app/coffee/HttpController.coffee | 12 +++++++ .../HttpController/HttpControllerTests.coffee | 36 +++++++++++++++++++ 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 services/track-changes/app/coffee/HttpController.coffee create mode 100644 services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee diff --git a/services/track-changes/app.coffee b/services/track-changes/app.coffee index b208df2368..412f32315a 100644 --- a/services/track-changes/app.coffee +++ b/services/track-changes/app.coffee @@ -1,19 +1,12 @@ Settings = require "settings-sharelatex" -express = require "express" -app = express() - -HistoryManager = require "./app/js/HistoryManager" logger = require "logger-sharelatex" logger.initialize("history") -app.post "/doc/:doc_id/history", express.bodyParser(), (req, res, next) -> - doc_id = req.params.doc_id - docOps = req.body.docOps - version = req.body.version - logger.log doc_id: doc_id, version: version, "compressing doc history" - HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) -> - return next(error) if error? - res.send 204 +HttpController = require "./app/js/HttpController" +express = require "express" +app = express() + +app.post "/doc/:doc_id/history", express.bodyParser(), HttpController.appendUpdates app.use (error, req, res, next) -> logger.error err: error, "an internal error occured" diff --git a/services/track-changes/app/coffee/HttpController.coffee b/services/track-changes/app/coffee/HttpController.coffee new file mode 100644 index 0000000000..99e745078f --- /dev/null +++ b/services/track-changes/app/coffee/HttpController.coffee @@ -0,0 +1,12 @@ +HistoryManager = require "./HistoryManager" +logger = require "logger-sharelatex" + +module.exports = HttpController = + appendUpdates: (req, res, next = (error) ->) -> + doc_id = req.params.doc_id + docOps = req.body.docOps + version = req.body.version + logger.log doc_id: doc_id, version: version, "compressing doc history" + HistoryManager.compressAndSaveRawUpdates doc_id, docOps, (error) -> + return next(error) if error? + res.send 204 diff --git a/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee new file mode 100644 index 0000000000..f1b84020dc --- /dev/null +++ b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee @@ -0,0 +1,36 @@ +sinon = require('sinon') +chai = require('chai') +should = chai.should() +expect = chai.expect +modulePath = "../../../../app/js/HttpController.js" +SandboxedModule = require('sandboxed-module') + +describe "HttpController", -> + beforeEach -> + @HttpController = SandboxedModule.require modulePath, requires: + "logger-sharelatex": { log: sinon.stub() } + "./HistoryManager": @HistoryManager = {} + @doc_id = "doc-id-123" + @version = 42 + @next = sinon.stub() + + describe "appendUpdates", -> + beforeEach -> + @req = + params: + doc_id: @doc_id + body: + docOps: @docOps = ["mock-ops"] + version: @version + @res = + send: sinon.stub() + @HistoryManager.compressAndSaveRawUpdates = sinon.stub().callsArg(2) + @HttpController.appendUpdates @req, @res, @next + + it "should append the updates", -> + @HistoryManager.compressAndSaveRawUpdates + .calledWith(@doc_id, @docOps) + .should.equal true + + it "should return a success code", -> + @res.send.calledWith(204).should.equal true \ No newline at end of file