diff --git a/lib/pact_broker/hash_refinements.rb b/lib/pact_broker/hash_refinements.rb index 06afa1ac3..9c5ef4d70 100644 --- a/lib/pact_broker/hash_refinements.rb +++ b/lib/pact_broker/hash_refinements.rb @@ -45,10 +45,10 @@ def snakecase_keys_private(params) when Hash params.inject({}) do |result, (key, value)| snake_key = case key - when String then key.snakecase - when Symbol then key.to_s.snakecase.to_sym - else - key + when String then key.snakecase + when Symbol then key.to_s.snakecase.to_sym + else + key end result.merge(snake_key => snakecase_keys_private(value)) end @@ -64,10 +64,10 @@ def camelcase_keys_private(params) when Hash params.inject({}) do |result, (key, value)| snake_key = case key - when String then key.camelcase - when Symbol then key.to_s.camelcase.to_sym - else - key + when String then key.camelcase + when Symbol then key.to_s.camelcase.to_sym + else + key end result.merge(snake_key => camelcase_keys_private(value)) end diff --git a/lib/pact_broker/index/service.rb b/lib/pact_broker/index/service.rb index 2b829574e..e2b29b4eb 100644 --- a/lib/pact_broker/index/service.rb +++ b/lib/pact_broker/index/service.rb @@ -156,11 +156,11 @@ def self.head_pact_publications(options = {}) latest = base.overall_latest ids_query = if options[:tags].is_a?(Array) - latest.union(base.latest_for_consumer_tag(options[:tags])) - elsif options[:tags] - latest.union(base.latest_by_consumer_tag) - else - latest + latest.union(base.latest_for_consumer_tag(options[:tags])) + elsif options[:tags] + latest.union(base.latest_by_consumer_tag) + else + latest end query = PactBroker::Pacts::PactPublication.select_all_qualified.where(Sequel[:pact_publications][:id] => ids_query) @@ -194,6 +194,14 @@ def self.latest_verifications_for_consumer_version_tags(options) nil # should not be used end end + + def self.validate_pacticipant(pacticipant_type, pacticipant_name) + error_messages = [] + + pacticipant = pacticipant_service.find_pacticipant_by_name(pacticipant_name) + + error_messages << "#{pacticipant_type} #{pacticipant_name} cannot be found" unless pacticipant + end end end end diff --git a/lib/pact_broker/locale/en.yml b/lib/pact_broker/locale/en.yml index e17caf922..667665ae7 100644 --- a/lib/pact_broker/locale/en.yml +++ b/lib/pact_broker/locale/en.yml @@ -17,6 +17,8 @@ en: pact_broker: messages: response_body_hidden: For security purposes, the response details are not logged. To enable response logging, configure the webhook_host_whitelist property. See %{base_url}/doc/webhooks#whitelist for more information. + index: + title: Pacts matrix: pre_verified: This pact was "pre-verified" as it has identical content to a previously verified pact. webhooks: diff --git a/lib/pact_broker/ui/controllers/index.rb b/lib/pact_broker/ui/controllers/index.rb index 945c674dc..1fda14ee1 100644 --- a/lib/pact_broker/ui/controllers/index.rb +++ b/lib/pact_broker/ui/controllers/index.rb @@ -1,5 +1,6 @@ require 'pact_broker/ui/controllers/base_controller' require 'pact_broker/ui/view_models/index_items' +require 'pact_broker/messages' require 'haml' module PactBroker @@ -10,10 +11,9 @@ class Index < Base get "/" do set_headers - tags = nil - if params[:tags] - tags = params[:tags] == 'true' ? true : [*params[:tags]].compact - end + tags = if params[:tags] + params[:tags] == 'true' ? true : [*params[:tags]].compact + end page_number = params[:page]&.to_i || 1 # Make page size smaller for data intensive query page_size = params[:pageSize]&.to_i || (tags == true ? 30 : 100) @@ -23,27 +23,46 @@ class Index < Base page_size: page_size } + error_messages = pacticipants.reduce([]) do |agg, (pacticipant_type, pacticipant_name)| + agg << index_service.validate_pacticipant(pacticipant_type, pacticipant_name) + end.flatten + + options.merge!(pacticipants) if error_messages.blank? + index_items = ViewDomain::IndexItems.new(index_service.find_index_items(options), base_url: base_url) page = tags ? :'index/show-with-tags' : :'index/show' locals = { - title: "Pacts", + title: PactBroker::Messages.message('messages.index.title'), index_items: index_items, page_number: page_number, page_size: page_size, pagination_record_count: index_items.pagination_record_count, current_page_size: index_items.size, - base_url: base_url + base_url: base_url, + errors: error_messages } haml page, { locals: locals, layout: :'layouts/main', escape_html: true } end + private + def set_headers response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" response.headers["Pragma"] = "no-cache" response.headers["Expires"] = "0" end + + def pacticipants + consumer_name = params[:consumer_name].blank? ? nil : params[:consumer_name] + provider_name = params[:provider_name].blank? ? nil : params[:provider_name] + + @pacticipants ||= { + consumer_name: consumer_name, + provider_name: provider_name, + }.compact + end end end end diff --git a/lib/pact_broker/ui/views/index/show.haml b/lib/pact_broker/ui/views/index/show.haml index 85aefe339..fca40ec36 100644 --- a/lib/pact_broker/ui/views/index/show.haml +++ b/lib/pact_broker/ui/views/index/show.haml @@ -6,6 +6,24 @@ != render :haml, :'index/_getting-started', :layout => false %h1.page-header Pacts + + - unless errors.blank? + - errors.each do | error | + %div.alert.alert-danger + = error + + %form{action: "#{base_url}", onsubmit:'return onSubmit()'} + .field + %label{for: "consumer_name"} + Consumer name + %input{name: 'consumer_name', id: "consumer_name", class: 'pacticipant_name'} + .field + %label{for: "provider_name"} + Provider name + %input{name: 'provider_name', id: "provider_name", class: 'pacticipant_name'} + %div.top-of-group + %input{type: 'submit'} + %table.table.table-bordered.table-striped{ id: 'relationships' } %thead %tr diff --git a/public/stylesheets/index.css b/public/stylesheets/index.css index 6adf1dd57..bcc0e670b 100644 --- a/public/stylesheets/index.css +++ b/public/stylesheets/index.css @@ -232,3 +232,17 @@ span.copy-success-icon { width: 16px; height: 16px; } + +.field label { + width: 10em; + margin-top: 15px; +} + +.field .pacticipant_name { + width: 20em; +} + +div.top-of-group { + margin-top: 20px; + margin-bottom: 20px; +} diff --git a/spec/lib/pact_broker/index/service_spec.rb b/spec/lib/pact_broker/index/service_spec.rb index c9b53f0f3..f0fc01b23 100644 --- a/spec/lib/pact_broker/index/service_spec.rb +++ b/spec/lib/pact_broker/index/service_spec.rb @@ -388,6 +388,27 @@ module Index end end end + + describe ".validate_pacticipant" do + before { td.create_pact_with_hierarchy } + + context "when validating non existing pacticipant" do + it "returns an error" do + non_existing_pacticipant_name = "no name" + non_existing_pacticipant_type = :pacticipant_type + error_messages = subject.validate_pacticipant(non_existing_pacticipant_type, non_existing_pacticipant_name) + expect(error_messages.count).to eq(1) + expect(error_messages.first).to eq("pacticipant_type no name cannot be found") + end + end + + context "when validating existing pacticipant" do + it "returns no error" do + error_messages = subject.validate_pacticipant(:consumer_name, "Consumer") + expect(error_messages).to be_nil + end + end + end end end end diff --git a/spec/lib/pact_broker/ui/controllers/index_spec.rb b/spec/lib/pact_broker/ui/controllers/index_spec.rb index 52f3232b6..dbe1f58a4 100644 --- a/spec/lib/pact_broker/ui/controllers/index_spec.rb +++ b/spec/lib/pact_broker/ui/controllers/index_spec.rb @@ -85,6 +85,56 @@ module Controllers get "/", { tags: "prod" } end end + + context "when parameter consumer_name presents" do + context "when it is blank" do + it "ignores it" do + expect(PactBroker::Index::Service).not_to receive(:find_index_items).with(hash_including(:consumer_name)) + get "/", { consumer_name: "" } + end + end + + context "when it is NOT blank and the pacticipant name exist" do + it "passes consumer_name to the service" do + allow(PactBroker::Index::Service).to receive(:validate_pacticipant).and_return([]) + expect(PactBroker::Index::Service).to receive(:find_index_items).with(hash_including(consumer_name: "Consumer")) + get "/", { consumer_name: "Consumer" } + end + end + + context "when it is NOT blank but the pacticipant name does NOT exist" do + it "passes consumer_name to the service" do + allow(PactBroker::Index::Service).to receive(:validate_pacticipant).and_return(["pacticipant does not exist"]) + expect(PactBroker::Index::Service).not_to receive(:find_index_items).with(hash_including(:consumer_name)) + get "/", { consumer_name: "Consumer" } + end + end + end + + context "when parameter provider_name presents" do + context "when it is blank" do + it "ignores it" do + expect(PactBroker::Index::Service).not_to receive(:find_index_items).with(hash_including(:provider_name)) + get "/", { provider_name: "" } + end + end + + context "when it is NOT blank and the pacticipant exists" do + it "passes provider_name to the service" do + allow(PactBroker::Index::Service).to receive(:validate_pacticipant).and_return([]) + expect(PactBroker::Index::Service).to receive(:find_index_items).with(hash_including(provider_name: "Provider")) + get "/", { provider_name: "Provider" } + end + end + + context "when it is NOT blank but the pacticipant name does NOT exist" do + it "passes provider_name to the service" do + allow(PactBroker::Index::Service).to receive(:validate_pacticipant).and_return(["pacticipant does not exist"]) + expect(PactBroker::Index::Service).not_to receive(:find_index_items).with(hash_including(:provider_name)) + get "/", { provider_name: "Provider" } + end + end + end end end end