Skip to content

Commit

Permalink
feat(create-or-update-webhook): print 'created' or 'updated' based on…
Browse files Browse the repository at this point in the history
… response status
  • Loading branch information
bethesque committed Feb 19, 2020
1 parent 946001b commit 0c34090
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 21 deletions.
66 changes: 66 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@

* [A request to tag the production version of Condor](#a_request_to_tag_the_production_version_of_Condor_given_'Condor'_exists_in_the_pact-broker) given 'Condor' exists in the pact-broker

* [A request to update a webhook](#a_request_to_update_a_webhook_given_a_webhook_with_the_uuid_696c5f93-1b7f-44bc-8d03-59440fcaa9a0_exists) given a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists

* [An invalid request to create a webhook for a consumer and provider](#an_invalid_request_to_create_a_webhook_for_a_consumer_and_provider_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker

#### Interactions
Expand Down Expand Up @@ -766,6 +768,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -864,6 +867,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -917,6 +921,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -967,6 +972,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -1072,6 +1078,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -1120,6 +1127,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -1182,6 +1190,7 @@ Pact Broker will respond with:
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
Expand Down Expand Up @@ -1800,6 +1809,63 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_update_a_webhook_given_a_webhook_with_the_uuid_696c5f93-1b7f-44bc-8d03-59440fcaa9a0_exists"></a>
Given **a webhook with the uuid 696c5f93-1b7f-44bc-8d03-59440fcaa9a0 exists**, upon receiving **a request to update a webhook** from Pact Broker Client, with
```json
{
"method": "put",
"path": "/webhooks/696c5f93-1b7f-44bc-8d03-59440fcaa9a0",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"description": "a webhook",
"events": [
{
"name": "contract_content_changed"
}
],
"request": {
"url": "https://webhook",
"method": "POST",
"headers": {
"Foo": "bar",
"Bar": "foo"
},
"body": {
"some": "body"
},
"username": "username",
"password": "password"
},
"provider": {
"name": "Pricing Service"
},
"consumer": {
"name": "Condor"
}
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"description": "a webhook",
"_links": {
"self": {
"href": "http://localhost:1234/some-url",
"title": "A title"
}
}
}
}
```
<a name="an_invalid_request_to_create_a_webhook_for_a_consumer_and_provider_given_the_&#39;Pricing_Service&#39;_and_&#39;Condor&#39;_already_exist_in_the_pact-broker"></a>
Given **the 'Pricing Service' and 'Condor' already exist in the pact-broker**, upon receiving **an invalid request to create a webhook for a consumer and provider** from Pact Broker Client, with
```json
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ def parse_webhook_options(webhook_url)

{
uuid: options.uuid,
description: options.description,
http_method: options.request,
url: webhook_url,
headers: headers,
Expand Down
4 changes: 1 addition & 3 deletions lib/pact_broker/client/hal/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ module PactBroker
module Client
module Hal
class RelationNotFoundError < ::PactBroker::Client::Error; end

class ErrorResponseReturned < ::PactBroker::Client::Error; end

class Entity

def initialize(href, data, http_client, response = nil)
@href = href
@data = data
Expand Down Expand Up @@ -71,7 +69,7 @@ def method_missing(method_name, *args, &block)
elsif @links.key?(method_name)
Link.new(@links[method_name], @client).run(*args)
else
super
nil
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/pact_broker/client/webhooks/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ def handle_response(webhook_entity)
end

def success_result(webhook_entity)
CommandResult.new(true, "Webhook #{webhook_entity._link('self').title_or_name.inspect} created")
action = webhook_entity.response.status == 201 ? "created" : "updated"
name = if webhook_entity.description && webhook_entity.description.size > 0
webhook_entity.description
else
webhook_entity._link('self').title_or_name
end
CommandResult.new(true, "Webhook #{name.inspect} #{action}")
end

def error_result(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module CLI
let(:options_hash) do
{
uuid: '9999',
description: "some webhook",
request: "POST",
header: header,
data: data,
Expand All @@ -39,6 +40,7 @@ module CLI
let(:expected_params) do
{
uuid: '9999',
description: "some webhook",
http_method: "POST",
url: "http://webhook",
headers: { "Foo" => "bar", "Bar" => "foo"},
Expand Down
1 change: 1 addition & 0 deletions spec/lib/pact_broker/client/hal/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Hal
"name" => "Provider"
}
end

let(:pact_hash) do
{
"name" => "a name",
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/pact_broker/client/webhooks/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ module Webhooks
expect(subject.message).to match /"some":"error"/
end
end

context "when there is an empty description returned" do
let!(:webhook_request) do
stub_request(:post, "http://broker/webhooks").to_return(status: 200, body: response_body.to_json, headers: { "Content-Type" => "application/hal+json" })
end

let(:response_body) do
{
description: "",
_links: {
self: {
href: "href",
title: "the title"
}
}
}
end

it "uses the self title in the message instead" do
expect(subject.message).to include "the title"
end
end
end
end
end
Expand Down
Loading

0 comments on commit 0c34090

Please sign in to comment.