From 5971a19084d0b4df28a1ea2af65e8220f46a2913 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 11 Aug 2017 16:55:31 +0100 Subject: [PATCH] added acceptance tests --- .../coffee/GettingProjectDocsTests.coffee | 68 +++++++++++++++++++ .../coffee/helpers/DocUpdaterClient.coffee | 6 ++ 2 files changed, 74 insertions(+) create mode 100644 services/document-updater/test/acceptance/coffee/GettingProjectDocsTests.coffee diff --git a/services/document-updater/test/acceptance/coffee/GettingProjectDocsTests.coffee b/services/document-updater/test/acceptance/coffee/GettingProjectDocsTests.coffee new file mode 100644 index 0000000000..708176ea69 --- /dev/null +++ b/services/document-updater/test/acceptance/coffee/GettingProjectDocsTests.coffee @@ -0,0 +1,68 @@ +sinon = require "sinon" +chai = require("chai") +chai.should() +expect = chai.expect + +MockWebApi = require "./helpers/MockWebApi" +DocUpdaterClient = require "./helpers/DocUpdaterClient" + +describe "Getting documents for project", -> + before (done) -> + @lines = ["one", "two", "three"] + @version = 42 + setTimeout done, 200 # Give MockWebApi a chance to start + + describe "when project state hash does not match", -> + before (done) -> + @projectStateHash = DocUpdaterClient.randomId() + [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] + + MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version} + DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) => + throw error if error? + DocUpdaterClient.getProjectDocs @project_id, @projectStateHash, (error, @res, @returnedDocs) => + done() + + it "should return a 409 Conflict response", -> + @res.statusCode.should.equal 409 + + + describe "when project state hash matches", -> + before (done) -> + @projectStateHash = DocUpdaterClient.randomId() + [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] + + MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version} + DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) => + throw error if error? + DocUpdaterClient.getProjectDocs @project_id, @projectStateHash, (error, @res0, @returnedDocs0) => + # set the hash + DocUpdaterClient.getProjectDocs @project_id, @projectStateHash, (error, @res, @returnedDocs) => + # the hash should now match + done() + + it "should return a 200 response", -> + @res.statusCode.should.equal 200 + + it "should return the documents", -> + @returnedDocs.should.deep.equal [ {_id: @doc_id, lines: @lines, v: @version} ] + + + describe "when the doc has been removed", -> + before (done) -> + @projectStateHash = DocUpdaterClient.randomId() + [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()] + + MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version} + DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) => + throw error if error? + DocUpdaterClient.getProjectDocs @project_id, @projectStateHash, (error, @res0, @returnedDocs0) => + # set the hash + DocUpdaterClient.deleteDoc @project_id, @doc_id, (error, res, body) => + # delete the doc + DocUpdaterClient.getProjectDocs @project_id, @projectStateHash, (error, @res, @returnedDocs) => + # the hash would match, but the doc has been deleted + done() + + it "should return a 409 Conflict response", -> + @res.statusCode.should.equal 409 diff --git a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee index 4b57e0659f..6b2a5ac2fb 100644 --- a/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee +++ b/services/document-updater/test/acceptance/coffee/helpers/DocUpdaterClient.coffee @@ -80,3 +80,9 @@ module.exports = DocUpdaterClient = removeComment: (project_id, doc_id, comment, callback = () ->) -> request.del "http://localhost:3003/project/#{project_id}/doc/#{doc_id}/comment/#{comment}", callback + + getProjectDocs: (project_id, projectStateHash, callback = () ->) -> + request.get "http://localhost:3003/project/#{project_id}/doc?state=#{projectStateHash}", (error, res, body) -> + if body? and res.statusCode >= 200 and res.statusCode < 300 + body = JSON.parse(body) + callback error, res, body