diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 53a6888bc..7f1438c07 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -1,6 +1,7 @@ require 'sequel' require 'pact_broker/domain/pacticipant' require 'pact_broker/repositories/helpers' +require 'pact_broker/error' module PactBroker module Pacticipants @@ -9,7 +10,13 @@ class Repository include PactBroker::Repositories::Helpers def find_by_name name - PactBroker::Domain::Pacticipant.where(name_like(:name, name)).single_record + if PactBroker.configuration.use_case_sensitive_resource_names + PactBroker::Domain::Pacticipant.where(name: name).single_record + else + pacticipants = PactBroker::Domain::Pacticipant.where(name_like(:name, name)).all + handle_multiple_pacticipants_found(name, pacticipants) if pacticipants.size > 1 + pacticipants.first + end end def find_by_id id @@ -66,6 +73,11 @@ def delete_if_orphan(pacticipant) pacticipant.destroy end end + + def handle_multiple_pacticipants_found(name, pacticipants) + names = pacticipants.collect(&:name).join(", ") + raise PactBroker::Error.new("Found multiple pacticipants with a case insensitive name match for '#{name}': #{names}. Please delete one of them, or set PactBroker.configuration.use_case_sensitive_resource_names = true") + end end end end diff --git a/spec/lib/pact_broker/pacticipants/repository_spec.rb b/spec/lib/pact_broker/pacticipants/repository_spec.rb index 563ac8d8a..3d5596d56 100644 --- a/spec/lib/pact_broker/pacticipants/repository_spec.rb +++ b/spec/lib/pact_broker/pacticipants/repository_spec.rb @@ -60,7 +60,7 @@ module Pacticipants end describe "#find_by_name" do before do - TestDataBuilder.new.create_pacticipant("Foo Bar") + td.create_pacticipant("Foo Bar") end subject { Repository.new.find_by_name('foo bar') } @@ -86,6 +86,23 @@ module Pacticipants expect(subject.name).to eq "Foo Bar" end end + + context "with case sensitivity turned off and multiple records found" do + before do + td.create_pacticipant("Foo bar") + allow(PactBroker.configuration).to receive(:use_case_sensitive_resource_names).and_return(false) + end + + it "raises an error" do + expect { subject }.to raise_error PactBroker::Error, /Found multiple pacticipants.*foo bar/ + end + end + + context "with case sensitivity turned off no record found" do + subject { Repository.new.find_by_name('blah') } + + it { is_expected.to be nil } + end end end