From f1c33ae5cec570c36643325e5d116db4124a379f Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 9 Apr 2020 17:36:03 +1000 Subject: [PATCH] feat: add create-or-update-pacticipant --- .../Pact Broker Client - Pact Broker.md | 158 ++++++++++++++++ lib/pact_broker/client/cli/broker.rb | 12 ++ lib/pact_broker/client/cli/custom_thor.rb | 4 + lib/pact_broker/client/hal/entity.rb | 13 +- lib/pact_broker/client/hal/http_client.rb | 5 + lib/pact_broker/client/hal/link.rb | 4 + lib/pact_broker/client/hal_client_methods.rb | 15 ++ lib/pact_broker/client/pacticipants/create.rb | 57 ++++++ .../client/pacticipants/create_spec.rb | 28 +++ .../pacts/pact_broker_client-pact_broker.json | 169 ++++++++++++++++++ spec/service_providers/pact_helper.rb | 29 +++ .../pacticipants_create_spec.rb | 118 ++++++++++++ 12 files changed, 611 insertions(+), 1 deletion(-) create mode 100644 lib/pact_broker/client/hal_client_methods.rb create mode 100644 lib/pact_broker/client/pacticipants/create.rb create mode 100644 spec/lib/pact_broker/client/pacticipants/create_spec.rb create mode 100644 spec/service_providers/pacticipants_create_spec.rb diff --git a/doc/pacts/markdown/Pact Broker Client - Pact Broker.md b/doc/pacts/markdown/Pact Broker Client - Pact Broker.md index 8186af05..49110ab6 100644 --- a/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +++ b/doc/pacts/markdown/Pact Broker Client - Pact Broker.md @@ -22,6 +22,8 @@ * [A request for the index resource](#a_request_for_the_index_resource) +* [A request for the index resource](#a_request_for_the_index_resource_given_the_pacticipant_relations_are_present) given the pacticipant relations are present + * [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-tagged-version_relation_exists_in_the_index_resource) given the pb:latest-tagged-version relation exists in the index resource * [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:latest-version_relation_exists_in_the_index_resource) given the pb:latest-version relation exists in the index resource @@ -38,6 +40,8 @@ * [A request to create a global webhook with a JSON body](#a_request_to_create_a_global_webhook_with_a_JSON_body) +* [A request to create a pacticipant](#a_request_to_create_a_pacticipant) + * [A request to create a webhook for a consumer and provider](#a_request_to_create_a_webhook_for_a_consumer_and_provider_given_'Condor'_does_not_exist_in_the_pact-broker) given 'Condor' does not exist in the pact-broker * [A request to create a webhook with a JSON body and a uuid](#a_request_to_create_a_webhook_with_a_JSON_body_and_a_uuid_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker @@ -76,6 +80,10 @@ * [A request to register the repository URL of a pacticipant](#a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_'Pricing_Service'_does_not_exist_in_the_pact-broker) given the 'Pricing Service' does not exist in the pact-broker +* [A request to retrieve a pacticipant](#a_request_to_retrieve_a_pacticipant_given_a_pacticipant_with_name_Foo_exists) given a pacticipant with name Foo exists + +* [A request to retrieve a pacticipant](#a_request_to_retrieve_a_pacticipant) + * [A request to retrieve the latest 'production' version of Condor](#a_request_to_retrieve_the_latest_'production'_version_of_Condor_given_'Condor'_exists_in_the_pact-broker_with_the_latest_tagged_'production'_version_1.2.3) given 'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3 * [A request to retrieve the latest pact between Condor and the Pricing Service](#a_request_to_retrieve_the_latest_pact_between_Condor_and_the_Pricing_Service_given_a_pact_between_Condor_and_the_Pricing_Service_exists) given a pact between Condor and the Pricing Service exists @@ -92,6 +100,8 @@ * [A request to tag the production version of Condor](#a_request_to_tag_the_production_version_of_Condor_given_'Condor'_exists_in_the_pact-broker) given 'Condor' exists in the pact-broker +* [A request to update a pacticipant](#a_request_to_update_a_pacticipant_given_a_pacticipant_with_name_Foo_exists) given a pacticipant with name Foo exists + * [A request to update a webhook](#a_request_to_update_a_webhook_given_a_webhook_with_the_uuid_696c5f93-1b7f-44bc-8d03-59440fcaa9a0_exists) given a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists * [An invalid request to create a webhook for a consumer and provider](#an_invalid_request_to_create_a_webhook_for_a_consumer_and_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker @@ -506,6 +516,42 @@ Pact Broker will respond with: "_links": { "pb:webhooks": { "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-WEBHOOKS" + }, + "pb:pacticipants": { + "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANTS" + }, + "pb:pacticipant": { + "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-{pacticipant}" + } + } + } +} +``` + +Given **the pacticipant relations are present**, upon receiving **a request for the index resource** from Pact Broker Client, with +```json +{ + "method": "get", + "path": "/", + "headers": { + "Accept": "application/hal+json" + } +} +``` +Pact Broker will respond with: +```json +{ + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "_links": { + "pb:pacticipants": { + "href": "http://localhost:1234/pacticipants" + }, + "pb:pacticipant": { + "href": "http://localhost:1234/pacticipants/{pacticipant}" } } } @@ -778,6 +824,40 @@ Pact Broker will respond with: } } ``` + +Upon receiving **a request to create a pacticipant** from Pact Broker Client, with +```json +{ + "method": "post", + "path": "/pacticipants", + "headers": { + "Content-Type": "application/json", + "Accept": "application/hal+json" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo" + } +} +``` +Pact Broker will respond with: +```json +{ + "status": 201, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo", + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + } +} +``` Given **'Condor' does not exist in the pact-broker**, upon receiving **a request to create a webhook for a consumer and provider** from Pact Broker Client, with ```json @@ -1598,6 +1678,50 @@ Pact Broker will respond with: } } ``` + +Given **a pacticipant with name Foo exists**, upon receiving **a request to retrieve a pacticipant** from Pact Broker Client, with +```json +{ + "method": "get", + "path": "/pacticipants/Foo", + "headers": { + "Accept": "application/hal+json" + } +} +``` +Pact Broker will respond with: +```json +{ + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + } +} +``` + +Upon receiving **a request to retrieve a pacticipant** from Pact Broker Client, with +```json +{ + "method": "get", + "path": "/pacticipants/Foo", + "headers": { + "Accept": "application/hal+json" + } +} +``` +Pact Broker will respond with: +```json +{ + "status": 404 +} +``` Given **'Condor' exists in the pact-broker with the latest tagged 'production' version 1.2.3**, upon receiving **a request to retrieve the latest 'production' version of Condor** from Pact Broker Client, with ```json @@ -1809,6 +1933,40 @@ Pact Broker will respond with: } } ``` + +Given **a pacticipant with name Foo exists**, upon receiving **a request to update a pacticipant** from Pact Broker Client, with +```json +{ + "method": "patch", + "path": "/pacticipants/Foo", + "headers": { + "Content-Type": "application/json", + "Accept": "application/hal+json" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo" + } +} +``` +Pact Broker will respond with: +```json +{ + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo", + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + } +} +``` Given **a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists**, upon receiving **a request to update a webhook** from Pact Broker Client, with ```json diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index b68fc7d1..09d35587 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -147,6 +147,18 @@ def generate_uuid puts SecureRandom.uuid end + desc 'create-or-update-pacticipant', 'Create or update pacticipant by name' + method_option :name, type: :string, required: true, desc: "Pacticipant name" + method_option :repository_url, type: :string, required: false, desc: "The repository URL of the pacticipant" + shared_authentication_options_for_pact_broker + verbose_option + def create_or_update_pacticipant(*required_but_ignored) + raise ::Thor::RequiredArgumentMissingError, "Pacticipant name cannot be blank" if options.name.strip.size == 0 + require 'pact_broker/client/pacticipants/create' + result = PactBroker::Client::Pacticipants2::Create.call({ name: options.name, repository_url: options.repository_url }, options.broker_base_url, pact_broker_client_options) + $stdout.puts result.message + end + ignored_and_hidden_potential_options_from_environment_variables desc 'version', "Show the pact_broker-client gem version" def version diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index 3956d12c..75907290 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -112,6 +112,10 @@ def self.shared_authentication_options_for_pact_broker method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password" method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token" end + + def self.verbose_option + method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false" + end end end end diff --git a/lib/pact_broker/client/hal/entity.rb b/lib/pact_broker/client/hal/entity.rb index de21c9c0..2f7dfe87 100644 --- a/lib/pact_broker/client/hal/entity.rb +++ b/lib/pact_broker/client/hal/entity.rb @@ -29,6 +29,10 @@ def put(key, *args) _link(key).put(*args) end + def patch(key, *args) + _link(key).patch(*args) + end + def can?(key) @links.key? key.to_s end @@ -55,6 +59,10 @@ def success? true end + def does_not_exist? + false + end + def response @response end @@ -83,7 +91,6 @@ def assert_success! end class ErrorEntity < Entity - def initialize(href, data, http_client, response = nil) @href = href @data = data @@ -92,6 +99,10 @@ def initialize(href, data, http_client, response = nil) @response = response end + def does_not_exist? + response&.status == 404 + end + def success? false end diff --git a/lib/pact_broker/client/hal/http_client.rb b/lib/pact_broker/client/hal/http_client.rb index c2718f96..3f45896f 100644 --- a/lib/pact_broker/client/hal/http_client.rb +++ b/lib/pact_broker/client/hal/http_client.rb @@ -33,6 +33,11 @@ def post href, body = nil, headers = {} perform_request(create_request(uri, 'Post', body, headers), uri) end + def patch href, body = nil, headers = {} + uri = URI(href) + perform_request(create_request(uri, 'Patch', body, headers), uri) + end + def create_request uri, http_method, body = nil, headers = {} request = Net::HTTP.const_get(http_method).new(uri.request_uri) request['Content-Type'] = "application/json" if ['Post', 'Put', 'Patch'].include?(http_method) diff --git a/lib/pact_broker/client/hal/link.rb b/lib/pact_broker/client/hal/link.rb index ccc39ade..5393ac4c 100644 --- a/lib/pact_broker/client/hal/link.rb +++ b/lib/pact_broker/client/hal/link.rb @@ -53,6 +53,10 @@ def post(payload = nil, headers = {}) wrap_response(href, @http_client.post(href, payload ? JSON.dump(payload) : nil, headers)) end + def patch(payload = nil, headers = {}) + wrap_response(href, @http_client.patch(href, payload ? JSON.dump(payload) : nil, headers)) + end + def expand(params) expanded_url = expand_url(params, href) new_attrs = @attrs.merge('href' => expanded_url) diff --git a/lib/pact_broker/client/hal_client_methods.rb b/lib/pact_broker/client/hal_client_methods.rb new file mode 100644 index 00000000..cb722e18 --- /dev/null +++ b/lib/pact_broker/client/hal_client_methods.rb @@ -0,0 +1,15 @@ +require 'pact_broker/client/hal' + +module PactBroker + module Client + module HalClientMethods + def create_index_entry_point(pact_broker_base_url, pact_broker_client_options) + PactBroker::Client::Hal::EntryPoint.new(pact_broker_base_url, create_http_client(pact_broker_client_options)) + end + + def create_http_client(pact_broker_client_options) + PactBroker::Client::Hal::HttpClient.new(pact_broker_client_options.merge(pact_broker_client_options[:basic_auth] || {})) + end + end + end +end diff --git a/lib/pact_broker/client/pacticipants/create.rb b/lib/pact_broker/client/pacticipants/create.rb new file mode 100644 index 00000000..7bb287f2 --- /dev/null +++ b/lib/pact_broker/client/pacticipants/create.rb @@ -0,0 +1,57 @@ +require 'pact_broker/client/hal' +require 'json' +require 'pact_broker/client/command_result' +require 'pact_broker/client/hal_client_methods' + +module PactBroker + module Client + module Pacticipants2 + class Create + + include HalClientMethods + + def self.call(params, pact_broker_base_url, pact_broker_client_options) + new(params, pact_broker_base_url, pact_broker_client_options).call + end + + def initialize(params, pact_broker_base_url, pact_broker_client_options) + @params = params + @index_entry_point = create_index_entry_point(pact_broker_base_url, pact_broker_client_options) + @verbose = pact_broker_client_options[:verbose] + end + + def call + pacticipant_entity = index_entity._link('pb:pacticipant').expand('pacticipant' => params[:name]).get + message = nil + response_entity = if pacticipant_entity.does_not_exist? + message = "Pacticipant \"#{params[:name]}\" created" + index_entity._link!('pb:pacticipants').post(pacticipant_resource_params) + else + message = "Pacticipant \"#{params[:name]}\" updated" + pacticipant_entity._link!('self').patch(pacticipant_resource_params) + end + + response_entity.assert_success! + PactBroker::Client::CommandResult.new(true, message) + rescue StandardError => e + $stderr.puts("#{e.class} - #{e}\n#{e.backtrace.join("\n")}") if verbose + PactBroker::Client::CommandResult.new(false, "#{e.class} - #{e}") + end + + private + + attr_reader :index_entry_point, :params, :verbose + + def index_entity + @index_entity ||= index_entry_point.get! + end + + def pacticipant_resource_params + p = { name: params[:name] } + p[:repositoryUrl] = params[:repository_url] if params[:repository_url] + p + end + end + end + end +end diff --git a/spec/lib/pact_broker/client/pacticipants/create_spec.rb b/spec/lib/pact_broker/client/pacticipants/create_spec.rb new file mode 100644 index 00000000..4923b9b5 --- /dev/null +++ b/spec/lib/pact_broker/client/pacticipants/create_spec.rb @@ -0,0 +1,28 @@ +require 'pact_broker/client/pacticipants/create' + +module PactBroker + module Client + module Pacticipants2 + describe Create do + describe ".call" do + let(:pact_broker_client_options) { {} } + let(:broker_base_url) { "http://url" } + let(:params) { { name: 'Foo' } } + + subject { Create.call(params, broker_base_url, pact_broker_client_options)} + + context "when there is an http error" do + let!(:index_request) do + stub_request(:get, broker_base_url).to_return(status: 500, body: 'some error', headers: { "Content-Type" => "application/hal+json" } ) + end + + it "returns a failure result" do + expect(subject.success).to be false + expect(subject.message).to include 'some error' + end + end + end + end + end + end +end diff --git a/spec/pacts/pact_broker_client-pact_broker.json b/spec/pacts/pact_broker_client-pact_broker.json index f18620cb..18884e1f 100644 --- a/spec/pacts/pact_broker_client-pact_broker.json +++ b/spec/pacts/pact_broker_client-pact_broker.json @@ -1249,6 +1249,161 @@ } } }, + { + "description": "a request for the index resource", + "providerState": "the pacticipant relations are present", + "request": { + "method": "get", + "path": "/", + "headers": { + "Accept": "application/hal+json" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "_links": { + "pb:pacticipants": { + "href": "http://localhost:1234/pacticipants" + }, + "pb:pacticipant": { + "href": "http://localhost:1234/pacticipants/{pacticipant}" + } + } + }, + "matchingRules": { + "$.body._links.pb:pacticipants.href": { + "match": "regex", + "regex": "http:\\/\\/.*" + }, + "$.body._links.pb:pacticipant.href": { + "match": "regex", + "regex": "http:\\/\\/.*\\{pacticipant\\}" + } + } + } + }, + { + "description": "a request to retrieve a pacticipant", + "request": { + "method": "get", + "path": "/pacticipants/Foo", + "headers": { + "Accept": "application/hal+json" + } + }, + "response": { + "status": 404, + "headers": { + } + } + }, + { + "description": "a request to create a pacticipant", + "request": { + "method": "post", + "path": "/pacticipants", + "headers": { + "Content-Type": "application/json", + "Accept": "application/hal+json" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo" + } + }, + "response": { + "status": 201, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo", + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + }, + "matchingRules": { + "$.body._links.self.href": { + "match": "regex", + "regex": "http:\\/\\/.*" + } + } + } + }, + { + "description": "a request to retrieve a pacticipant", + "providerState": "a pacticipant with name Foo exists", + "request": { + "method": "get", + "path": "/pacticipants/Foo", + "headers": { + "Accept": "application/hal+json" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + }, + "matchingRules": { + "$.body._links.self.href": { + "match": "regex", + "regex": "http:\\/\\/.*" + } + } + } + }, + { + "description": "a request to update a pacticipant", + "providerState": "a pacticipant with name Foo exists", + "request": { + "method": "patch", + "path": "/pacticipants/Foo", + "headers": { + "Content-Type": "application/json", + "Accept": "application/hal+json" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/hal+json;charset=utf-8" + }, + "body": { + "name": "Foo", + "repositoryUrl": "http://foo", + "_links": { + "self": { + "href": "http://localhost:1234/pacticipants/Foo" + } + } + }, + "matchingRules": { + "$.body._links.self.href": { + "match": "regex", + "regex": "http:\\/\\/.*" + } + } + } + }, { "description": "a request to create a webhook with a JSON body for a consumer and provider", "providerState": "the 'Pricing Service' and 'Condor' already exist in the pact-broker", @@ -1550,6 +1705,12 @@ "_links": { "pb:webhooks": { "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-WEBHOOKS" + }, + "pb:pacticipants": { + "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANTS" + }, + "pb:pacticipant": { + "href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PACTICIPANT-{pacticipant}" } } }, @@ -1557,6 +1718,14 @@ "$.body._links.pb:webhooks.href": { "match": "regex", "regex": "http:\\/\\/.*" + }, + "$.body._links.pb:pacticipants.href": { + "match": "regex", + "regex": "http:\\/\\/.*" + }, + "$.body._links.pb:pacticipant.href": { + "match": "regex", + "regex": "http:\\/\\/.*{pacticipant}" } } } diff --git a/spec/service_providers/pact_helper.rb b/spec/service_providers/pact_helper.rb index 6048de5d..2f3ba510 100644 --- a/spec/service_providers/pact_helper.rb +++ b/spec/service_providers/pact_helper.rb @@ -58,12 +58,41 @@ def mock_pact_broker_index(context) _links: { :'pb:webhooks' => { href: placeholder_url_term('pb:webhooks') + }, + :'pb:pacticipants' => { + href: placeholder_url_term('pb:pacticipants') + }, + :'pb:pacticipant' => { + href: placeholder_url_term('pb:pacticipant', ['pacticipant']) } } } ) end + def mock_pact_broker_index_with_relations(context, links, provider_state) + _links = links.each_with_object({}) do | (key, value), new_links | + new_links[key] = { + href: value + } + end + + pact_broker + .given(provider_state) + .upon_receiving("a request for the index resource") + .with( + method: :get, + path: '/', + headers: context.get_request_headers). + will_respond_with( + status: 200, + headers: context.pact_broker_response_headers, + body: { + _links: _links + } + ) + end + def mock_pact_broker_index_with_webhook_relation(context) pact_broker .upon_receiving("a request for the index resource with the webhook relation") diff --git a/spec/service_providers/pacticipants_create_spec.rb b/spec/service_providers/pacticipants_create_spec.rb new file mode 100644 index 00000000..ef7bf013 --- /dev/null +++ b/spec/service_providers/pacticipants_create_spec.rb @@ -0,0 +1,118 @@ +require_relative 'pact_helper' +require 'pact_broker/client/pacticipants/create' + +RSpec.describe "creating or updating a pacticipant", pact: true do + include_context "pact broker" + include PactBrokerPactHelperMethods + + before do + index_links = { + 'pb:pacticipants' => Pact.term('http://localhost:1234/pacticipants', %r{http://.*}), + 'pb:pacticipant' => Pact.term('http://localhost:1234/pacticipants/{pacticipant}', %r{http://.*\{pacticipant\}}), + } + mock_pact_broker_index_with_relations(self, index_links, "the pacticipant relations are present") + end + + let(:params) do + { + name: "Foo", + repository_url: "http://foo" + } + end + + let(:request_body) { { name: "Foo", repositoryUrl: "http://foo" } } + + + let(:response_status) { 201 } + let(:create_success_response) do + { + status: response_status, + headers: pact_broker_response_headers, + body: { + name: "Foo", + repositoryUrl: "http://foo", + _links: { + self: { + href: Pact.term('http://localhost:1234/pacticipants/Foo', %r{http://.*}), + } + } + } + } + end + + let(:get_success_response) do + { + status: 200, + headers: pact_broker_response_headers, + body: { + _links: { + self: { + href: Pact.term('http://localhost:1234/pacticipants/Foo', %r{http://.*}), + } + } + } + } + end + + let(:pact_broker_client_options) { {} } + + subject { PactBroker::Client::Pacticipants2::Create.call(params, broker_base_url, pact_broker_client_options) } + + context "when the pacticipant does not already exist" do + before do + pact_broker + .upon_receiving("a request to retrieve a pacticipant") + .with( + method: :get, + path: '/pacticipants/Foo', + headers: get_request_headers) + .will_respond_with(status: 404) + + pact_broker + .upon_receiving("a request to create a pacticipant") + .with( + method: :post, + path: '/pacticipants', + headers: post_request_headers, + body: request_body) + .will_respond_with(create_success_response) + end + + it "returns a CommandResult with success = true" do + expect(subject).to be_a PactBroker::Client::CommandResult + expect(subject.success).to be true + expect(subject.message).to eq "Pacticipant \"Foo\" created" + end + end + + context "when the pacticipant does already exist" do + before do + pact_broker + .given("a pacticipant with name Foo exists") + .upon_receiving("a request to retrieve a pacticipant") + .with( + method: :get, + path: '/pacticipants/Foo', + headers: get_request_headers) + .will_respond_with(get_success_response) + + pact_broker + .given("a pacticipant with name Foo exists") + .upon_receiving("a request to update a pacticipant") + .with( + method: :patch, + path: '/pacticipants/Foo', + headers: post_request_headers, + body: request_body) + .will_respond_with(create_success_response) + end + + let(:response_status) { 200 } + + it "returns a CommandResult with success = true" do + expect(subject).to be_a PactBroker::Client::CommandResult + expect(subject.success).to be true + expect(subject.message).to eq "Pacticipant \"Foo\" updated" + end + end +end