Skip to content

Commit

Permalink
assert_equal_hash performs assert_equal, but improves diffs on fail
Browse files Browse the repository at this point in the history
Convert all multi-line hash comparisons to assert_equal_hash
  • Loading branch information
martinemde committed Dec 3, 2024
1 parent fb1b507 commit 3c23a18
Show file tree
Hide file tree
Showing 20 changed files with 239 additions and 213 deletions.
53 changes: 27 additions & 26 deletions test/integration/api/v1/oidc/api_key_roles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ class Api::V1::OIDC::ApiKeyRolesTest < ActionDispatch::IntegrationTest
headers: { "HTTP_AUTHORIZATION" => @user_api_key }

assert_response :success
assert_equal(
{
"id" => @role.id,
assert_equal_hash(
{ "id" => @role.id,
"token" => @role.token,
"oidc_provider_id" => @role.oidc_provider_id,
"user_id" => @user.id,
Expand All @@ -75,8 +74,8 @@ class Api::V1::OIDC::ApiKeyRolesTest < ActionDispatch::IntegrationTest
] },
"created_at" => @role.created_at.as_json,
"updated_at" => @role.updated_at.as_json,
"deleted_at" => nil
}, response.parsed_body
"deleted_at" => nil },
response.parsed_body
)
end
end
Expand Down Expand Up @@ -252,12 +251,13 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
"rubygems_api_key" => resp["rubygems_api_key"],
assert_equal_hash(
{ "rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
"expires_at" => 30.minutes.from_now
}, resp)
"expires_at" => 30.minutes.from_now },
resp
)
hashed_key = @user.api_keys.sole.hashed_key

assert_equal hashed_key, Digest::SHA256.hexdigest(resp["rubygems_api_key"])
Expand All @@ -281,13 +281,14 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
"rubygems_api_key" => resp["rubygems_api_key"],
assert_equal_hash(
{ "rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
"expires_at" => 30.minutes.from_now,
"gem" => Rubygem.find_by!(name: gem_name).as_json
}, resp)
"gem" => Rubygem.find_by!(name: gem_name).as_json },
resp
)
hashed_key = @user.api_keys.sole.hashed_key

assert_equal hashed_key, Digest::SHA256.hexdigest(resp["rubygems_api_key"])
Expand Down Expand Up @@ -344,12 +345,13 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
"rubygems_api_key" => resp["rubygems_api_key"],
assert_equal_hash(
{ "rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
"expires_at" => 30.minutes.from_now
}, resp)
"expires_at" => 30.minutes.from_now },
resp
)
hashed_key = @user.api_keys.sole.hashed_key

assert_equal hashed_key, Digest::SHA256.hexdigest(resp["rubygems_api_key"])
Expand All @@ -358,16 +360,14 @@ def jwt(claims = @claims, key: @pkey)

assert_equal hashed_key, oidc_id_token.api_key.hashed_key
assert_equal @role.provider, oidc_id_token.provider
assert_equal(
{
"claims" => @claims,
assert_equal_hash(
{ "claims" => @claims,
"header" => {
"alg" => "RS256",
"kid" => @pkey.to_jwk[:kid],
"typ" => "JWT"
}
},
oidc_id_token.jwt
} },
oidc_id_token.jwt
)

post assume_role_api_v1_oidc_api_key_role_path(@role.token),
Expand All @@ -377,9 +377,10 @@ def jwt(claims = @claims, key: @pkey)
headers: {}

assert_response :unprocessable_content
assert_equal({
"errors" => { "jwt.claims.jti" => ["must be unique"] }
}, response.parsed_body)
assert_equal_hash(
{ "errors" => { "jwt.claims.jti" => ["must be unique"] } },
response.parsed_body
)
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions test/integration/api/v1/oidc/id_tokens_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ class Api::V1::OIDC::IdTokensTest < ActionDispatch::IntegrationTest
headers: { "HTTP_AUTHORIZATION" => @user_api_key }

assert_response :success
assert_equal(
{
"api_key_role_token" => @id_token.api_key_role.token,
assert_equal_hash(
{ "api_key_role_token" => @id_token.api_key_role.token,
"jwt" => {
"claims" => @id_token.jwt["claims"],
"header" => @id_token.jwt["header"]
}
}, response.parsed_body
} },
response.parsed_body
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Api::V1::OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::In
repository_name = @trusted_publisher.trusted_publisher.repository_name

assert_response :success
assert_equal(
assert_equal_hash(
{ "id" => @trusted_publisher.id,
"trusted_publisher_type" => "OIDC::TrustedPublisher::GitHubAction",
"trusted_publisher" => {
Expand All @@ -117,7 +117,8 @@ class Api::V1::OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::In
"repository_owner_id" => "123456",
"workflow_filename" => "push_gem.yml",
"environment" => nil
} }, @response.parsed_body
} },
@response.parsed_body
)
end
end
Expand All @@ -142,7 +143,7 @@ class Api::V1::OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::In
trusted_publisher = OIDC::RubygemTrustedPublisher.find(response.parsed_body["id"])

assert_equal @rubygem, trusted_publisher.rubygem
assert_equal(
assert_equal_hash(
{ "id" => response.parsed_body["id"],
"trusted_publisher_type" => "OIDC::TrustedPublisher::GitHubAction",
"trusted_publisher" => {
Expand All @@ -152,7 +153,8 @@ class Api::V1::OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::In
"repository_owner_id" => "123456",
"workflow_filename" => "push_gem.yml",
"environment" => nil
} }, response.parsed_body
} },
response.parsed_body
)
end

Expand Down Expand Up @@ -180,9 +182,11 @@ class Api::V1::OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::In
headers: { "HTTP_AUTHORIZATION" => "12345" }

assert_response :unprocessable_content
assert_equal({ "trusted_publisher.repository_name" => ["can't be blank"],
"trusted_publisher.workflow_filename" => ["can't be blank"] },
response.parsed_body["errors"])
assert_equal_hash(
{ "trusted_publisher.repository_name" => ["can't be blank"],
"trusted_publisher.workflow_filename" => ["can't be blank"] },
response.parsed_body["errors"]
)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
"rubygems_api_key" => resp["rubygems_api_key"],
"name" => "GitHub Actions segiddins/oidc-test @ .github/workflows/token.yml 2023-03-28T16:22:17Z",
"scopes" => ["push_rubygem"],
"expires_at" => 15.minutes.from_now
}, resp)
assert_equal_hash(
{ "rubygems_api_key" => resp["rubygems_api_key"],
"name" => "GitHub Actions segiddins/oidc-test @ .github/workflows/token.yml 2023-03-28T16:22:17Z",
"scopes" => ["push_rubygem"],
"expires_at" => 15.minutes.from_now },
resp
)

api_key = trusted_publisher.api_keys.sole

Expand Down
9 changes: 4 additions & 5 deletions test/jobs/upload_info_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class UploadInfoFileJobTest < ActiveJob::TestCase

assert_equal content, RubygemFs.compact_index.get("info/#{version.rubygem.name}")

assert_equal(
{
metadata: {
assert_equal_hash(
{ metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
"surrogate-key" =>
"info/* info/#{version.rubygem.name} gem/#{version.rubygem.name} s3-compact-index s3-info/* s3-info/#{version.rubygem.name}",
Expand All @@ -30,8 +29,8 @@ class UploadInfoFileJobTest < ActiveJob::TestCase
content_type: "text/plain; charset=utf-8",
checksum_sha256: Digest::SHA256.base64digest(content),
content_md5: Digest::MD5.base64digest(content),
key: "info/#{version.rubygem.name}"
}, RubygemFs.compact_index.head("info/#{version.rubygem.name}")
key: "info/#{version.rubygem.name}" },
RubygemFs.compact_index.head("info/#{version.rubygem.name}")
)

assert_enqueued_with(job: FastlyPurgeJob, args: [{ key: "s3-info/#{version.rubygem.name}", soft: true }])
Expand Down
9 changes: 4 additions & 5 deletions test/jobs/upload_names_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class UploadNamesFileJobTest < ActiveJob::TestCase

assert_equal content, RubygemFs.compact_index.get("names")

assert_equal(
{
metadata: {
assert_equal_hash(
{ metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
"surrogate-key" =>
"names s3-compact-index s3-names",
Expand All @@ -30,8 +29,8 @@ class UploadNamesFileJobTest < ActiveJob::TestCase
content_type: "text/plain; charset=utf-8",
checksum_sha256: Digest::SHA256.base64digest(content),
content_md5: Digest::MD5.base64digest(content),
key: "names"
}, RubygemFs.compact_index.head("names")
key: "names" },
RubygemFs.compact_index.head("names")
)

assert_enqueued_with(job: FastlyPurgeJob, args: [{ key: "s3-names", soft: true }])
Expand Down
9 changes: 4 additions & 5 deletions test/jobs/upload_versions_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class UploadVersionsFileJobTest < ActiveJob::TestCase

assert_equal content, RubygemFs.compact_index.get("versions")

assert_equal(
{
metadata: {
assert_equal_hash(
{ metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
"surrogate-key" => "versions s3-compact-index s3-versions",
"sha256" => Digest::SHA256.base64digest(content),
Expand All @@ -30,8 +29,8 @@ class UploadVersionsFileJobTest < ActiveJob::TestCase
content_type: "text/plain; charset=utf-8",
checksum_sha256: Digest::SHA256.base64digest(content),
content_md5: Digest::MD5.base64digest(content),
key: "versions"
}, RubygemFs.compact_index.head("versions")
key: "versions" },
RubygemFs.compact_index.head("versions")
)

assert_enqueued_with(job: FastlyPurgeJob, args: [{ key: "s3-versions", soft: true }])
Expand Down
16 changes: 6 additions & 10 deletions test/models/oidc/trusted_publisher/github_action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ class OIDC::TrustedPublisher::GitHubActionTest < ActiveSupport::TestCase
test "#to_access_policy" do
publisher = create(:oidc_trusted_publisher_github_action, repository_name: "rubygem1")

assert_equal(
assert_equal_hash(
{
statements: [
{
effect: "allow",
{ effect: "allow",
principal: {
oidc: "https://token.actions.githubusercontent.com"
},
Expand All @@ -92,10 +91,8 @@ class OIDC::TrustedPublisher::GitHubActionTest < ActiveSupport::TestCase
{ operator: "string_equals", claim: "repository_owner_id", value: "123456" },
{ operator: "string_equals", claim: "aud", value: Gemcutter::HOST },
{ operator: "string_equals", claim: "job_workflow_ref", value: "example/rubygem1/.github/workflows/push_gem.yml@ref" }
]
},
{
effect: "allow",
] },
{ effect: "allow",
principal: {
oidc: "https://token.actions.githubusercontent.com"
},
Expand All @@ -104,16 +101,15 @@ class OIDC::TrustedPublisher::GitHubActionTest < ActiveSupport::TestCase
{ operator: "string_equals", claim: "repository_owner_id", value: "123456" },
{ operator: "string_equals", claim: "aud", value: Gemcutter::HOST },
{ operator: "string_equals", claim: "job_workflow_ref", value: "example/rubygem1/.github/workflows/push_gem.yml@sha" }
]
}
] }
]
}.deep_stringify_keys,
publisher.to_access_policy({ ref: "ref", sha: "sha" }).as_json
)

publisher.update!(environment: "test")

assert_equal(
assert_equal_hash(
{
statements: [
{
Expand Down
8 changes: 3 additions & 5 deletions test/models/sendgrid_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ class SendgridEventTest < ActiveSupport::TestCase
assert_equal("bounce", event.event_type)
assert_equal(occurred_at, event.occurred_at)
assert_predicate event, :pending?
assert_equal(
{
"email" => "[email protected]",
assert_equal_hash(
{ "email" => "[email protected]",
"sg_event_id" => "t61hI0Xpmk8XSR1YX4s0Kg==",
"event" => "bounce",
"timestamp" => occurred_at.to_i
},
"timestamp" => occurred_at.to_i },
event.payload
)
end
Expand Down
18 changes: 8 additions & 10 deletions test/models/web_hook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@ def stub_hook_relay_request(url, webhook_id, authorization, max_attempts = 3)
end

should "show limited attributes for to_json" do
assert_equal(
{
"url" => @url,
"failure_count" => @webhook.failure_count
}, JSON.load(@webhook.to_json)
assert_equal_hash(
{ "url" => @url,
"failure_count" => @webhook.failure_count },
JSON.load(@webhook.to_json)
)
end

Expand All @@ -115,11 +114,10 @@ def stub_hook_relay_request(url, webhook_id, authorization, max_attempts = 3)
end

should "show limited attributes for to_yaml" do
assert_equal(
{
"url" => @url,
"failure_count" => @webhook.failure_count
}, YAML.safe_load(@webhook.to_yaml)
assert_equal_hash(
{ "url" => @url,
"failure_count" => @webhook.failure_count },
YAML.safe_load(@webhook.to_yaml)
)
end

Expand Down
6 changes: 3 additions & 3 deletions test/system/avo/manual_changes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Avo::ManualChangesSystemTest < ApplicationSystemTestCase
assert_equal log_ticket, audit.auditable
assert_equal "LogTicket", audit.auditable_type
assert_equal "Manual create of LogTicket", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/LogTicket/#{log_ticket.id}" => {
Expand Down Expand Up @@ -80,7 +80,7 @@ class Avo::ManualChangesSystemTest < ApplicationSystemTestCase
assert_equal log_ticket, audit.auditable
assert_equal "LogTicket", audit.auditable_type
assert_equal "Manual update of LogTicket", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/LogTicket/#{log_ticket.id}" => {
Expand Down Expand Up @@ -129,7 +129,7 @@ class Avo::ManualChangesSystemTest < ApplicationSystemTestCase
assert_equal log_ticket.id, audit.auditable_id
assert_equal "LogTicket", audit.auditable_type
assert_equal "Manual destroy of LogTicket", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/LogTicket/#{log_ticket.id}" => {
Expand Down
Loading

0 comments on commit 3c23a18

Please sign in to comment.