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 876a1cc
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 72 deletions.
12 changes: 6 additions & 6 deletions test/integration/api/v1/oidc/api_key_roles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Api::V1::OIDC::ApiKeyRolesTest < ActionDispatch::IntegrationTest
headers: { "HTTP_AUTHORIZATION" => @user_api_key }

assert_response :success
assert_equal(
assert_equal_hash(
{
"id" => @role.id,
"token" => @role.token,
Expand Down Expand Up @@ -252,7 +252,7 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
assert_equal_hash({
"rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
Expand Down Expand Up @@ -281,7 +281,7 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
assert_equal_hash({
"rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
Expand Down Expand Up @@ -344,7 +344,7 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
assert_equal_hash({
"rubygems_api_key" => resp["rubygems_api_key"],
"name" => "#{@role.name}-79685b65-945d-450a-a3d8-a36bcf72c23d",
"scopes" => ["push_rubygem"],
Expand All @@ -358,7 +358,7 @@ 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(
assert_equal_hash(
{
"claims" => @claims,
"header" => {
Expand All @@ -377,7 +377,7 @@ def jwt(claims = @claims, key: @pkey)
headers: {}

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

assert_response :success
assert_equal(
assert_equal_hash(
{
"api_key_role_token" => @id_token.api_key_role.token,
"jwt" => {
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 Down Expand Up @@ -142,7 +142,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 Down Expand Up @@ -180,9 +180,10 @@ 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,7 +241,7 @@ def jwt(claims = @claims, key: @pkey)
resp = response.parsed_body

assert_match(/^rubygems_/, resp["rubygems_api_key"])
assert_equal({
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"],
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/upload_info_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UploadInfoFileJobTest < ActiveJob::TestCase

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

assert_equal(
assert_equal_hash(
{
metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/upload_names_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UploadNamesFileJobTest < ActiveJob::TestCase

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

assert_equal(
assert_equal_hash(
{
metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/upload_versions_file_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UploadVersionsFileJobTest < ActiveJob::TestCase

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

assert_equal(
assert_equal_hash(
{
metadata: {
"surrogate-control" => "max-age=3600, stale-while-revalidate=1800",
Expand Down
4 changes: 2 additions & 2 deletions test/models/oidc/trusted_publisher/github_action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ 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: [
{
Expand Down Expand Up @@ -113,7 +113,7 @@ class OIDC::TrustedPublisher::GitHubActionTest < ActiveSupport::TestCase

publisher.update!(environment: "test")

assert_equal(
assert_equal_hash(
{
statements: [
{
Expand Down
2 changes: 1 addition & 1 deletion test/models/sendgrid_event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SendgridEventTest < ActiveSupport::TestCase
assert_equal("bounce", event.event_type)
assert_equal(occurred_at, event.occurred_at)
assert_predicate event, :pending?
assert_equal(
assert_equal_hash(
{
"email" => "[email protected]",
"sg_event_id" => "t61hI0Xpmk8XSR1YX4s0Kg==",
Expand Down
4 changes: 2 additions & 2 deletions test/models/web_hook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def stub_hook_relay_request(url, webhook_id, authorization, max_attempts = 3)
end

should "show limited attributes for to_json" do
assert_equal(
assert_equal_hash(
{
"url" => @url,
"failure_count" => @webhook.failure_count
Expand All @@ -115,7 +115,7 @@ def stub_hook_relay_request(url, webhook_id, authorization, max_attempts = 3)
end

should "show limited attributes for to_yaml" do
assert_equal(
assert_equal_hash(
{
"url" => @url,
"failure_count" => @webhook.failure_count
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
8 changes: 4 additions & 4 deletions test/system/avo/rubygems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Avo::RubygemsSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "Rubygem", audit.auditable_type
assert_equal "Release reserved namespace", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Rubygem/#{rubygem.id}" => {
Expand Down Expand Up @@ -106,7 +106,7 @@ class Avo::RubygemsSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "Rubygem", audit.auditable_type
assert_equal "Yank Rubygem", audit.action
assert_equal(
assert_equal_hash(
{
"fields" => { "version" => version.id.to_s },
"arguments" => {},
Expand Down Expand Up @@ -187,7 +187,7 @@ class Avo::RubygemsSystemTest < ApplicationSystemTestCase
assert_equal "Rubygem", audit.auditable_type
assert_equal "Yank Rubygem", audit.action

assert_equal(
assert_equal_hash(
{
"fields" => { "version" => "All" },
"arguments" => {},
Expand Down Expand Up @@ -274,7 +274,7 @@ class Avo::RubygemsSystemTest < ApplicationSystemTestCase
assert_equal "Rubygem", audit.auditable_type
assert_equal "Add owner", audit.action

assert_equal(
assert_equal_hash(
{
"fields" => { "owner" => { "id" => new_owner.id, "handle" => new_owner.handle } },
"arguments" => {},
Expand Down
14 changes: 7 additions & 7 deletions test/system/avo/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Reset User 2FA", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -123,7 +123,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Block User", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -208,7 +208,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Reset Api Key", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -294,7 +294,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
end
rubygem_updated_at_changes = rubygem_audit["gid://gemcutter/Rubygem/#{rubygem.id}"]["changes"]["updated_at"]

assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Deletion/#{deletion.id}" => {
Expand Down Expand Up @@ -398,7 +398,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
password_changed_event = user.events.where(tag: Events::UserEvent::PASSWORD_CHANGED).sole
version_yanked_event = rubygem.events.where(tag: Events::RubygemEvent::VERSION_YANKED).sole

assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Deletion/#{deletion.id}" => {
Expand Down Expand Up @@ -519,7 +519,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Change User Email", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down Expand Up @@ -593,7 +593,7 @@ class Avo::UsersSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "User", audit.auditable_type
assert_equal "Create User", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/User/#{user.id}" => {
Expand Down
2 changes: 1 addition & 1 deletion test/system/avo/versions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Avo::VersionsSystemTest < ApplicationSystemTestCase
rubygem_updated_at_changes = rubygem_audit["gid://gemcutter/Rubygem/#{rubygem.id}"]["changes"]["updated_at"]
version_unyank_event = rubygem.events.where(tag: Events::RubygemEvent::VERSION_UNYANKED).sole

assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/Version/#{version.id}" =>
Expand Down
2 changes: 1 addition & 1 deletion test/system/avo/web_hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Avo::WebHooksSystemTest < ApplicationSystemTestCase
page.assert_text audit.id
assert_equal "WebHook", audit.auditable_type
assert_equal "Delete Webhook", audit.action
assert_equal(
assert_equal_hash(
{
"records" => {
"gid://gemcutter/WebHook/#{web_hook.id}" => {
Expand Down
Loading

0 comments on commit 876a1cc

Please sign in to comment.