From 1bf38e3cb7a9920c6b12559d8188b25d4d3997f7 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Wed, 13 Oct 2021 15:58:41 +1100 Subject: [PATCH] feat: show more helpful error message if pb:environments not available for pactflow --- .../client/deployments/record_release.rb | 6 +++++- .../client/environments/environment_command.rb | 7 ++++++- .../client/deployments/record_deployment_spec.rb | 13 ++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/pact_broker/client/deployments/record_release.rb b/lib/pact_broker/client/deployments/record_release.rb index 79454988..1552e180 100644 --- a/lib/pact_broker/client/deployments/record_release.rb +++ b/lib/pact_broker/client/deployments/record_release.rb @@ -30,7 +30,11 @@ def action_relation_name end def not_supported_message - "This version of the Pact Broker does not support recording #{action}s. Please upgrade to version 2.80.0 or later." + if is_pactflow? + "This version of Pactflow does not support recording #{action}s, or you do not have the required permission to read environments. Please upgrade to the latest version if using Pactflow On-Premises, and ensure the user has the environment read permission." + else + "This version of the Pact Broker does not support recording #{action}s. Please upgrade to version 2.80.0 or later." + end end def environment_exists? diff --git a/lib/pact_broker/client/environments/environment_command.rb b/lib/pact_broker/client/environments/environment_command.rb index d7a195b4..8fea7d0e 100644 --- a/lib/pact_broker/client/environments/environment_command.rb +++ b/lib/pact_broker/client/environments/environment_command.rb @@ -5,6 +5,7 @@ module Client module Environments class EnvironmentCommand < PactBroker::Client::BaseCommand NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support environments. Please upgrade to version 2.80.0 or later." + PACTFLOW_NOT_SUPPORTED_MESSAGE = "This version of Pactflow does not support environments or you do not have the required permission to read them. Please upgrade to the latest version if using Pactflow On-Premises and ensure the user has the environment read permission." private @@ -57,7 +58,11 @@ def contacts def check_if_command_supported unless index_resource.can?("pb:environments") - raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE) + if is_pactflow? + raise PactBroker::Client::Error.new(PACTFLOW_NOT_SUPPORTED_MESSAGE) + else + raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE) + end end end end diff --git a/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb b/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb index a72545d8..46763cf2 100644 --- a/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb +++ b/spec/lib/pact_broker/client/deployments/record_deployment_spec.rb @@ -44,8 +44,9 @@ module Deployments let(:record_deployment_body_hash) do { "some" => "response" } end + let(:index_headers) { { "Content-Type" => "application/hal+json" } } let!(:index_request) do - stub_request(:get, broker_base_url).to_return(status: 200, body: index_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } ) + stub_request(:get, broker_base_url).to_return(status: 200, body: index_body_hash.to_json, headers: index_headers ) end let!(:version_request) do stub_request(:get, broker_base_url + "/pacticipants/Foo/versions/1").to_return(status: 200, body: version_body_hash.to_json, headers: { "Content-Type" => "application/hal+json" } ) @@ -89,10 +90,20 @@ module Deployments "_links" => {} } end + it "returns an error response" do expect(subject.success).to be false expect(subject.message).to include "does not support" end + + context "when the server is Pactflow" do + let(:index_headers) { { "Content-Type" => "application/hal+json", "Pactflow-Something" => "foo" } } + + it "returns an error response" do + expect(subject.message).to include "permission" + expect(subject.message).to include "does not support" + end + end end context "when the specified version does not exist" do