Skip to content

Commit

Permalink
feat: add resource to get latest verification for a pact
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 26, 2019
1 parent 6725f3d commit f02a1ca
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module PactBroker
# Verifications
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results'], Api::Resources::Verifications, {resource_name: "verification_results"}
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'metadata', :metadata, 'verification-results'], Api::Resources::Verifications, {resource_name: "verification_results"}
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'version', :consumer_version_number, 'verification-results', 'latest'], Api::Resources::LatestVerificationForPact, {resource_name: "latest_verification_results_for_pact_publication"}
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', 'latest'], Api::Resources::LatestVerificationForPact, {resource_name: "latest_verification_results_for_pact_version"}
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', :verification_number], Api::Resources::Verification, {resource_name: "verification_result"}
add ['pacts', 'provider', :provider_name, 'consumer', :consumer_name, 'pact-version', :pact_version_sha, 'verification-results', :verification_number, 'triggered-webhooks'], Api::Resources::VerificationTriggeredWebhooks, {resource_name: "verification_result_triggered_webhooks"}
add ['verification-results', 'consumer', :consumer_name, 'version', :consumer_version_number,'latest'], Api::Resources::LatestVerificationsForConsumerVersion, {resource_name: "verification_results_for_consumer_version"}
Expand Down
16 changes: 16 additions & 0 deletions lib/pact_broker/api/decorators/extended_verification_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'pact_broker/api/decorators/verification_decorator'

module PactBroker
module Api
module Decorators
class ExtendedVerificationDecorator < VerificationDecorator
class TagDecorator < BaseDecorator
property :name
property :latest?, as: :latest
end

collection :provider_version_tags, as: :tags, embedded: true, extend: TagDecorator
end
end
end
end
6 changes: 6 additions & 0 deletions lib/pact_broker/api/decorators/pact_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def to_hash(options = {})
}
end

link :'pb:latest-verification-results' do | options |
{
href: latest_verification_for_pact_url(represented, options.fetch(:base_url))
}
end

link :'pb:triggered-webhooks' do | options |
{
title: "Webhooks triggered by the publication of this pact",
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/api/decorators/verification_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class TagDecorator < BaseDecorator
property :execution_date, as: :verificationDate
property :build_url, as: :buildUrl
property :test_results, as: :testResults
collection :provider_version_tags, as: :tags, embedded: true, extend: TagDecorator

link :self do | options |
{
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def latest_verifications_for_consumer_version_url version, base_url
"#{base_url}/verification-results/consumer/#{url_encode(version.pacticipant.name)}/version/#{version.number}/latest"
end

def latest_verification_for_pact_url pact, base_url
verification_url_from_params(
provider_name: pact.provider_name,
consumer_name: pact.consumer_name,
pact_version_sha: pact.pact_version_sha,
verification_number: 'latest')
end

def verification_triggered_webhooks_url verification, base_url = ''
"#{verification_url(verification, base_url)}/triggered-webhooks"
end
Expand Down
19 changes: 19 additions & 0 deletions lib/pact_broker/api/resources/latest_verification_for_pact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'pact_broker/api/resources/verification'

module PactBroker
module Api
module Resources
class LatestVerificationForPact < Verification
private

def pact
@pact ||= pact_service.find_pact(pact_params)
end

def verification
@verification ||= pact && verification_service.find_latest_for_pact(pact)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
module PactBroker
module Api
module Resources

class LatestVerificationsForConsumerVersion < BaseResource

def allowed_methods
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def content_types_provided
[["application/hal+json", :to_json],
["application/json", :to_json],
["text/html", :to_html],
["application/vnd.pactbroker.v1+json", :to_extended_json]
["application/vnd.pactbrokerextended.v1+json", :to_extended_json]
]
end

Expand Down
15 changes: 14 additions & 1 deletion lib/pact_broker/api/resources/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
require 'pact_broker/domain/verification'
require 'pact_broker/api/contracts/verification_contract'
require 'pact_broker/api/decorators/verification_decorator'
require 'pact_broker/api/decorators/extended_verification_decorator'

module PactBroker
module Api
module Resources
class Verification < BaseResource
def content_types_provided
[["application/hal+json", :to_json], ["application/json", :to_json]]
[
["application/hal+json", :to_json],
["application/json", :to_json],
["application/vnd.pactbrokerextended.v1+json", :to_extended_json]
]
end

# Remember to update latest_verification_id_for_pact_version_and_provider_version
Expand All @@ -26,6 +31,10 @@ def to_json
decorator_for(verification).to_json(user_options: {base_url: base_url})
end

def to_extended_json
extended_decorator_for(verification).to_json(user_options: {base_url: base_url})
end

private

def verification
Expand All @@ -35,6 +44,10 @@ def verification
def decorator_for model
PactBroker::Api::Decorators::VerificationDecorator.new(model)
end

def extended_decorator_for model
PactBroker::Api::Decorators::ExtendedVerificationDecorator.new(model)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def find consumer_name, provider_name, pact_version_sha, verification_number
.verification_number(verification_number).single_record
end

def find_latest_for_pact(pact)
PactBroker::Pacts::PactPublication.where(id: pact.id).single_record.latest_verification
end

def search_for_latest consumer_name, provider_name
query = LatestVerificationForPactVersion
.select_all_qualified
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def find params
verification_repository.find(params.fetch(:consumer_name), params.fetch(:provider_name), params.fetch(:pact_version_sha), params.fetch(:verification_number))
end

def find_latest_for_pact(pact)
verification_repository.find_latest_for_pact(pact)
end

def find_by_id id
PactBroker::Domain::Verification.find(id: id)
end
Expand Down
42 changes: 42 additions & 0 deletions spec/features/get_latest_verification_for_pact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec/support/test_data_builder'

describe "Get latest verification for pact" do
before do
td
.create_consumer("Consumer")
.create_consumer_version("1.2.3")
.create_provider("Another provider")
.create_pact
.create_verification(number: 1, provider_version: "5")
.create_provider("Provider")
.create_pact
.create_verification(number: 1, provider_version: "3")
.create_verification(number: 2, provider_version: "4")
end

let(:last_response_body) { JSON.parse(subject.body, symbolize_names: true) }
let(:content_type) { "application/vnd.pactbrokerextended.v1+json" }

subject { get(path, nil, "HTTP_ACCEPT" => content_type) }

context "by pact version" do
let!(:path) { "/pacts/provider/Provider/consumer/Consumer/pact-version/#{td.pact.pact_version_sha}/verification-results/latest" }

it "returns a 200 OK" do
expect(subject.status).to eq 200
expect(subject.headers['Content-Type']).to include content_type
end

it "returns the verification" do
expect(last_response_body[:providerApplicationVersion]).to eq "4"
end
end

context "by consumer version" do
let(:path) { "/pacts/provider/Provider/consumer/Consumer/version/1.2.3/verification-results/latest" }

it "returns the verification" do
expect(last_response_body[:providerApplicationVersion]).to eq "4"
end
end
end

0 comments on commit f02a1ca

Please sign in to comment.