Files
coder/coderd/oauth2provider/authorize_internal_test.go
T
Ethan c650aabbef chore: standardize on *_internal_test.go for white-box tests (#25601)
My agent added `//nolint:testpackage` to a test file on one of my PRs.
Again. This PR cleans it up across the entire repo and updates the
in-repo conventions so future agents stop doing it.

The repo already has a precedent for white-box tests that need to touch
unexported symbols: `*_internal_test.go` (145+ existing files). The
`testpackage` linter's default `skip-regexp` exempts that filename
suffix, so the `//nolint:testpackage` directive is unnecessary in every
case where someone reached for it. This PR renames 51 such files to
`*_internal_test.go` via `git mv` so blame and history follow, and
strips the dead directive from 2 files that were already correctly named
(`coderd/oauth2provider/authorize_internal_test.go`,
`coderd/x/chatd/advisor_internal_test.go`).

`.claude/docs/TESTING.md` now documents the rule explicitly under *Test
Package Naming*, which is imported into the root `AGENTS.md` via
`@.claude/docs/TESTING.md`. The rule: prefer `package foo_test`; if you
need internal access, rename the file to `*_internal_test.go` rather
than adding a nolint directive.
2026-05-22 20:24:38 +10:00

53 lines
1.5 KiB
Go

package oauth2provider
import (
"crypto/sha256"
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHashOAuth2State(t *testing.T) {
t.Parallel()
t.Run("EmptyState", func(t *testing.T) {
t.Parallel()
result := hashOAuth2State("")
assert.False(t, result.Valid, "empty state should return invalid NullString")
assert.Empty(t, result.String, "empty state should return empty string")
})
t.Run("NonEmptyState", func(t *testing.T) {
t.Parallel()
state := "test-state-value"
result := hashOAuth2State(state)
require.True(t, result.Valid, "non-empty state should return valid NullString")
// Verify it's a proper SHA-256 hash.
expected := sha256.Sum256([]byte(state))
assert.Equal(t, hex.EncodeToString(expected[:]), result.String,
"state hash should be SHA-256 hex digest")
})
t.Run("DifferentStatesProduceDifferentHashes", func(t *testing.T) {
t.Parallel()
hash1 := hashOAuth2State("state-a")
hash2 := hashOAuth2State("state-b")
require.True(t, hash1.Valid)
require.True(t, hash2.Valid)
assert.NotEqual(t, hash1.String, hash2.String,
"different states should produce different hashes")
})
t.Run("SameStateProducesSameHash", func(t *testing.T) {
t.Parallel()
hash1 := hashOAuth2State("deterministic")
hash2 := hashOAuth2State("deterministic")
require.True(t, hash1.Valid)
assert.Equal(t, hash1.String, hash2.String,
"same state should produce identical hash")
})
}