Skip to content

Commit

Permalink
feat: add token, username and password options to stub service (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: Beth Skurrie <bethesque@users.noreply.github.com>
  • Loading branch information
mefellows and bethesque committed Jan 17, 2020
1 parent d7d6e35 commit 76236d8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/pact/mock_service/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/pact/mock_service/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 3 additions & 0 deletions lib/pact/stub_service/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 76236d8

Please sign in to comment.