From e3913f7b01c3bef25fbf04c7d820ef26e89280c7 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 27 Oct 2017 14:30:27 +1100 Subject: [PATCH] =?UTF-8?q?feat(matrix):=20change=20query=20params=20for?= =?UTF-8?q?=20matrix=20to=20use=20q[][pacticipant]=3D=3F=20and=20q[][versi?= =?UTF-8?q?on]=3D=3F?= --- lib/pact_broker/matrix/parse_query.rb | 8 +++--- lib/pact_broker/matrix/service.rb | 16 +++++++++++- spec/features/get_matrix_spec.rb | 6 +++-- .../pact_broker/api/resources/matrix_spec.rb | 2 +- .../pact_broker/matrix/parse_query_spec.rb | 20 +++++++++++++-- spec/lib/pact_broker/matrix/service_spec.rb | 25 ++++++++++++++++++- 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/lib/pact_broker/matrix/parse_query.rb b/lib/pact_broker/matrix/parse_query.rb index 8992f92a1..5d7ad97b2 100644 --- a/lib/pact_broker/matrix/parse_query.rb +++ b/lib/pact_broker/matrix/parse_query.rb @@ -1,12 +1,12 @@ -require 'cgi' +require 'rack/utils' module PactBroker module Matrix class ParseQuery def self.call query - params = CGI.parse(CGI.unescape(query)) - params['pacticipant[]'].zip(params['version[]']).each_with_object({}) do | (pacticipant, version), hash | - hash[pacticipant] = version + params = Rack::Utils.parse_nested_query(query) + (params['q'] || []).each_with_object({}) do | selector, hash | + hash[selector['pacticipant']] = selector['version'] end end end diff --git a/lib/pact_broker/matrix/service.rb b/lib/pact_broker/matrix/service.rb index 9d4364ec6..ffef7a77c 100644 --- a/lib/pact_broker/matrix/service.rb +++ b/lib/pact_broker/matrix/service.rb @@ -23,7 +23,21 @@ def find_compatible_pacticipant_versions criteria def validate_selectors selectors error_messages = [] - selectors.keys.each do | pacticipant_name | + selectors.each do | pacticipant_name, version | + if pacticipant_name.nil? && version.nil? + error_messages << "Please specify the pacticipant name and version" + elsif pacticipant_name.nil? + error_messages << "Please specify the pacticipant name" + elsif version.nil? + error_messages << "Please specify the version for #{pacticipant_name}" + end + end + + if selectors.values.any?(&:nil?) + error_messages << "Please specify the pacticipant version" + end + + selectors.keys.compact.each do | pacticipant_name | unless pacticipant_service.find_pacticipant_by_name(pacticipant_name) error_messages << "Pacticipant '#{pacticipant_name}' not found" end diff --git a/spec/features/get_matrix_spec.rb b/spec/features/get_matrix_spec.rb index 8d430aa72..2a6bfe11a 100644 --- a/spec/features/get_matrix_spec.rb +++ b/spec/features/get_matrix_spec.rb @@ -10,8 +10,10 @@ let(:path) { "/matrix" } let(:params) do { - pacticipant: ['Consumer', 'Provider'], - version: ['1.0.0', '4.5.6'] + q: [ + { pacticipant: 'Consumer', version: '1.0.0' }, + { pacticipant: 'Provider', version: '4.5.6' } + ] } end let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) } diff --git a/spec/lib/pact_broker/api/resources/matrix_spec.rb b/spec/lib/pact_broker/api/resources/matrix_spec.rb index 13cb35e49..14b6f80a2 100644 --- a/spec/lib/pact_broker/api/resources/matrix_spec.rb +++ b/spec/lib/pact_broker/api/resources/matrix_spec.rb @@ -12,7 +12,7 @@ module Resources let(:td) { TestDataBuilder.new } let(:path) { "/matrix" } let(:json_response_body) { JSON.parse(subject.body, symbolize_names: true) } - let(:params) { { pacticipant: ['Foo', 'Bar'], version: ['1', '2'] } } + let(:params) { {q: [{pacticipant: 'Foo', version: '1'}, {pacticipant: 'Bar', version: '2'}]} } let(:error_messages) { [] } subject { get path, params, {'Content-Type' => 'application/hal+json'}; last_response } diff --git a/spec/lib/pact_broker/matrix/parse_query_spec.rb b/spec/lib/pact_broker/matrix/parse_query_spec.rb index a5039aafd..bf75e7a56 100644 --- a/spec/lib/pact_broker/matrix/parse_query_spec.rb +++ b/spec/lib/pact_broker/matrix/parse_query_spec.rb @@ -4,7 +4,7 @@ module PactBroker module Matrix describe ParseQuery do describe ".call" do - let(:query) { "pacticipant[]=Foo&pacticipant[]=Bar&version[]=1.2.3&version[]=9.9.9" } + let(:query) { "q[][pacticipant]=Foo&q[][version]=1.2.3&q[][pacticipant]=Bar&q[][version]=9.9.9" } subject { ParseQuery.call(query) } @@ -13,12 +13,28 @@ module Matrix end context "with spaces" do - let(:query) { "pacticipant%5B%5D=Name%20With%20Spaces&version%5B%5D=1%202" } + let(:query) { "q[][pacticipant]=Name%20With%20Spaces&q[][version]=1%202" } it "works" do expect(subject).to eq "Name With Spaces" => "1 2" end end + + context "with no q" do + let(:query) { "foo" } + + it "returns an empty hash" do + expect(subject).to eq({}) + end + end + + context "with an incorrect param names" do + let(:query) { "q[][wrong]=Foo&q[][blah]=1.2.3" } + + it "returns nil keys or values" do + expect(subject).to eq nil => nil + end + end end end end diff --git a/spec/lib/pact_broker/matrix/service_spec.rb b/spec/lib/pact_broker/matrix/service_spec.rb index 5cf997361..b613aa838 100644 --- a/spec/lib/pact_broker/matrix/service_spec.rb +++ b/spec/lib/pact_broker/matrix/service_spec.rb @@ -46,8 +46,31 @@ module Matrix expect(subject.first).to eq "Pacticipant 'Foo' not found" end end + + context "when the pacticipant name is not specified" do + let(:selectors) { {nil => "1"} } + + it "returns error messages" do + expect(subject.first).to eq "Please specify the pacticipant name" + end + end + + context "when the pacticipant version is not specified" do + let(:selectors) { {'Foo' => nil} } + + it "returns error messages" do + expect(subject.first).to eq "Please specify the version for Foo" + end + end + + context "when the pacticipant name and version are not specified" do + let(:selectors) { {nil => nil} } + + it "returns error messages" do + expect(subject.first).to eq "Please specify the pacticipant name and version" + end + end end end end end -