Skip to content

test(contract): pin request-contract hook header rules - #60

Merged
2ynn merged 2 commits into
mainfrom
tests/auth
Oct 7, 2026
Merged

2ynn merged 2 commits into
mainfrom
tests/auth

Conversation

@2ynn

@2ynn 2ynn commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

request_contract.go is the custom hook that shapes every generated request. Two of its header rules had no contract coverage:

  • a caller-supplied Authorization header must displace x-goog-api-key (only the raw files upload transport was pinned);
  • x-goog-user-project is sent only when the user project is set and non-empty.

Tests

Test Asserts
TestAuthenticationHeadersAndPrecedence / custom Authorization header displaces API key API key in config.yaml + --header "Authorization: Bearer …" → only Authorization reaches the wire
TestUserProjectHeader / set, unset, empty --user-project value is sent; no x-goog-user-project header at all when the flag is absent or empty

Removing either hook branch fails the matching case.

Also: isolatedEnv now sets USERPROFILE alongside HOME; os.UserHomeDir reads it on Windows, where tests would otherwise read the developer's real config.yaml.

@2ynn
2ynn requested a review from a team October 1, 2026 17:45
@2ynn 2ynn changed the title test(contract): cover auth login and logout against a fake keychain Oct 1, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces contract tests for the authentication commands (auth login, auth logout, and auth whoami) along with a file-backed fake keyring implementation to isolate tests from the host OS keychain. Feedback on these changes includes resolving the absolute path of the fake keyring source directory using runtime.Caller to prevent build failures when tests are executed from different working directories, and gracefully handling empty keyring files in the fake keyring loader to avoid JSON unmarshalling errors.

Comment thread tests/contract/auth_test.go Resolved
auth login and logout write to the OS keychain, so running them from the
contract suite would read and delete the keychain entries of whoever runs
the tests. testdata/fakekeyring wraps the CLI with a file-backed keychain
(config.SetKeyringBackend), built once next to the contract binary.

Covers dry-run no-ops, login without credential flags, storage in the
keychain or the 0600 config-file fallback, whoami sources and masking, the
credential reaching the wire, and logout clearing both stores while keeping
other settings. auth login/logout/whoami join the command inventory.
@2ynn
2ynn requested a review from wanlin31 October 1, 2026 19:24
@2ynn
2ynn marked this pull request as draft October 1, 2026 19:30
auth login/logout, the keychain fallback and whoami are generated code,
covered by the generator's own suite; drop auth_test.go and the
fake-keychain entrypoint.

Cover the custom request hook instead: a custom Authorization header
displaces the API key on generated requests, and x-goog-user-project is
sent only when set and non-empty. isolatedEnv also points USERPROFILE at
the temporary home, which os.UserHomeDir reads on Windows.
@2ynn 2ynn changed the title chore(tests): cover auth login and logout against a fake keychain Oct 1, 2026
@2ynn
2ynn marked this pull request as ready for review October 1, 2026 20:33
@2ynn
2ynn merged commit 8831565 into main Oct 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants