Skip to content

Commit

Permalink
chore: pass pact in to webhook when executing
Browse files Browse the repository at this point in the history
Preparing for implementing templating
#130
  • Loading branch information
bethesque committed Nov 7, 2017
1 parent b257868 commit 8424e14
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/pact_broker/domain/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def request_description
request && request.description
end

def execute options
def execute pact, options
logger.info "Executing #{self}"
request.execute options
request.execute pact, options
end

def to_s
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/domain/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def display_password
password.nil? ? nil : "**********"
end

def execute options = {}
def execute pact, options = {}
logs = StringIO.new
execution_logger = Logger.new(logs)
begin
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/triggered_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def request_description
end

def execute options
webhook.to_domain.execute options
webhook.to_domain.execute pact_publication.to_domain, options
end

def consumer_name
Expand Down
31 changes: 16 additions & 15 deletions spec/lib/pact_broker/domain/webhook_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Domain
let(:logs) { StringIO.new }
let(:execution_logger) { Logger.new(logs) }
let(:options) { {failure_log_message: 'oops'}}
let(:pact) { instance_double('PactBroker::Domain::Pact') }

subject do
WebhookRequest.new(
Expand All @@ -26,7 +27,7 @@ module Domain
body: body)
end

let(:logs) { subject.execute(options).logs }
let(:logs) { subject.execute(pact, options).logs }

describe "description" do
it "returns a brief description of the HTTP request" do
Expand Down Expand Up @@ -57,22 +58,22 @@ module Domain
end

it "executes the configured request" do
subject.execute(options)
subject.execute(pact, options)
expect(http_request).to have_been_made
end

it "logs the request" do
allow(PactBroker.logger).to receive(:info)
expect(PactBroker.logger).to receive(:info).with(/POST.*example.*text.*body/)
subject.execute(options)
subject.execute(pact, options)
end

it "logs the response" do
allow(PactBroker.logger).to receive(:info)
allow(PactBroker.logger).to receive(:debug)
expect(PactBroker.logger).to receive(:info).with(/response.*200/)
expect(PactBroker.logger).to receive(:debug).with(/respbod/)
subject.execute(options)
subject.execute(pact, options)
end

describe "execution logs" do
Expand Down Expand Up @@ -144,7 +145,7 @@ module Domain
end

it "uses the credentials" do
subject.execute(options)
subject.execute(pact, options)
expect(http_request_with_basic_auth).to have_been_made
end
end
Expand All @@ -160,7 +161,7 @@ module Domain
end

it "uses SSL" do
subject.execute(options)
subject.execute(pact, options)
expect(https_request).to have_been_made
end
end
Expand All @@ -175,7 +176,7 @@ module Domain
end

it "converts the body to JSON before submitting the request" do
subject.execute(options)
subject.execute(pact, options)
expect(http_request).to have_been_made
end
end
Expand All @@ -190,18 +191,18 @@ module Domain
end

it "executes the request without a body" do
subject.execute(options)
subject.execute(pact, options)
expect(http_request).to have_been_made
end
end

context "when the request is successful" do
it "returns a WebhookExecutionResult with success=true" do
expect(subject.execute(options).success?).to be true
expect(subject.execute(pact, options).success?).to be true
end

it "sets the response on the result" do
expect(subject.execute(options).response).to be_instance_of(Net::HTTPOK)
expect(subject.execute(pact, options).response).to be_instance_of(Net::HTTPOK)
end
end

Expand All @@ -214,11 +215,11 @@ module Domain
end

it "returns a WebhookExecutionResult with success=false" do
expect(subject.execute(options).success?).to be false
expect(subject.execute(pact, options).success?).to be false
end

it "sets the response on the result" do
expect(subject.execute(options).response).to be_instance_of(Net::HTTPInternalServerError)
expect(subject.execute(pact, options).response).to be_instance_of(Net::HTTPInternalServerError)
end
end

Expand All @@ -233,15 +234,15 @@ class WebhookTestError < StandardError; end
it "logs the error" do
allow(PactBroker.logger).to receive(:error)
expect(PactBroker.logger).to receive(:error).with(/Error.*WebhookTestError.*blah/)
subject.execute(options)
subject.execute(pact, options)
end

it "returns a WebhookExecutionResult with success=false" do
expect(subject.execute(options).success?).to be false
expect(subject.execute(pact, options).success?).to be false
end

it "returns a WebhookExecutionResult with an error" do
expect(subject.execute(options).error).to be_instance_of WebhookTestError
expect(subject.execute(pact, options).error).to be_instance_of WebhookTestError
end

it "logs the failure_log_message" do
Expand Down
8 changes: 5 additions & 3 deletions spec/lib/pact_broker/domain/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Domain
let(:provider) { Pacticipant.new(name: 'Provider')}
let(:request) { instance_double(PactBroker::Domain::WebhookRequest, execute: nil)}
let(:options) { double('options') }
let(:pact) { double('pact') }

subject { Webhook.new(request: request, consumer: consumer, provider: provider,) }

describe "description" do
Expand All @@ -22,14 +24,14 @@ module Domain
describe "execute" do

it "executes the request" do
expect(request).to receive(:execute).with(options)
subject.execute options
expect(request).to receive(:execute).with(pact, options)
subject.execute pact, options
end

it "logs before and after" do
allow(PactBroker.logger).to receive(:info)
expect(PactBroker.logger).to receive(:info).with(/Executing/)
subject.execute options
subject.execute pact, options
end
end
end
Expand Down

0 comments on commit 8424e14

Please sign in to comment.