Skip to content

Commit

Permalink
feat(cli): raise error if both basic auth credentials and bearer toke…
Browse files Browse the repository at this point in the history
…n are set
  • Loading branch information
bethesque committed Feb 17, 2020
1 parent 249bc6a commit 163f56b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
15 changes: 14 additions & 1 deletion lib/pact_broker/client/cli/broker.rb
Expand Up @@ -15,6 +15,7 @@ module CLI
# Thor::Error will have its backtrace hidden
class PactPublicationError < ::Thor::Error; end
class WebhookCreationError < ::Thor::Error; end
class AuthError < ::Thor::Error; end

class Broker < CustomThor
desc 'can-i-deploy', ''
Expand All @@ -36,6 +37,7 @@ class Broker < CustomThor
method_option :limit, hide: true

def can_i_deploy(*ignored_but_necessary)
validate_credentials
selectors = VersionSelectorOptionsParser.call(ARGV)
validate_can_i_deploy_selectors(selectors)
can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval }
Expand All @@ -55,6 +57,7 @@ def can_i_deploy(*ignored_but_necessary)
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"

def publish(*pact_files)
validate_credentials
validate_pact_files(pact_files)
success = publish_pacts(pact_files)
raise PactPublicationError, "One or more pacts failed to be published" unless success
Expand All @@ -74,6 +77,7 @@ def publish(*pact_files)
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"

def create_version_tag
validate_credentials
PactBroker::Client::CreateTag.call(
options.broker_base_url,
options.pacticipant,
Expand All @@ -94,6 +98,7 @@ def create_version_tag

desc 'describe-version', 'Describes a pacticipant version. If no version or tag is specified, the latest version is described.'
def describe_version
validate_credentials
latest = !options.latest.nil? || (options.latest.nil? && options.version.nil?)
params = {
pacticipant: options.pacticipant,
Expand Down Expand Up @@ -144,6 +149,12 @@ def self.exit_on_failure?
true
end

def validate_credentials
if options.broker_username && options.broker_token
raise AuthError, "You cannot provide both a username/password and a bearer token. If your Pact Broker uses a bearer token, please delete the username and password."
end
end

def validate_pact_files pact_files
unless pact_files && pact_files.any?
raise ::Thor::RequiredArgumentMissingError, "No value provided for required pact_files"
Expand Down Expand Up @@ -183,7 +194,8 @@ def tags
end

def pact_broker_client_options
client_options = { verbose: options.verbose, token: options.broker_token }
client_options = { verbose: options.verbose }
client_options[:token] = options.broker_token if options.broker_token
if options.broker_username
client_options[:basic_auth] = {
username: options.broker_username,
Expand Down Expand Up @@ -243,6 +255,7 @@ def parse_webhook_options(webhook_url)

def run_webhook_commands webhook_url
require 'pact_broker/client/webhooks/create'
validate_credentials
result = PactBroker::Client::Webhooks::Create.call(parse_webhook_options(webhook_url), options.broker_base_url, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
Expand Down
16 changes: 13 additions & 3 deletions spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb
Expand Up @@ -20,7 +20,6 @@ module CLI
{
broker_base_url: 'http://pact-broker',
output: 'table',
broker_token: 'token',
verbose: 'verbose',
retry_while_unknown: 1,
retry_interval: 2,
Expand All @@ -36,7 +35,7 @@ module CLI
end

it "invokes the CanIDeploy service" do
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {token: 'token', verbose: 'verbose'})
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
invoke_can_i_deploy
end

Expand Down Expand Up @@ -66,7 +65,18 @@ module CLI
end

it "invokes the CanIDeploy service with the basic auth credentials" do
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {basic_auth: {username: "foo", password: "bar"}, token: 'token', verbose: 'verbose'})
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {basic_auth: {username: "foo", password: "bar"}, verbose: 'verbose'})
invoke_can_i_deploy
end
end

context "with a bearer token" do
before do
subject.options.broker_token = "some token"
end

it "invokes the CanIDeploy service with the basic auth credentials" do
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {token: "some token", verbose: 'verbose'})
invoke_can_i_deploy
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/client/cli/broker_publish_spec.rb
Expand Up @@ -27,7 +27,7 @@ module PactBroker::Client::CLI
["spec/support/cli_test_pacts/foo.json"],
"1.2.3",
[],
{token: nil, verbose: nil}
{verbose: nil}
)
invoke_broker
end
Expand Down
Expand Up @@ -31,7 +31,6 @@ module CLI
broker_base_url: "http://broker",
broker_username: "username",
broker_password: "password",
broker_token: "token",
contract_content_changed: true,
verbose: true
}
Expand Down Expand Up @@ -65,7 +64,7 @@ module CLI
it "calls PactBroker::Client::Webhooks::Create with pact broker details" do
expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, broker_base_url, pact_broker_client_options |
expect(broker_base_url).to eq "http://broker"
expect(pact_broker_client_options).to eq(basic_auth: { username: "username", password: "password"}, token: "token", verbose: true)
expect(pact_broker_client_options).to eq(basic_auth: { username: "username", password: "password"}, verbose: true)
command_result
end
subject
Expand All @@ -91,7 +90,25 @@ module CLI

it "calls Webhooks::Create without basic auth" do
expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, _, pact_broker_client_options |
expect(pact_broker_client_options).to eq(token: "token", verbose: true)
expect(pact_broker_client_options).to eq(verbose: true)
command_result
end
subject
end
end

context "with a bearer token" do
before do
options_hash.delete(:broker_username)
options_hash.delete(:broker_password)
options_hash[:broker_token] = "token"
broker.options = OpenStruct.new(options_hash)
end

it "calls Webhooks::Create without basic auth" do

expect(PactBroker::Client::Webhooks::Create).to receive(:call) do | _, _, pact_broker_client_options |
expect(pact_broker_client_options).to eq(verbose: true, token: "token")
command_result
end
subject
Expand Down

0 comments on commit 163f56b

Please sign in to comment.