Skip to content

Commit

Permalink
fix: ensure latest verification is loaded on pact object before execu…
Browse files Browse the repository at this point in the history
…ting webhook

This fixes the bug where the githubVerificationStatus was being calculated as 'pending' when a pact with the same content as a previously verified one was republished.
  • Loading branch information
bethesque committed Feb 1, 2020
1 parent d6987a4 commit 41eb25d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
24 changes: 23 additions & 1 deletion lib/pact_broker/domain/pact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
=end

module PactBroker
class UnsetAttributeError < StandardError; end
class UnsetAttribute; end

module Domain
class Pact

# The ID is the pact_publication ID
attr_accessor :id,
:provider,
Expand All @@ -21,7 +25,8 @@ class Pact
:latest_verification,
:head_tag_names

def initialize attributes
def initialize attributes = {}
@latest_verification = UnsetAttribute.new
attributes.each_pair do | key, value |
self.send(key.to_s + "=", value)
end
Expand All @@ -47,6 +52,10 @@ def latest_consumer_version_tag_names= latest_consumer_version_tag_names
@latest_consumer_version_tag_names = latest_consumer_version_tag_names
end

def latest_verification
get_attribute_if_set :latest_verification
end

def to_s
"Pact: consumer=#{consumer.name} provider=#{provider.name}"
end
Expand Down Expand Up @@ -88,6 +97,19 @@ def pending?
def pact_version
db_model.pact_version
end

# This class has various incarnations with different properties loaded.
# They should probably be different classes, but for now, raise an error if
# an attribute is called when it hasn't been set in the constuctor, because
# returning nil when there should be an object causes bugs.
def get_attribute_if_set attribute_name
val = instance_variable_get("@#{attribute_name}".to_sym)
if val.is_a?(UnsetAttribute)
raise UnsetAttributeError.new("Attribute #{attribute_name} not set")
else
val
end
end
end
end
end
10 changes: 7 additions & 3 deletions lib/pact_broker/pacts/all_pact_publications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def to_domain_without_tags
number: consumer_version_number,
order: consumer_version_order,
pacticipant: consumer,
tags: nil)
Domain::Pact.new(id: id,
tags: nil
)
Domain::Pact.new(
id: id,
consumer: consumer,
consumer_version: consumer_version,
provider: provider,
Expand All @@ -103,7 +105,9 @@ def to_domain_without_tags
pact_version_sha: pact_version_sha,
created_at: created_at,
head_tag_names: head_tag_names,
db_model: self)
latest_verification: pact_version.latest_verification,
db_model: self
)
end

def head_tag_names
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/pacts/pact_publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def to_domain
revision_number: revision_number,
json_content: pact_version.content,
pact_version_sha: pact_version.sha,
latest_verification: nil,
latest_verification: pact_version.latest_verification,
created_at: created_at,
head_tag_names: [],
db_model: self
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/pact_broker/domain/pact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'pact_broker/domain/pact'

module PactBroker
module Domain
describe Pact do
describe "latest_verification" do
context "when it has been set to an object" do
subject { Pact.new(latest_verification: 'verification') }

its(:latest_verification) { is_expected.to eq 'verification' }
end

context "when it has been set to nil" do
subject { Pact.new(latest_verification: nil) }

its(:latest_verification) { is_expected.to eq nil }
end

context "when it has not been set" do
it "raises an error" do
expect { Pact.new.latest_verification.foo }.to raise_error UnsetAttributeError
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions spec/lib/pact_broker/pacts/pact_publication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
module PactBroker
module Pacts
describe PactPublication do
describe "to_domain" do
before do
td.create_pact_with_verification("Foo", "1", "Bar", "2")
end

subject { PactPublication.first.to_domain }

its(:latest_verification) { is_expected.to_not be nil }
end

describe "to_domain_lightweight" do
before do
td.create_pact_with_verification("Foo", "1", "Bar", "2")
end

subject { PactPublication.first.to_domain_lightweight }

it "raises an error if you try to access the latest_verification" do
expect { subject.latest_verification }.to raise_error PactBroker::UnsetAttributeError
end
end

describe "save and upsert" do
before do
td.create_consumer
Expand Down

0 comments on commit 41eb25d

Please sign in to comment.