Skip to content

Commit

Permalink
feat(cli): raise an error when basic auth credentials and bearer toke…
Browse files Browse the repository at this point in the history
…n are set at the same time
  • Loading branch information
bethesque committed Feb 17, 2020
1 parent 087665d commit 7f4507e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/pact/provider_verifier/cli/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module CLI
class Verify < CustomThor

class InvalidArgumentsError < ::Thor::Error; end
class AuthError < ::Thor::Error; end

SELECTOR_DOCS = "Selectors: These are specified using JSON strings. The keys are 'tag' (the name of the consumer version tag) and 'latest' (true|false). " +
"For example '{\"tag\": \"master\", \"latest\": true}'. For a detailed explanation of selectors, see https://pact.io/selectors"
Expand Down Expand Up @@ -49,6 +50,7 @@ class InvalidArgumentsError < ::Thor::Error; end
method_option :wait, banner: "SECONDS", required: false, type: :numeric, desc: "The number of seconds to poll for the provider to become available before running the verification", default: 0

def verify(*pact_urls)
validate_credentials
validate_verify
print_deprecation_warnings
success = Pact::ProviderVerifier::App.call(merged_urls(pact_urls), options)
Expand All @@ -75,6 +77,12 @@ def print_deprecation_warnings
end
end

def validate_credentials
if (options.broker_username || ENV['PACT_BROKER_USERNAME']) && (options.broker_token || ENV['PACT_BROKER_TOKEN'])
raise AuthError, "You cannot provide both a username/password and a bearer token. If your Pact Broker uses a bearer token, please remove the username and password configuration."
end
end

def validate_verify
if options.pact_broker_base_url && (options.provider.nil? || options.provider == "")
raise InvalidArgumentsError, "No value provided for required option '--provider'"
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/pact/provider_verifier/cli/verify_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ module CLI
end
end

context "when both basic auth credentials and bearer token are set" do
before do
subject.options = OpenStruct.new(options)
end

let(:options) do
minimum_valid_options.merge(
broker_username: "username",
broker_token: "token"
)
end

it "raises an error" do
expect { invoke_verify }.to raise_error Verify::AuthError, /both/
end
end

context "when the deprecated pact-urls option is used" do
before do
allow($stderr).to receive(:puts)
Expand Down

0 comments on commit 7f4507e

Please sign in to comment.