[test-improver] Improve tests for auth package (GenerateRandomAPIKey)#5441
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test-improver] Improve tests for auth package (GenerateRandomAPIKey)#5441github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Add comprehensive tests for GenerateRandomAPIKey: - Validate hex encoding by decoding with encoding/hex - Verify lowercase hex charset via regex - Test uniqueness across 20 iterations - Test length consistency across repeated calls Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements:
apikey_test.goFile Analyzed
internal/auth/apikey_test.gointernal/authImprovements Made
1. Better Testing Patterns
TestFunctionName_Scenarionaming for each new test functionGenerateRandomAPIKey, following the single-responsibility principle2. Increased Coverage
The two uncovered branches in
GenerateRandomAPIKeyrequirecrypto/randto fail at the OS level, which cannot be triggered in unit tests without OS-level fault injection. However, the new tests add meaningful quality verification:TestGenerateRandomAPIKey_IsValidHex— decodes the returned key withencoding/hexto confirm it is valid hex and resolves to exactly 32 bytesTestGenerateRandomAPIKey_IsLowercaseHex— uses a regex to confirm output uses only[0-9a-f](lowercase), matchinghex.EncodeToStringoutputTestGenerateRandomAPIKey_Uniqueness— verifies uniqueness across 20 consecutive calls (up from 2), catching any regression in entropy usageTestGenerateRandomAPIKey_LengthConsistency— confirms every call returns exactly 64 characters, not just the first one3. Cleaner & More Stable Tests
require.NoErrorguards before each assertion so failures abort early with a clear messageTest Execution
All tests pass:
Why These Changes?
apikey_test.gowas selected because it had a single test function that verified only surface-level properties (non-empty, length 64, two unique calls).GenerateRandomAPIKeyis a security-critical function (spec §7.3) — the gateway uses it to auto-generate API keys on startup — so stronger assertions about encoding format, character set, and entropy uniqueness add meaningful confidence that the function behaves correctly.Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
invalidhostthatdoesnotexist12345.comSee Network Configuration for more information.