Module: RailsAuditLog::Graphql::Testing::MinitestAssertions

Defined in:
lib/rails_audit_log/graphql/testing/minitest_assertions.rb

Overview

Minitest assertions for GraphQL audit log entries.

Include this module in a test class or in a shared support file:

class ActiveSupport::TestCase
  include RailsAuditLog::Graphql::Testing::MinitestAssertions
end

The assertions inspect the auditLogEntries and auditLogEntriesConnection.nodes keys in the response data. To use the touching: option, include diff { attribute } in your GraphQL query.

Instance Method Summary collapse

Instance Method Details

#assert_graphql_audit_entry(response, event:, touching: nil, item_type: nil, message: nil) ⇒ Object

Asserts that the GraphQL response contains at least one audit log entry matching the given criteria.

Examples:

Assert an update entry exists

result = MySchema.execute('{ auditLogEntries { event } }')
assert_graphql_audit_entry result, event: :update

Assert an update entry that touched :title

result = MySchema.execute('{ auditLogEntries { event diff { attribute } } }')
assert_graphql_audit_entry result, event: :update, touching: :title

Parameters:

  • response (Hash, GraphQL::Query::Result)

    the result of Schema.execute

  • event (Symbol, String)

    expected event type (:create, :update, :destroy)

  • touching (Symbol, String, nil) (defaults to: nil)

    when given, requires the entry’s diff to include an attribute with this name (query must include diff { attribute })

  • item_type (String, nil) (defaults to: nil)

    when given, requires the entry’s itemType to match

  • message (String, nil) (defaults to: nil)

    custom failure message



35
36
37
38
39
# File 'lib/rails_audit_log/graphql/testing/minitest_assertions.rb', line 35

def assert_graphql_audit_entry(response, event:, touching: nil, item_type: nil, message: nil)
  matched = filter_entries(response, event: event, touching: touching, item_type: item_type)
  default_msg = build_message("Expected", event, touching, item_type)
  assert matched.any?, message || default_msg
end

#refute_graphql_audit_entry(response, event:, touching: nil, item_type: nil, message: nil) ⇒ Object

Asserts that the GraphQL response does NOT contain an audit log entry matching the given criteria.

Parameters:

  • response (Hash, GraphQL::Query::Result)

    the result of Schema.execute

  • event (Symbol, String)

    the event type to check

  • touching (Symbol, String, nil) (defaults to: nil)

    attribute name to match against diff

  • item_type (String, nil) (defaults to: nil)

    model class name to match against itemType

  • message (String, nil) (defaults to: nil)

    custom failure message



49
50
51
52
53
# File 'lib/rails_audit_log/graphql/testing/minitest_assertions.rb', line 49

def refute_graphql_audit_entry(response, event:, touching: nil, item_type: nil, message: nil)
  matched = filter_entries(response, event: event, touching: touching, item_type: item_type)
  default_msg = build_message("Expected no", event, touching, item_type)
  refute matched.any?, message || default_msg
end