Skip to content

Commit

Permalink
feat: Filter pacticipant name on index page
Browse files Browse the repository at this point in the history
  • Loading branch information
bangn committed Jun 7, 2021
1 parent 85540c8 commit a14a146
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 19 deletions.
16 changes: 8 additions & 8 deletions lib/pact_broker/hash_refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/pact_broker/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 25 additions & 6 deletions lib/pact_broker/ui/controllers/index.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/pact_broker/ui/views/index/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions public/stylesheets/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 21 additions & 0 deletions spec/lib/pact_broker/index/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 50 additions & 0 deletions spec/lib/pact_broker/ui/controllers/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a14a146

Please sign in to comment.