-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ids to interactions when pacts are published
These will be used to match test results published back to the broker with the correct interaction.
- Loading branch information
Showing
8 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'digest/sha1' | ||
require 'pact_broker/configuration' | ||
require 'pact_broker/pacts/sort_content' | ||
require 'pact_broker/pacts/parse' | ||
require 'pact_broker/pacts/content' | ||
|
||
module PactBroker | ||
module Pacts | ||
module GenerateInteractionSha | ||
def self.call interaction_hash, options = {} | ||
ordered_interaction_hash = interaction_hash.keys.sort.each_with_object({}) do | key, new_interaction_hash | | ||
new_interaction_hash[key] = interaction_hash[key] unless key == "id" | ||
end | ||
|
||
Digest::SHA1.hexdigest(ordered_interaction_hash.to_json) | ||
end | ||
|
||
def generate_interaction_sha interaction_hash, options = {} | ||
GenerateInteractionSha.call(interaction_hash, options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
spec/lib/pact_broker/pacts/generate_interaction_sha_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'pact_broker/pacts/generate_interaction_sha' | ||
|
||
module PactBroker | ||
module Pacts | ||
describe GenerateInteractionSha do | ||
describe ".call" do | ||
let(:interaction_hash) do | ||
{ | ||
"description" => "foo", | ||
"providerStates" => [ | ||
"name" => "bar", | ||
"params" => { | ||
"wiffle" => "bar", | ||
"meep" => "eek" | ||
} | ||
] | ||
} | ||
end | ||
|
||
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" | ||
} | ||
] | ||
} | ||
end | ||
|
||
it "generates a SHA based on the sorted keys" do | ||
expect(GenerateInteractionSha.call(interaction_hash)).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521" | ||
end | ||
|
||
it "generates the same SHA if the top level keys are ordered differently" do | ||
expect(GenerateInteractionSha.call(interaction_hash)).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(GenerateInteractionSha.call(interaction_hash)).to_not eq GenerateInteractionSha.call(interaction_hash_with_different_params_order) | ||
end | ||
|
||
it "ignores any existing id in the hash" do | ||
interaction_hash["id"] = "foo" | ||
expect(GenerateInteractionSha.call(interaction_hash)).to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters