From 018310d4d1898f64833a8822bbd223bf918d4d4b Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Tue, 8 Jul 2025 11:46:33 -0400 Subject: [PATCH] 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. --- .../api/v1/transactions_controller_test.rb | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb index 7978a5f6..dfbcff64 100644 --- a/test/controllers/api/v1/transactions_controller_test.rb +++ b/test/controllers/api/v1/transactions_controller_test.rb @@ -8,8 +8,29 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest @family = @user.family @account = @family.accounts.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 # INDEX action tests @@ -335,6 +356,6 @@ end private def api_headers(api_key) - { "X-Api-Key" => api_key.plain_key } + { "X-Api-Key" => api_key.display_key } end end