1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-28 17:49:38 +02:00

Fix rate limiting errors in API transaction controller tests

When tests run in parallel, they were sharing the same API key fixtures
which caused Redis rate limit counters to accumulate across test workers,
leading to unexpected rate limit errors.

Changes:
- Create fresh API keys in setup instead of using fixtures
- Each API key gets a unique auto-generated ID
- Clear existing active keys to avoid validation conflicts
- Use different sources (web/mobile) for multiple test keys
- Clear Redis rate limit data in setup to ensure clean state
- Update api_headers helper to use display_key instead of plain_key

This follows the existing pattern used in UsageControllerTest for
handling API keys that interact with Redis state.
This commit is contained in:
Zach Gollwitzer 2025-07-08 11:46:33 -04:00
parent 6322c48848
commit 018310d4d1

View file

@ -8,8 +8,29 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
@family = @user.family @family = @user.family
@account = @family.accounts.first @account = @family.accounts.first
@transaction = @family.transactions.first @transaction = @family.transactions.first
@api_key = api_keys(:active_key) # Has read_write scope
@read_only_api_key = api_keys(:one) # Has read scope # Destroy existing active API keys to avoid validation errors
@user.api_keys.active.destroy_all
# Create fresh API keys instead of using fixtures to avoid parallel test conflicts
@api_key = ApiKey.create!(
user: @user,
name: "Test Read-Write Key",
scopes: ["read_write"],
display_key: "test_rw_#{SecureRandom.hex(8)}"
)
@read_only_api_key = ApiKey.create!(
user: @user,
name: "Test Read-Only Key",
scopes: ["read"],
display_key: "test_ro_#{SecureRandom.hex(8)}",
source: "mobile" # Use different source to allow multiple keys
)
# Clear any existing rate limit data
Redis.new.del("api_rate_limit:#{@api_key.id}")
Redis.new.del("api_rate_limit:#{@read_only_api_key.id}")
end end
# INDEX action tests # INDEX action tests
@ -335,6 +356,6 @@ end
private private
def api_headers(api_key) def api_headers(api_key)
{ "X-Api-Key" => api_key.plain_key } { "X-Api-Key" => api_key.display_key }
end end
end end