Skip to content

Commit

Permalink
feat: allow for broker token to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
vixplows committed Feb 22, 2019
1 parent e342c29 commit 3266bc1
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/client/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +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.headers('Authorization' => "Bearer #{client_options[:token]}") if client_options[: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
2 changes: 1 addition & 1 deletion lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def tags
end

def pact_broker_client_options
client_options = { verbose: options.verbose, broker_token: options.broker_token }
client_options = { verbose: options.verbose, token: options.broker_token }
if options.broker_username
client_options[:basic_auth] = {
username: options.broker_username,
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/client/tasks/publication_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module PactBroker
module Client
class PublicationTask < ::Rake::TaskLib

attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth
attr_accessor :pattern, :pact_broker_base_url, :consumer_version, :tag, :write_method, :tag_with_git_branch, :pact_broker_basic_auth, :pact_broker_token
alias_method :tags=, :tag=
alias_method :tags, :tag

Expand All @@ -37,8 +37,10 @@ def rake_task &block
task task_name do
block.call(self)
require 'pact_broker/client/publish_pacts'
basic_auth_client_options = pact_broker_basic_auth ? {basic_auth: pact_broker_basic_auth} : {}
pact_broker_client_options = basic_auth_client_options.merge(write_method ? {write: write_method} : {})
pact_broker_client_options = {}
.merge( pact_broker_basic_auth ? { basic_auth: pact_broker_basic_auth } : {} )
.merge( write_method ? { write: write_method } : {} )
.merge( pact_broker_token ? { token: pact_broker_token } : {} )
success = PactBroker::Client::PublishPacts.new(pact_broker_base_url, FileList[pattern], consumer_version, all_tags, pact_broker_client_options).call
raise "One or more pacts failed to be published" unless success
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/client/base_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Client
username: username,
password: password
},
broker_token: token
token: token
}
end

Expand Down
5 changes: 3 additions & 2 deletions spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module CLI
{
broker_base_url: 'http://pact-broker',
output: 'table',
broker_token: 'token',
verbose: 'verbose',
retry_while_unknown: 1,
retry_interval: 2
Expand All @@ -34,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}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {token: 'token', verbose: 'verbose'})
invoke_can_i_deploy
end

Expand Down Expand Up @@ -64,7 +65,7 @@ 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"}, verbose: 'verbose'})
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, anything, {basic_auth: {username: "foo", password: "bar"}, token: 'token', verbose: 'verbose'})
invoke_can_i_deploy
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/pact_broker/client/cli/broker_create_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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 @@ -62,7 +63,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"}, verbose: true)
expect(pact_broker_client_options).to eq(basic_auth: { username: "username", password: "password"}, token: "token", verbose: true)
command_result
end
subject
Expand All @@ -88,7 +89,7 @@ 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(verbose: true)
expect(pact_broker_client_options).to eq(token: "token", verbose: true)
command_result
end
subject
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/client/cli/broker_publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module PactBroker::Client::CLI
["spec/support/cli_test_pacts/foo.json"],
"1.2.3",
[],
{verbose: nil}
{token: nil, verbose: nil}
)
invoke_broker
end
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/pact_broker/client/cli/custom_thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_multiple_options
method_option :broker_base_url, required: true, aliases: "-b"
method_option :broker_username, aliases: "-u"
method_option :broker_password, aliases: "-p"
method_option :broker_token, aliases: "-k"
def test_using_env_vars
Delegate.call(options)
end
Expand All @@ -42,13 +43,15 @@ def test_using_env_vars
ENV['PACT_BROKER_BASE_URL'] = 'http://foo'
ENV['PACT_BROKER_USERNAME'] = 'username'
ENV['PACT_BROKER_PASSWORD'] = 'password'
ENV['PACT_BROKER_TOKEN'] = 'token'
end

it "populates the options from the environment variables" do
expect(Delegate).to receive(:call) do | options |
expect(options.broker_base_url).to eq 'http://foo'
expect(options.broker_username).to eq 'username'
expect(options.broker_password).to eq 'password'
expect(options.broker_token).to eq 'token'
end
TestThor.start(%w{test_using_env_vars})
end
Expand All @@ -58,6 +61,7 @@ def test_using_env_vars
expect(options.broker_base_url).to eq 'http://bar'
expect(options.broker_username).to eq 'username'
expect(options.broker_password).to eq 'password'
expect(options.broker_token).to eq 'token'
end
TestThor.start(%w{test_using_env_vars --broker-base-url http://bar})
end
Expand Down
8 changes: 5 additions & 3 deletions spec/lib/pact_broker/client/publish_pacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module Client
before do
FakeFS.activate!
allow(pacts_client).to receive(:publish).and_return(latest_pact_url)
allow(PactBroker::Client::PactBrokerClient).to receive(:new)
.with(base_url: pact_broker_base_url, client_options: pact_broker_client_options)
.and_return(pact_broker_client)
allow(PactBroker::Client::PactBrokerClient).to receive(:new).and_return(pact_broker_client)
allow(pact_broker_client).to receive_message_chain(:pacticipants, :versions).and_return(pact_versions_client)
allow(pact_broker_client).to receive_message_chain(:pacticipants, :versions, :pacts).and_return(pacts_client)
allow(pacts_client).to receive(:version_published?).and_return(false)
Expand Down Expand Up @@ -53,6 +51,10 @@ module Client
subject { PublishPacts.new(pact_broker_base_url, pact_file_paths, consumer_version, tags, pact_broker_client_options) }

describe "call" do
it "creates a PactBroker Client" do
expect(PactBroker::Client::PactBrokerClient).to receive(:new).with(base_url: pact_broker_base_url, client_options: pact_broker_client_options)
subject.call
end

it "uses the pact_broker client to publish the given pact" do
expect(pacts_client).to receive(:publish).with(pact_hash: pact_hash, consumer_version: consumer_version)
Expand Down
12 changes: 10 additions & 2 deletions spec/lib/pact_broker/client/tasks/publication_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,29 @@ module PactBroker::Client
before :all do
@pact_broker_base_url = "http://some-host"
@pattern = "pact/*.json"
@pact_broker_basic_auth = { username: 'user', password: 'pass'}
@pact_broker_basic_auth = { username: 'user', password: 'pass' }
@pact_broker_token = 'token'
@tag = "dev"
PactBroker::Client::PublicationTask.new(:custom) do | task |
task.consumer_version = '1.2.3'
task.tag = @tag
task.pact_broker_base_url = @pact_broker_base_url
task.pattern = @pattern
task.pact_broker_basic_auth = @pact_broker_basic_auth
task.pact_broker_token = @pact_broker_token
end
end

let(:pattern) { @pattern }

it "invokes PublishPacts with the customised values" do
expect(PactBroker::Client::PublishPacts).to receive(:new).with(@pact_broker_base_url, pact_file_list, '1.2.3', [@tag], {basic_auth: @pact_broker_basic_auth})
expect(PactBroker::Client::PublishPacts).to receive(:new).with(
@pact_broker_base_url,
pact_file_list,
'1.2.3',
[@tag],
{ basic_auth: @pact_broker_basic_auth, token: @pact_broker_token }
)
expect(publish_pacts).to receive(:call).and_return(true)
Rake::Task['pact:publish:custom'].execute
end
Expand Down

0 comments on commit 3266bc1

Please sign in to comment.