Skip to content

Commit 6ab3fda

Browse files
committed
feat: add configuration option for check_for_potential_duplicate_pacticipant_names
This allows the checking for potential duplicate names (eg. a service being called "Foo" in one pact and "Foo Service" or "foo" or "Foos" in another) to be turned off when publishing a pact. Turning this check off will likely result in a mess of data, and result in the end of the world as we know it. Just for @uglyog
1 parent 48f9853 commit 6ab3fda

File tree

7 files changed

+78
-17
lines changed

7 files changed

+78
-17
lines changed

lib/pact_broker/api/resources/pacticipant_resource_methods.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ module Resources
55
module PacticipantResourceMethods
66

77
def potential_duplicate_pacticipants? pacticipant_names
8-
messages = pacticipant_service.messages_for_potential_duplicate_pacticipants pacticipant_names, base_url
9-
if messages.any?
10-
response.body = messages.join("\n")
11-
response.headers['Content-Type'] = 'text/plain'
8+
if PactBroker.configuration.check_for_potential_duplicate_pacticipant_names
9+
messages = pacticipant_service.messages_for_potential_duplicate_pacticipants pacticipant_names, base_url
10+
if messages.any?
11+
response.body = messages.join("\n")
12+
response.headers['Content-Type'] = 'text/plain'
13+
end
14+
messages.any?
15+
else
16+
false
1217
end
13-
messages.any?
1418
end
15-
1619
end
1720
end
1821
end
19-
end
22+
end

lib/pact_broker/configuration.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ def self.reset_configuration
1111

1212
class Configuration
1313

14-
SAVABLE_SETTING_NAMES = [:order_versions_by_date, :use_case_sensitive_resource_names, :enable_public_badge_access, :shields_io_base_url]
14+
SAVABLE_SETTING_NAMES = [
15+
:order_versions_by_date,
16+
:use_case_sensitive_resource_names,
17+
:enable_public_badge_access,
18+
:shields_io_base_url,
19+
:check_for_potential_duplicate_pacticipant_names
20+
]
1521

1622
attr_accessor :log_dir, :database_connection, :auto_migrate_db, :use_hal_browser, :html_pact_renderer
1723
attr_accessor :validate_database_connection_config, :enable_diagnostic_endpoints, :version_parser
1824
attr_accessor :use_case_sensitive_resource_names, :order_versions_by_date
25+
attr_accessor :check_for_potential_duplicate_pacticipant_names
1926
attr_accessor :semver_formats
2027
attr_accessor :enable_public_badge_access, :shields_io_base_url
2128
attr_accessor :webhook_retry_schedule
@@ -50,6 +57,7 @@ def self.default_configuration
5057
config.order_versions_by_date = false
5158
config.semver_formats = ["%M.%m.%p%s%d","%M.%m", "%M"]
5259
config.webhook_retry_schedule = [10, 60, 120, 300, 600, 1200] #10 sec, 1 min, 2 min, 5 min, 10 min, 20 min => 38 minutes
60+
config.check_for_potential_duplicate_pacticipant_names = true
5361
config
5462
end
5563

lib/pact_broker/locale/en.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ en:
2828
If you meant to specify one of the above names, please correct the pact configuration, and re-publish the pact.
2929
If the pact is intended to be for a new consumer or provider, please manually create "%{new_name}" using the following command, and then re-publish the pact:
3030
$ curl -v -XPOST -H "Content-Type: application/json" -d "{\"name\": \"%{new_name}\"}" %{create_pacticipant_url}
31-
If the pact broker requires authentication, include the '-u' flag with the proper credentials:
32-
$ curl -v -XPOST -u <username>:<password> -H "Content-Type: application/json" -d "{\"name\": \"%{new_name}\"}" %{create_pacticipant_url}
31+
If the pact broker requires basic authentication, add '-u <username:password>' to the command.
32+
To disable this check, set `check_for_potential_duplicate_pacticipant_names` to false in the configuration.
3333
"400":
3434
title: 400 Malformed Request
3535
message: The request was malformed and could not be processed.

lib/pact_broker/pacticipants/find_potential_duplicate_pacticipant_names.rb

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ def self.split(string)
3838
.downcase
3939
.split("_")
4040
end
41-
4241
end
43-
4442
end
4543
end
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'support/test_data_builder'
2+
3+
describe "Creating a pacticipant" do
4+
let(:path) { "/pacticipants" }
5+
let(:headers) { {'CONTENT_TYPE' => 'application/json'} }
6+
let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)}
7+
let(:pacticipant_hash) { {name: 'Foo Thing'}}
8+
9+
subject { post path, pacticipant_hash.to_json, headers; last_response }
10+
11+
it "returns a 201 response" do
12+
subject
13+
expect(last_response.status).to be 201
14+
end
15+
16+
it "returns the Location header" do
17+
subject
18+
expect(last_response.headers['Location']).to eq 'http://example.org/pacticpants/Foo%20Thing'
19+
end
20+
21+
it "returns a JSON Content Type" do
22+
subject
23+
expect(last_response.headers['Content-Type']).to eq 'application/hal+json;charset=utf-8'
24+
end
25+
26+
it "returns the newly created webhook" do
27+
subject
28+
expect(response_body).to include pacticipant_hash
29+
end
30+
end

spec/features/publish_pact_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,30 @@
4646
expect(subject).to be_a_json_error_response "does not match"
4747
end
4848
end
49+
50+
context "when the pacticipant name is an almost duplicate of an existing pacticipant name" do
51+
before do
52+
TestDataBuilder.new.create_pacticipant("A Provider Service")
53+
end
54+
55+
context "when duplicate checking is on" do
56+
before do
57+
PactBroker.configuration.check_for_potential_duplicate_pacticipant_names = true
58+
end
59+
60+
it "returns a 409" do
61+
expect(subject.status).to eq 409
62+
end
63+
end
64+
65+
context "when duplicate checking is off" do
66+
before do
67+
PactBroker.configuration.check_for_potential_duplicate_pacticipant_names = false
68+
end
69+
70+
it "returns a 201" do
71+
expect(subject.status).to eq 201
72+
end
73+
end
74+
end
4975
end

spec/lib/pact_broker/messages_spec.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
module PactBroker
55
module Messages
6-
7-
86
describe "#potential_duplicate_pacticipant_message" do
97
let(:new_name) { 'Contracts' }
108
let(:fred) { double('Contracts Service', name: 'Contracts Service') }
@@ -19,14 +17,12 @@ module Messages
1917
If you meant to specify one of the above names, please correct the pact configuration, and re-publish the pact.
2018
If the pact is intended to be for a new consumer or provider, please manually create "Contracts" using the following command, and then re-publish the pact:
2119
$ curl -v -XPOST -H "Content-Type: application/json" -d "{\\\"name\\\": \\\"Contracts\\\"}" http://example.org/pacticipants
22-
If the pact broker requires authentication, include the '-u' flag with the proper credentials:
23-
$ curl -v -XPOST -u <username>:<password> -H "Content-Type: application/json" -d "{\\\"name\\\": \\\"Contracts\\\"}" http://example.org/pacticipants
2420
EOS
2521
}
2622
subject { Messages.potential_duplicate_pacticipant_message new_name, potential_duplicate_pacticipants, 'http://example.org' }
2723

2824
it "returns a message" do
29-
expect(subject).to eq expected_message
25+
expect(subject).to start_with expected_message
3026
end
3127
end
3228
end

0 commit comments

Comments
 (0)