From 9ee913be39a5ccac287187c23167a337a715ee8b Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 1 Dec 2016 16:40:15 +0000 Subject: [PATCH] Update PersistenceManagerTests --- .../app/coffee/PersistenceManager.coffee | 6 +- .../PersistenceManagerTests.coffee | 176 ++++++++++++++++++ .../PersistenceManager/getDocTests.coffee | 104 ----------- .../PersistenceManager/setDocTests.coffee | 90 --------- 4 files changed, 178 insertions(+), 198 deletions(-) create mode 100644 services/document-updater/test/unit/coffee/PersistenceManager/PersistenceManagerTests.coffee delete mode 100644 services/document-updater/test/unit/coffee/PersistenceManager/getDocTests.coffee delete mode 100644 services/document-updater/test/unit/coffee/PersistenceManager/setDocTests.coffee diff --git a/services/document-updater/app/coffee/PersistenceManager.coffee b/services/document-updater/app/coffee/PersistenceManager.coffee index fff037f6fc..25eb1f32a2 100644 --- a/services/document-updater/app/coffee/PersistenceManager.coffee +++ b/services/document-updater/app/coffee/PersistenceManager.coffee @@ -39,7 +39,7 @@ module.exports = PersistenceManager = return callback(new Error("web API response had no doc lines")) if !body.version? or not body.version instanceof Number return callback(new Error("web API response had no valid doc version")) - return callback null, body.lines, body.track_changes, body.track_changes_entries + return callback null, body.lines, body.version, body.track_changes, body.track_changes_entries else if res.statusCode == 404 return callback(new Errors.NotFoundError("doc not not found: #{url}")) else @@ -55,13 +55,11 @@ module.exports = PersistenceManager = request { url: url method: "POST" - body: JSON.stringify + json: lines: lines track_changes: track_changes track_changes_entries: track_changes_entries version: version - headers: - "content-type": "application/json" auth: user: Settings.apis.web.user pass: Settings.apis.web.pass diff --git a/services/document-updater/test/unit/coffee/PersistenceManager/PersistenceManagerTests.coffee b/services/document-updater/test/unit/coffee/PersistenceManager/PersistenceManagerTests.coffee new file mode 100644 index 0000000000..e9f2cf212e --- /dev/null +++ b/services/document-updater/test/unit/coffee/PersistenceManager/PersistenceManagerTests.coffee @@ -0,0 +1,176 @@ +sinon = require('sinon') +chai = require('chai') +should = chai.should() +modulePath = "../../../../app/js/PersistenceManager.js" +SandboxedModule = require('sandboxed-module') +Errors = require "../../../../app/js/Errors" + +describe "PersistenceManager", -> + beforeEach -> + @PersistenceManager = SandboxedModule.require modulePath, requires: + "request": @request = sinon.stub() + "settings-sharelatex": @Settings = {} + "./Metrics": @Metrics = + Timer: class Timer + done: sinon.stub() + "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()} + @project_id = "project-id-123" + @doc_id = "doc-id-123" + @lines = ["one", "two", "three"] + @version = 42 + @callback = sinon.stub() + @track_changes_on = true + @track_changes_entries = { comments: "mock", entries: "mock" } + @Settings.apis = + web: + url: @url = "www.example.com" + user: @user = "sharelatex" + pass: @pass = "password" + + describe "getDoc", -> + + describe "with a successful response from the web api", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify({ + lines: @lines, + version: @version, + track_changes: @track_changes_on, + track_changes_entries: @track_changes_entries + })) + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should call the web api", -> + @request + .calledWith({ + url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}" + method: "GET" + headers: + "accept": "application/json" + auth: + user: @user + pass: @pass + sendImmediately: true + jar: false + timeout: 5000 + }) + .should.equal true + + it "should call the callback with the doc lines, version and track changes state", -> + @callback.calledWith(null, @lines, @version, @track_changes_on, @track_changes_entries).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when request returns an error", -> + beforeEach -> + @request.callsArgWith(1, @error = new Error("oops"), null, null) + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should return the error", -> + @callback.calledWith(@error).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when the request returns 404", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 404}, "") + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should return a NotFoundError", -> + @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when the request returns an error status code", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 500}, "") + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should return an error", -> + @callback.calledWith(new Error("web api error")).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when request returns an doc without lines", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(version: @version)) + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should return and error", -> + @callback.calledWith(new Error("web API response had no doc lines")).should.equal true + + describe "when request returns an doc without a version", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines)) + @PersistenceManager.getDoc(@project_id, @doc_id, @callback) + + it "should return and error", -> + @callback.calledWith(new Error("web API response had no valid doc version")).should.equal true + + describe "setDoc", -> + describe "with a successful response from the web api", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 200}) + @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @track_changes_on, @track_changes_entries, @callback) + + it "should call the web api", -> + @request + .calledWith({ + url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}" + json: + lines: @lines + version: @version + track_changes: @track_changes_on + track_changes_entries: @track_changes_entries + method: "POST" + auth: + user: @user + pass: @pass + sendImmediately: true + jar: false + timeout: 5000 + }) + .should.equal true + + it "should call the callback without error", -> + @callback.calledWith(null).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when request returns an error", -> + beforeEach -> + @request.callsArgWith(1, @error = new Error("oops"), null, null) + @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @track_changes_on, @track_changes_entries, @callback) + + it "should return the error", -> + @callback.calledWith(@error).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when the request returns 404", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 404}, "") + @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @track_changes_on, @track_changes_entries, @callback) + + it "should return a NotFoundError", -> + @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + + describe "when the request returns an error status code", -> + beforeEach -> + @request.callsArgWith(1, null, {statusCode: 500}, "") + @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @track_changes_on, @track_changes_entries, @callback) + + it "should return an error", -> + @callback.calledWith(new Error("web api error")).should.equal true + + it "should time the execution", -> + @Metrics.Timer::done.called.should.equal true + diff --git a/services/document-updater/test/unit/coffee/PersistenceManager/getDocTests.coffee b/services/document-updater/test/unit/coffee/PersistenceManager/getDocTests.coffee deleted file mode 100644 index d4f44afa46..0000000000 --- a/services/document-updater/test/unit/coffee/PersistenceManager/getDocTests.coffee +++ /dev/null @@ -1,104 +0,0 @@ -sinon = require('sinon') -chai = require('chai') -should = chai.should() -modulePath = "../../../../app/js/PersistenceManager.js" -SandboxedModule = require('sandboxed-module') -Errors = require "../../../../app/js/Errors" - -describe "PersistenceManager.getDoc", -> - beforeEach -> - @PersistenceManager = SandboxedModule.require modulePath, requires: - "request": @request = sinon.stub() - "settings-sharelatex": @Settings = {} - "./Metrics": @Metrics = - Timer: class Timer - done: sinon.stub() - "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()} - @project_id = "project-id-123" - @doc_id = "doc-id-123" - @lines = ["one", "two", "three"] - @version = 42 - @callback = sinon.stub() - @Settings.apis = - web: - url: @url = "www.example.com" - user: @user = "sharelatex" - pass: @pass = "password" - - describe "with a successful response from the web api", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines, version: @version)) - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should call the web api", -> - @request - .calledWith({ - url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}" - method: "GET" - headers: - "accept": "application/json" - auth: - user: @user - pass: @pass - sendImmediately: true - jar: false - timeout: 5000 - }) - .should.equal true - - it "should call the callback with the doc lines and version", -> - @callback.calledWith(null, @lines, @version).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when request returns an error", -> - beforeEach -> - @request.callsArgWith(1, @error = new Error("oops"), null, null) - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should return the error", -> - @callback.calledWith(@error).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when the request returns 404", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 404}, "") - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should return a NotFoundError", -> - @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when the request returns an error status code", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 500}, "") - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should return an error", -> - @callback.calledWith(new Error("web api error")).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when request returns an doc without lines", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(version: @version)) - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should return and error", -> - @callback.calledWith(new Error("web API response had no doc lines")).should.equal true - - describe "when request returns an doc without a version", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines)) - @PersistenceManager.getDoc(@project_id, @doc_id, @callback) - - it "should return and error", -> - @callback.calledWith(new Error("web API response had no valid doc version")).should.equal true - - diff --git a/services/document-updater/test/unit/coffee/PersistenceManager/setDocTests.coffee b/services/document-updater/test/unit/coffee/PersistenceManager/setDocTests.coffee deleted file mode 100644 index 98f252a35d..0000000000 --- a/services/document-updater/test/unit/coffee/PersistenceManager/setDocTests.coffee +++ /dev/null @@ -1,90 +0,0 @@ -sinon = require('sinon') -chai = require('chai') -should = chai.should() -modulePath = "../../../../app/js/PersistenceManager.js" -SandboxedModule = require('sandboxed-module') -Errors = require "../../../../app/js/Errors" - -describe "PersistenceManager.setDoc", -> - beforeEach -> - @PersistenceManager = SandboxedModule.require modulePath, requires: - "request": @request = sinon.stub() - "settings-sharelatex": @Settings = {} - "./Metrics": @Metrics = - Timer: class Timer - done: sinon.stub() - "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()} - @project_id = "project-id-123" - @doc_id = "doc-id-123" - @lines = ["one", "two", "three"] - @version = 42 - @callback = sinon.stub() - @Settings.apis = - web: - url: @url = "www.example.com" - user: @user = "sharelatex" - pass: @pass = "password" - - describe "with a successful response from the web api", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines, version: @version)) - @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback) - - it "should call the web api", -> - @request - .calledWith({ - url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}" - body: JSON.stringify - lines: @lines - version: @version - method: "POST" - headers: - "content-type": "application/json" - auth: - user: @user - pass: @pass - sendImmediately: true - jar: false - timeout: 5000 - }) - .should.equal true - - it "should call the callback without error", -> - @callback.calledWith(null).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when request returns an error", -> - beforeEach -> - @request.callsArgWith(1, @error = new Error("oops"), null, null) - @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback) - - it "should return the error", -> - @callback.calledWith(@error).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when the request returns 404", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 404}, "") - @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback) - - it "should return a NotFoundError", -> - @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true - - describe "when the request returns an error status code", -> - beforeEach -> - @request.callsArgWith(1, null, {statusCode: 500}, "") - @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @callback) - - it "should return an error", -> - @callback.calledWith(new Error("web api error")).should.equal true - - it "should time the execution", -> - @Metrics.Timer::done.called.should.equal true -