-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhooks): allow testing of an unsaved webhook
- Loading branch information
Showing
10 changed files
with
141 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,17 @@ | ||
module PactBroker | ||
module Api | ||
|
||
module Resources | ||
|
||
module WebhookResourceMethods | ||
|
||
def malformed_webhook_request? webhook | ||
begin | ||
if (errors = webhook.validate).any? | ||
set_json_validation_error_messages errors | ||
return true | ||
end | ||
rescue | ||
set_json_error_message 'Invalid JSON' | ||
return true | ||
def webhook_validation_errors? webhook | ||
errors = webhook_service.errors(webhook) | ||
if !errors.empty? | ||
set_json_validation_error_messages(errors.messages) | ||
true | ||
else | ||
false | ||
end | ||
false | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'support/test_data_builder' | ||
require 'webmock/rspec' | ||
require 'rack/pact_broker/database_transaction' | ||
|
||
describe "Execute a webhook" do | ||
|
||
let(:td) { TestDataBuilder.new } | ||
|
||
before do | ||
td.create_pact_with_hierarchy("Foo", "1", "Bar") | ||
allow(PactBroker.configuration).to receive(:webhook_scheme_whitelist).and_return(%w[http]) | ||
end | ||
|
||
let(:params) do | ||
{ | ||
request: { | ||
method: 'POST', | ||
url: 'http://example.org', | ||
headers: {'Content-Type' => 'application/json'}, | ||
body: '${pactbroker.pactUrl}' | ||
} | ||
} | ||
end | ||
let(:rack_headers) { { "CONTENT_TYPE" => "application/json", "HTTP_ACCEPT" => "application/hal+json" } } | ||
|
||
let(:path) { "/webhooks/execute" } | ||
let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} | ||
|
||
subject { post(path, params.to_json, rack_headers) } | ||
|
||
context "when the execution is successful" do | ||
let!(:request) do | ||
stub_request(:post, /http/).with(body: expected_webhook_url).to_return(:status => 200, body: response_body) | ||
end | ||
|
||
let(:expected_webhook_url) { %r{http://example.org/pacts/provider/Bar/consumer/Foo.*} } | ||
let(:response_body) { "webhook-response-body" } | ||
|
||
it "performs the HTTP request" do | ||
subject | ||
expect(request).to have_been_made | ||
end | ||
|
||
it "returns a 200 response" do | ||
expect(subject.status).to be 200 | ||
end | ||
end | ||
|
||
context "when there is a validation error" do | ||
let(:params) { {} } | ||
|
||
it "returns a 400" do | ||
expect(subject.status).to be 400 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters