Skip to content

[test-improver] Improve tests for auth package - #7650

Merged
lpcox merged 8 commits into
mainfrom
improve-auth-header-tests-26e82a32345023e0
Jun 17, 2026
Merged

[test-improver] Improve tests for auth package#7650
lpcox merged 8 commits into
mainfrom
improve-auth-header-tests-26e82a32345023e0

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Test Improvements: internal/auth/header_test.go

File Analyzed

  • Test File: internal/auth/header_test.go
  • Package: internal/auth
  • Lines Changed: +78 / -20

1. Fix Bound Asserter Scoping (correctness fix)

All seven table-driven test functions in header_test.go bound assert/require to the outer *testing.T, then used those bound asserters inside t.Run subtests:

// Before (incorrect pattern)
func TestIsMalformedHeader(t *testing.T) {
    assert := assert.New(t)           // bound to OUTER t
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {   // inner t is different
            assert.Equal(tt.want, got)         // silently uses OUTER t!
        })
    }
}

This caused two real problems:

  • Wrong failure attribution: test failures were reported against TestIsMalformedHeader instead of TestIsMalformedHeader/Null_byte_is_malformed, making failures much harder to diagnose
  • Wrong FailNow target: require.NoError/require.ErrorIs call t.FailNow() on the outer test, which can abort all remaining subtests when only one should stop

Fix: switched all inner assertions to the non-bound form using the subtest's own t:

// After (correct pattern)
func TestIsMalformedHeader(t *testing.T) {
    t.Parallel()
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            t.Parallel()
            assert.Equal(t, tt.want, got)  // uses subtest's t
        })
    }
}

Affected test functions:

  • TestIsMalformedHeader
  • TestTruncateSecret
  • TestParseAuthHeader (also had require.ErrorIs/require.NoError scoping issue)
  • TestValidateAgentID
  • TestExtractAgentID
  • TestExtractSessionID
  • TestStripAuthScheme

2. Add t.Parallel() Throughout

All top-level test functions and their subtests now call t.Parallel(). The auth functions under test are pure (no global state mutation), so parallel execution is safe and catches potential data-race issues.

3. New Edge-Case Test Inputs

Added missing inputs that test boundary behaviour of the scheme-stripping logic:

TestParseAuthHeader — 2 new cases:

  • "Bearer " (Bearer prefix with empty token) → apiKey="", agentID=""
  • "Agent " (Agent prefix with empty value) → apiKey="", agentID=""

TestExtractSessionIDFromHeaders — 3 new cases:

  • Both headers empty → ""
  • Valid X-Agent-ID with malformed Authorization → returns the valid X-Agent-ID
  • X-Agent-ID present alongside a Bearer AuthorizationX-Agent-ID wins

TestStripAuthScheme — 2 new cases:

  • "Bearer " → scheme "Bearer", value "", matched true
  • "Agent " → scheme "Agent", value "", matched true

Test Execution

All tests pass and are stable across multiple runs:

ok  github.com/github/gh-aw-mcpg/internal/auth  0.009s

Subtests now report correctly:

--- PASS: TestIsMalformedHeader/Null_byte_(0x00)_is_malformed (0.00s)
--- PASS: TestParseAuthHeader/Bearer_with_empty_token (0.00s)
--- PASS: TestExtractSessionIDFromHeaders/both_headers_empty_returns_empty_string (0.00s)

Notes on Remaining Uncovered Lines

The two functions below 100% contain legitimately untestable paths:

  • ExtractAgentID (83.3%): the if err != nil guard after ParseAuthHeader is dead code — ParseAuthHeader only errors on empty input, which is already handled by the preceding nil-check
  • GenerateRandomAgentID (71.4%): the error path requires crypto/rand to fail, which needs dependency injection to test

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:

  • index.crates.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "index.crates.io"

See Network Configuration for more information.

Generated by Test Improver · 1.8K AIC · ⊞ 29.5K ·

Fix bound asserter scoping in header_test.go: all seven table-driven
test functions were creating assert/require bound to the outer *testing.T,
then using those inside t.Run subtests. This caused:

- Test failures to be reported against the outer test function instead
  of the specific failing subtest, making failures hard to identify
- require.FailNow() (via require.NoError/require.ErrorIs) to call
  FailNow() on the outer t instead of the subtest's t, which could
  stop all subsequent subtests on the first require failure

Fix by switching all inner assertions to the non-bound form
(assert.Equal(t, ...) using the subtest's t parameter).

Also add t.Parallel() to all top-level test functions and their
subtests to catch data-race issues and speed up execution.

Add missing edge-case test inputs:
- ParseAuthHeader: "Bearer " (Bearer with empty token) and
  "Agent " (Agent with empty value)
- ExtractSessionIDFromHeaders: both headers empty, valid X-Agent-ID
  with malformed Authorization, X-Agent-ID with Bearer Authorization
- StripAuthScheme: "Bearer " and "Agent " (scheme prefix with no value)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the internal/auth test suite by fixing incorrect testify bound-asserter usage in subtests, increasing parallelization, and adding coverage for additional auth header edge cases. These changes strengthen failure attribution and exercise more boundary behaviors in auth scheme parsing and session ID extraction.

Changes:

  • Replaced bound assert.New(t) / require.New(t) usage with assert.*(t, ...) / require.*(t, ...) inside t.Run subtests.
  • Added t.Parallel() to top-level tests and subtests.
  • Added new test cases for empty Bearer / Agent values and additional ExtractSessionIDFromHeaders precedence scenarios.
Show a summary per file
File Description
internal/auth/header_test.go Refactors subtest assertion scoping, adds parallelization, and expands edge-case coverage for auth header parsing and session ID extraction.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 7

Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
Comment thread internal/auth/header_test.go
lpcox and others added 3 commits June 17, 2026 07:49
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:50 View session
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:50 View session
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:50 View session
lpcox and others added 2 commits June 17, 2026 07:50
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:51 View session
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:51
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:52
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:52
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:52
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:52 View session
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:53 View session
GitHub Advanced Security started work on behalf of lpcox June 17, 2026 14:53 View session
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:54
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:54
GitHub Advanced Security finished work on behalf of lpcox June 17, 2026 14:54
@lpcox
lpcox merged commit ff75ba4 into main Jun 17, 2026
29 checks passed
@lpcox
lpcox deleted the improve-auth-header-tests-26e82a32345023e0 branch June 17, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants