Repository navigation
test(proxy): cover the authentication challenges for basic auth and app tokens - #4092
Conversation
…pp tokens Assisted-by: Claude Code:claude-opus-5-5
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 11 |
🟢 Coverage ∅ diff coverage · +0.08% coverage variation
Metric Results Coverage variation ✅ +0.08% coverage variation (-1.00%) Diff coverage ✅ ∅ diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (d43947a) 94350 28356 30.05% Head commit (1aa9a5a) 94350 (+0) 28433 (+77) 30.14% (+0.08%) Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#4092) 0 0 ∅ (not applicable) Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
pbleser-oc
left a comment
There was a problem hiding this comment.
Thanks Sara, this looks good to me. Building the real chain with loadMiddlewares is what makes the test worth having: when I changed the condition in server.go to only cfg.EnableBasicAuth, the "app tokens only" case failed, so it guards exactly the case that wasn't covered. It also passes with -race -count=2, so resetting the package state works.
One nitpick, feel free to ignore it: newTestMetrics takes cfg, but only the first call uses it, because of the sync.Once. Passing the routes of the default config inside it, without the parameter, would make that clearer.
Assisted-by: Claude Code:claude-opus-5-5
|
@pbleser-oc Thanks! Done in 1aa9a5a: newTestMetrics now builds the default config itself. |
Description
A unit test for the
WWW-Authenticatechallenges the proxy sends with a401. Only tests change, no production code.The proxy advertises
Basicwhen basic auth is enabled, and also when only app tokens are allowed (PROXY_ENABLE_APP_AUTH, on by default), since app tokens are sent with basic auth.loadMiddlewaresdecides this by passingcfg.EnableBasicAuth || cfg.AuthMiddleware.AllowAppAuthto the authentication middleware. Nothing covered it so far.TestUnauthenticatedRequestChallengesbuilds the real middleware chain withloadMiddlewares, sends a request without credentials to a protected route, and checks the status and the challenges:EnableBasicAuthAllowAppAuthBearer,BasicBearer,BasicBearer,Basic(Basiconly once)BearerSetting up the chain changes package-level state (the list of challenges, and the global config the CSP settings are loaded into), so the test resets it before and after each case, and creates the metrics, which register globally, only once.
Related Issue
Follow-ups (separate issues, not part of this PR)
credentials_by_user_agentmatches the user agent the wrong way round #4091:credentials_by_user_agentmatches the user agent the wrong way round. The test uses the default (no user agent lock-in), so it doesn't depend on that bug.Motivation and Context
Clients that use app tokens rely on the
Basicchallenge to know that they can send them. The test makes sure a change of the middleware setup doesn't silently stop advertisingBasicwhen only app tokens are allowed.How Has This Been Tested?
TestUnauthenticatedRequestChallenges(4 cases) inservices/proxy/pkg/command/server_test.go. All pass against the current code.|| cfg.AuthMiddleware.AllowAppAuthtemporarily removed fromloadMiddlewares, the "app tokens only" case fails (onlyBeareris sent), the other cases pass. The change was reverted afterwards.go test -race -count=3 -shuffle=on ./services/proxy/pkg/command/andgo test -race ./services/proxy/pkg/middleware/pass.gofmtandgit diff --checkare clean,go vet ./services/proxy/pkg/command/reports nothing.golangci-lint. No acceptance test changes.Types of changes
Checklist:
AI assistance: Claude Code (claude-opus-5-5) wrote the first draft of the test and this description; I reviewed and adjusted them.