Skip to content

Commit

Permalink
feat: add broker token to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
vixplows committed Feb 19, 2019
1 parent 8f2d5a9 commit e342c29
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/client/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def initialize options
self.class.base_uri base_url
self.class.debug_output($stderr) if verbose?
self.class.basic_auth(client_options[:basic_auth][:username], client_options[:basic_auth][:password]) if client_options[:basic_auth]
self.class.headers('Authorization' => "Bearer #{client_options[:broker_token]}") if client_options[:broker_token]
self.class.ssl_ca_file(ENV['SSL_CERT_FILE']) if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
self.class.ssl_ca_path(ENV['SSL_CERT_DIR']) if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
end
Expand Down
19 changes: 10 additions & 9 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Broker < CustomThor
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :output, aliases: "-o", desc: "json or table", default: 'table'
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
method_option :retry_while_unknown, banner: 'TIMES', type: :numeric, default: 0, required: false, desc: "The number of times to retry while there is an unknown verification result (ie. the provider verification is likely still running)"
Expand All @@ -46,6 +47,7 @@ def can_i_deploy(*ignored_but_necessary)
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for consumer version. Can be specified multiple times."
method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false"
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
Expand All @@ -66,6 +68,7 @@ def publish(*pact_files)
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"

def create_version_tag
Expand All @@ -83,6 +86,7 @@ def create_version_tag
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :output, aliases: "-o", desc: "json or table or id", default: 'table'
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"

Expand Down Expand Up @@ -112,6 +116,7 @@ def describe_version
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token"
method_option :contract_content_changed, type: :boolean, desc: "Trigger this webhook when the pact content changes"
method_option :provider_verification_published, type: :boolean, desc: "Trigger this webhook when a provider verification result is published"
method_option :verbose, aliases: "-v", type: :boolean, default: false, required: false, desc: "Verbose output. Default: false"
Expand Down Expand Up @@ -215,19 +220,15 @@ def tags
end

def pact_broker_client_options
client_options = { verbose: options.verbose, broker_token: options.broker_token }
if options.broker_username
{
basic_auth: {
client_options[:basic_auth] = {
username: options.broker_username,
password: options.broker_password
},
verbose: options.verbose
}
else
{
verbose: options.verbose
}
}
end

client_options
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/client/cli/custom_thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def self.add_broker_config_from_environment_variables argv

new_argv = add_option_from_environment_variable(argv, 'broker-base-url', 'b', 'PACT_BROKER_BASE_URL')
new_argv = add_option_from_environment_variable(new_argv, 'broker-username', 'u', 'PACT_BROKER_USERNAME')
new_argv = add_option_from_environment_variable(new_argv, 'broker-token', 'k', 'PACT_BROKER_TOKEN')
add_option_from_environment_variable(new_argv, 'broker-password', 'p', 'PACT_BROKER_PASSWORD')
end

Expand Down
29 changes: 23 additions & 6 deletions spec/lib/pact_broker/client/base_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ module Client
let(:base_url) { 'http://pact_broker_base_url'}
let(:username) { 'pact_repo_username'}
let(:password) { 'pact_repo_password'}
let(:token) { '123456789' }
let(:client_options) do
{
{
basic_auth: {
username: username,
password: password
}
},
broker_token: token
}
end

context 'with basic url' do
it 'sets the base url' do
base_client = BaseClient.new(base_url: base_url)
Expand All @@ -26,13 +29,22 @@ module Client

context 'with client options' do
subject { BaseClient.new(base_url: base_url, client_options: client_options) }
it 'sets the client options ' do
it 'sets the client options' do
expect(subject.client_options).to eq(client_options)
end

it 'sets the httpparty basic auth when client options contains basic auth' do
expect(BaseClient).to receive(:basic_auth).with(username, password)
subject
context 'when client options contains basic auth' do
it 'sets the httpparty basic auth' do
expect(BaseClient).to receive(:basic_auth).with(username, password)
subject
end
end

context 'when client options contains broker token' do
it 'sets the httparty broker token' do
expect(BaseClient).to receive(:headers).with('Authorization' => "Bearer #{token}")
subject
end
end
end

Expand All @@ -46,6 +58,11 @@ module Client
expect(BaseClient).to_not receive(:basic_auth).with(username, password)
subject
end

it 'does not set the httparty broker token' do
expect(BaseClient).to_not receive(:headers).with('Authorization' => "Bearer #{token}")
subject
end
end

context 'when the SSL_CERT_FILE environment variable is set' do
Expand Down

0 comments on commit e342c29

Please sign in to comment.