From e342c299d5eb377371c70945913c1038a3037c19 Mon Sep 17 00:00:00 2001 From: Victoria Plows Date: Tue, 19 Feb 2019 15:32:31 +1100 Subject: [PATCH] feat: add broker token to cli --- lib/pact_broker/client/base_client.rb | 1 + lib/pact_broker/client/cli/broker.rb | 19 ++++++------ lib/pact_broker/client/cli/custom_thor.rb | 1 + .../pact_broker/client/base_client_spec.rb | 29 +++++++++++++++---- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/pact_broker/client/base_client.rb b/lib/pact_broker/client/base_client.rb index e5011b07..4ae99222 100644 --- a/lib/pact_broker/client/base_client.rb +++ b/lib/pact_broker/client/base_client.rb @@ -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 diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 2dad40c5..4927da9e 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -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)" @@ -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" @@ -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 @@ -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" @@ -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" @@ -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 diff --git a/lib/pact_broker/client/cli/custom_thor.rb b/lib/pact_broker/client/cli/custom_thor.rb index 33c389a0..543c2c9d 100644 --- a/lib/pact_broker/client/cli/custom_thor.rb +++ b/lib/pact_broker/client/cli/custom_thor.rb @@ -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 diff --git a/spec/lib/pact_broker/client/base_client_spec.rb b/spec/lib/pact_broker/client/base_client_spec.rb index 6f93619b..bd23af2e 100644 --- a/spec/lib/pact_broker/client/base_client_spec.rb +++ b/spec/lib/pact_broker/client/base_client_spec.rb @@ -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) @@ -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 @@ -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