Skip to content

test(proxy): cover the authentication challenges for basic auth and app tokens - #4092

Merged
asaah262 merged 2 commits into
opencloud-eu:mainfrom
asaah262:test/856-basic-challenge-app-auth
Nov 24, 2026
Merged

asaah262 merged 2 commits into
opencloud-eu:mainfrom
asaah262:test/856-basic-challenge-app-auth

Conversation

@asaah262

Copy link
Copy Markdown
Contributor

Description

A unit test for the WWW-Authenticate challenges the proxy sends with a 401. Only tests change, no production code.

The proxy advertises Basic when 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. loadMiddlewares decides this by passing cfg.EnableBasicAuth || cfg.AuthMiddleware.AllowAppAuth to the authentication middleware. Nothing covered it so far.

TestUnauthenticatedRequestChallenges builds the real middleware chain with loadMiddlewares, sends a request without credentials to a protected route, and checks the status and the challenges:

EnableBasicAuth AllowAppAuth Challenges
false true Bearer, Basic
true false Bearer, Basic
true true Bearer, Basic (Basic only once)
false false Bearer

Setting 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

  • None: tests only, no change of behaviour.

Follow-ups (separate issues, not part of this PR)

Motivation and Context

Clients that use app tokens rely on the Basic challenge to know that they can send them. The test makes sure a change of the middleware setup doesn't silently stop advertising Basic when only app tokens are allowed.

How Has This Been Tested?

  • Test environment: Local Go development environment (testify).
  • New Go test TestUnauthenticatedRequestChallenges (4 cases) in services/proxy/pkg/command/server_test.go. All pass against the current code.
  • Checked that the test catches the regression: with || cfg.AuthMiddleware.AllowAppAuth temporarily removed from loadMiddlewares, the "app tokens only" case fails (only Bearer is sent), the other cases pass. The change was reverted afterwards.
  • go test -race -count=3 -shuffle=on ./services/proxy/pkg/command/ and go test -race ./services/proxy/pkg/middleware/ pass.
  • gofmt and git diff --check are clean, go vet ./services/proxy/pkg/command/ reports nothing.
  • Not run locally: the acceptance tests and golangci-lint. No acceptance test changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation added

AI assistance: Claude Code (claude-opus-5-5) wrote the first draft of the test and this description; I reviewed and adjusted them.

…pp tokens

Assisted-by: Claude Code:claude-opus-5-5
@codacy-production

codacy-production Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 11 complexity

Metric Results
Complexity 11

View in Codacy

🟢 Coverage ∅ diff coverage · +0.08% coverage variation

Metric Results
Coverage variation ✅ +0.08% coverage variation (-1.00%)
Diff coverage ✅ ∅ diff coverage

View coverage diff in Codacy

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.

@asaah262
asaah262 requested a review from pbleser-oc November 23, 2026 13:56
@asaah262
asaah262 marked this pull request as ready for review November 23, 2026 13:57

@pbleser-oc pbleser-oc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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
@asaah262

Copy link
Copy Markdown
Contributor Author

@pbleser-oc Thanks! Done in 1aa9a5a: newTestMetrics now builds the default config itself.

@asaah262
asaah262 merged commit 4e0b8f4 into opencloud-eu:main Nov 24, 2026
12 checks passed
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