diff --git a/lib/pact_broker/pacts/generate_interaction_sha.rb b/lib/pact_broker/pacts/generate_interaction_sha.rb index e916b5865..b5a55920b 100644 --- a/lib/pact_broker/pacts/generate_interaction_sha.rb +++ b/lib/pact_broker/pacts/generate_interaction_sha.rb @@ -1,18 +1,13 @@ require 'digest/sha1' -require 'pact_broker/configuration' -require 'pact_broker/pacts/sort_content' -require 'pact_broker/pacts/parse' -require 'pact_broker/pacts/content' +require 'pact_broker/pacts/order_object' module PactBroker module Pacts module GenerateInteractionSha - def self.call interaction_hash - ordered_interaction_hash = interaction_hash.keys.sort.each_with_object({}) do | key, new_interaction_hash | - new_interaction_hash[key] = interaction_hash[key] - end + extend OrderObject - Digest::SHA1.hexdigest(ordered_interaction_hash.to_json) + def self.call interaction_hash + Digest::SHA1.hexdigest(order_object(interaction_hash).to_json) end def generate_interaction_sha(interaction_hash) diff --git a/lib/pact_broker/pacts/order_object.rb b/lib/pact_broker/pacts/order_object.rb index 0b51c9ec9..d199e5ff8 100644 --- a/lib/pact_broker/pacts/order_object.rb +++ b/lib/pact_broker/pacts/order_object.rb @@ -2,7 +2,7 @@ module PactBroker module Pacts - class OrderObject + module OrderObject def self.call thing case thing when Hash then order_hash(thing) @@ -20,6 +20,10 @@ def self.order_hash hash new_hash[key] = call(hash[key]) end end + + def order_object(thing) + OrderObject.call(thing) + end end end end diff --git a/spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb b/spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb index 9b269229a..00801860f 100644 --- a/spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb +++ b/spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb @@ -19,47 +19,26 @@ module Pacts let(:interaction_hash_with_different_key_order) do { - "providerStates" => [ - "name" => "bar", - "params" => { - "wiffle" => "bar", - "meep" => "eek" - } - ], - "description" => "foo" - } - end - - let(:interaction_hash_with_different_params_order) do - { - "description" => "foo", "providerStates" => [ "name" => "bar", "params" => { "meep" => "eek", "wiffle" => "bar" } - ] + ], + "description" => "foo" } end subject { GenerateInteractionSha.call(interaction_hash) } it "generates a SHA based on the sorted keys" do - expect(subject).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521" + expect(subject).to eq "57d06e151eca35083e4d6b585b4d4fab2e2ed6b7" end - it "generates the same SHA if the top level keys are ordered differently" do + it "generates the same SHA if the keys are ordered differently" do expect(subject).to eq GenerateInteractionSha.call(interaction_hash_with_different_key_order) end - - # This could be a whole lot smarter, but I'm not sure it's worth it. - # eg. order of provider state params doesn't matter, but the ordering - # of the provider states themselves may... who knows. - # Let's not try and be too smart about it until we have a use case to flesh it out. - it "generates a different SHA if any of the other keys are ordered differently" do - expect(subject).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order) - end end end end