From 76236d815d0f64388d02a66c623025f130176207 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Fri, 17 Jan 2020 13:14:14 +1100 Subject: [PATCH] feat: add token, username and password options to stub service (#118) Co-authored-by: Beth Skurrie --- lib/pact/mock_service/app.rb | 17 ++++++++++++++++- lib/pact/mock_service/run.rb | 3 +++ lib/pact/stub_service/cli.rb | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/pact/mock_service/app.rb b/lib/pact/mock_service/app.rb index 6dd1e56..de91fb4 100644 --- a/lib/pact/mock_service/app.rb +++ b/lib/pact/mock_service/app.rb @@ -17,6 +17,7 @@ def self.new *args class App def initialize options = {} logger = Logger.from_options(options) + @options = options stubbing = options[:stub_pactfile_paths] && options[:stub_pactfile_paths].any? @name = options.fetch(:name, "MockService") @session = Session.new(options.merge(logger: logger, warn_on_too_many_interactions: !stubbing)) @@ -40,7 +41,7 @@ def shutdown def setup_stub stub_pactfile_paths interactions = stub_pactfile_paths.collect do | pactfile_path | $stdout.puts "INFO: Loading interactions from #{pactfile_path}" - hash_interactions = JSON.parse(Pact::PactFile.read(pactfile_path))['interactions'] + hash_interactions = JSON.parse(Pact::PactFile.read(pactfile_path, pactfile_options))['interactions'] hash_interactions.collect { | hash | Interaction.from_hash(hash) } end.flatten @session.set_expected_interactions interactions @@ -57,6 +58,20 @@ def write_pact_if_configured def to_s "#{@name} #{super.to_s}" end + + private + + def pactfile_options + { + :token => broker_token, + :username => @options[:broker_username], + :password => @options[:broker_password], + } + end + + def broker_token + @options[:broker_token] || ENV['PACT_BROKER_TOKEN'] + end end # Can't write to a file in a TRAP, might deadlock diff --git a/lib/pact/mock_service/run.rb b/lib/pact/mock_service/run.rb index b021845..02f3e60 100644 --- a/lib/pact/mock_service/run.rb +++ b/lib/pact/mock_service/run.rb @@ -58,6 +58,9 @@ def service_options unique_pact_file_names: options[:unique_pact_file_names], consumer: options[:consumer], provider: options[:provider], + broker_token: options[:broker_token], + broker_username: options[:broker_username], + broker_password: options[:broker_password], cors_enabled: options[:cors], pact_specification_version: options[:pact_specification_version] || Pact::SpecificationVersion::NIL_VERSION.to_s, pactfile_write_mode: options[:pact_file_write_mode], diff --git a/lib/pact/stub_service/cli.rb b/lib/pact/stub_service/cli.rb index 59a9356..c166744 100755 --- a/lib/pact/stub_service/cli.rb +++ b/lib/pact/stub_service/cli.rb @@ -27,6 +27,9 @@ class CLI < Pact::MockService::CLI::CustomThor method_option :port, aliases: "-p", desc: "Port on which to run the service" method_option :host, aliases: "-h", desc: "Host on which to bind the service", default: 'localhost' method_option :log, aliases: "-l", desc: "File to which to log output" + method_option :broker_username, aliases: "-n", desc: "Pact Broker basic auth username", :required => false + method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password", :required => false + method_option :broker_token, aliases: "-k", desc: "Pact Broker bearer token", :required => false method_option :log_level, desc: "Log level. Options are DEBUG INFO WARN ERROR", default: "DEBUG" method_option :cors, aliases: "-o", desc: "Support browser security in tests by responding to OPTIONS requests and adding CORS headers to mocked responses" method_option :ssl, desc: "Use a self-signed SSL cert to run the service over HTTPS", type: :boolean, default: false