From a72d2283543fe6e78cd5bcc966895f189344d611 Mon Sep 17 00:00:00 2001 From: Hayden Faulds Date: Wed, 7 Mar 2018 13:29:53 +0000 Subject: [PATCH] add unit tests --- .../DocumentManagerTests.coffee | 39 +++++++++++++++++++ .../HistoryManager/HistoryManagerTests.coffee | 31 +++++++++++++++ .../HttpController/HttpControllerTests.coffee | 36 +++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/services/document-updater/test/unit/coffee/DocumentManager/DocumentManagerTests.coffee b/services/document-updater/test/unit/coffee/DocumentManager/DocumentManagerTests.coffee index c390138cf9..69ab704094 100644 --- a/services/document-updater/test/unit/coffee/DocumentManager/DocumentManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/DocumentManager/DocumentManagerTests.coffee @@ -467,3 +467,42 @@ describe "DocumentManager", -> it "should call the callback", -> @callback.called.should.equal true + + describe "resyncDocContents", -> + describe "when doc is loaded in redis", -> + beforeEach -> + @RedisManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, @pathname) + @RedisManager.queueResyncDocContents = sinon.stub() + @DocumentManager.resyncDocContents @project_id, @doc_id, @callback + + it "gets the doc contents from redis", -> + @RedisManager.getDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "queues a resync doc content update", -> + @RedisManager.queueResyncDocContents + .calledWith(@project_id, @doc_id, @lines, @version, @pathname, @callback) + .should.equal true + + describe "when doc is not loaded in redis", -> + beforeEach -> + @RedisManager.getDoc = sinon.stub().callsArgWith(2, null) + @PersistenceManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @ranges, @pathname) + @RedisManager.queueResyncDocContents = sinon.stub() + @DocumentManager.resyncDocContents @project_id, @doc_id, @callback + + it "tries to get the doc contents from redis", -> + @RedisManager.getDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "gets the doc contents from web", -> + @PersistenceManager.getDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "queues a resync doc content update", -> + @RedisManager.queueResyncDocContents + .calledWith(@project_id, @doc_id, @lines, @version, @pathname, @callback) + .should.equal true diff --git a/services/document-updater/test/unit/coffee/HistoryManager/HistoryManagerTests.coffee b/services/document-updater/test/unit/coffee/HistoryManager/HistoryManagerTests.coffee index 6e5010d89c..c268a86ba7 100644 --- a/services/document-updater/test/unit/coffee/HistoryManager/HistoryManagerTests.coffee +++ b/services/document-updater/test/unit/coffee/HistoryManager/HistoryManagerTests.coffee @@ -16,7 +16,9 @@ describe "HistoryManager", -> url: "http://trackchanges.example.com" } "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() } + "./DocumentManager": @DocumentManager = {} "./HistoryRedisManager": @HistoryRedisManager = {} + "./RedisManager": @RedisManager = {} @project_id = "mock-project-id" @doc_id = "mock-doc-id" @callback = sinon.stub() @@ -158,3 +160,32 @@ describe "HistoryManager", -> # Previously we were on 16 ops # We didn't pass over a multiple of 5 @HistoryManager.shouldFlushHistoryOps(17, ['a', 'b', 'c'].length, 5).should.equal true + + describe "resyncProjectHistory", -> + beforeEach -> + @docs = [ + doc: @doc_id + path: 'main.tex' + ] + @files = [ + file: 'mock-file-id' + path: 'universe.png' + url: "www.filestore.test/#{@project_id}/mock-file-id" + ] + @RedisManager.queueResyncProjectStructure = sinon.stub().yields() + @DocumentManager.resyncDocContentsWithLock = sinon.stub().yields() + @HistoryManager.resyncProjectHistory @project_id, @docs, @files, @callback + + it "should queue a project structure reync", -> + @RedisManager.queueResyncProjectStructure + .calledWith(@project_id, @docs, @files) + .should.equal true + + it "should queue doc content reyncs", -> + @DocumentManager + .resyncDocContentsWithLock + .calledWith(@project_id, @doc_id) + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true diff --git a/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee b/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee index 99496332fc..b79659cada 100644 --- a/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee +++ b/services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee @@ -544,3 +544,39 @@ describe "HttpController", -> @next .calledWith(new Error("oops")) .should.equal true + + describe "resyncProjectHistory", -> + beforeEach -> + @docs = sinon.stub() + @files = sinon.stub() + @fileUpdates = sinon.stub() + @req = + body: + {@docs, @files} + params: + project_id: @project_id + + describe "successfully", -> + beforeEach -> + @HistoryManager.resyncProjectHistory = sinon.stub().callsArg(3) + @HttpController.resyncProjectHistory(@req, @res, @next) + + it "should accept the change", -> + @HistoryManager.resyncProjectHistory + .calledWith(@project_id, @docs, @files) + .should.equal true + + it "should return a successful No Content response", -> + @res.send + .calledWith(204) + .should.equal true + + describe "when an errors occurs", -> + beforeEach -> + @HistoryManager.resyncProjectHistory = sinon.stub().callsArgWith(3, new Error("oops")) + @HttpController.resyncProjectHistory(@req, @res, @next) + + it "should call next with the error", -> + @next + .calledWith(new Error("oops")) + .should.equal true