From d1501618c7801772172de72ee643f6bfa04f0755 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 13 Oct 2023 11:01:22 +1100 Subject: [PATCH] fix: do not error when no environment is found by name PACT-1402 --- lib/pact_broker/api/resources/environments.rb | 2 +- spec/features/get_environments_spec.rb | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/resources/environments.rb b/lib/pact_broker/api/resources/environments.rb index be74ad0db..1b5605ae0 100644 --- a/lib/pact_broker/api/resources/environments.rb +++ b/lib/pact_broker/api/resources/environments.rb @@ -60,7 +60,7 @@ def uuid def environments @environments ||= if request.query["name"] - [environment_service.find_by_name(request.query["name"])] + [environment_service.find_by_name(request.query["name"])].compact else environment_service.find_all end diff --git a/spec/features/get_environments_spec.rb b/spec/features/get_environments_spec.rb index 4d8b3ebf9..6bbef4534 100644 --- a/spec/features/get_environments_spec.rb +++ b/spec/features/get_environments_spec.rb @@ -7,14 +7,13 @@ end let(:path) { PactBroker::Api::PactBrokerUrls.environments_url } let(:headers) { {"HTTP_ACCEPT" => "application/hal+json"} } - let(:response_body) { JSON.parse(last_response.body, symbolize_names: true)} + let(:response_body) { JSON.parse(subject.body, symbolize_names: true)} subject { get(path, nil, headers) } it { is_expected.to be_a_hal_json_success_response } it "returns the environments" do - subject expect(response_body[:_embedded][:environments].size).to be 2 end @@ -22,9 +21,16 @@ let(:path) { PactBroker::Api::PactBrokerUrls.environments_url + "?name=test" } it "returns the environment with the matching name" do - subject expect(response_body[:_embedded][:environments].size).to be 1 expect(response_body[:_embedded][:environments].first[:name]).to eq "test" end + + context "when no environment exists" do + let(:path) { PactBroker::Api::PactBrokerUrls.environments_url + "?name=foo" } + + it "returns an empty list" do + expect(response_body[:_embedded][:environments].size).to be 0 + end + end end end