Skip to content

Commit dfaa8f3

Browse files
authored
auth/oidc: test slice-typed extra claims comparison (#1245)
## Summary Follow-up to #1238 (closes #988). Adds direct unit-test coverage for the `claimMatches` helper, which was the actual change in #1238. `claimMatches` and `toAnySlice` are unexported, so the tests live in `oidc_internal_test.go` (`package auth`) — same pattern as `http_internal_test.go`. Cases covered: - scalar/scalar match and mismatch - scalar actual against array expected (both directions of overlap) - array actual against scalar expected - **array/array** — pre-#1238 this combination panicked with `comparing uncomparable type []interface {}` - `[]string` typed slice (exercises the typed-slice branch of `toAnySlice`, which `[]any` skips) - non-string scalars (int, bool) - empty actual array Test values use single letters (`x`, `y`, `p`, `q`) rather than role labels — only the relationships matter, and short tokens keep `goconst` quiet. ## Test plan - [x] `go test ./internal/api/handlers/v0/auth/...` — all 13 subtests pass - [x] `golangci-lint run ./internal/api/handlers/v0/auth/...` — no new issues vs main
1 parent 811046c commit dfaa8f3

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package auth
2+
3+
import "testing"
4+
5+
func TestClaimMatches(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
actual any
9+
expected any
10+
want bool
11+
}{
12+
{"scalar/scalar match", "x", "x", true},
13+
{"scalar/scalar mismatch", "x", "y", false},
14+
15+
{"scalar actual in array expected", "x", []any{"x", "y"}, true},
16+
{"scalar actual not in array expected", "z", []any{"x", "y"}, false},
17+
18+
{"array actual contains scalar expected", []any{"x", "y"}, "x", true},
19+
{"array actual missing scalar expected", []any{"y", "z"}, "x", false},
20+
21+
// Pre-#1238 array/array comparison panicked with "comparing uncomparable type []interface {}".
22+
{"array/array overlap", []any{"a", "b"}, []any{"b", "c"}, true},
23+
{"array/array disjoint", []any{"a", "b"}, []any{"c", "d"}, false},
24+
25+
// Exercises the []string branch of toAnySlice (concrete typed slice, not []any).
26+
{"[]string actual matches scalar", []string{"p", "q"}, "p", true},
27+
{"scalar matches []string expected", "p", []string{"p", "q"}, true},
28+
29+
{"non-string scalar match", 42, 42, true},
30+
{"bool scalar match", true, true, true},
31+
{"empty actual array", []any{}, "x", false},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
if got := claimMatches(tt.actual, tt.expected); got != tt.want {
37+
t.Errorf("claimMatches(%v, %v) = %v, want %v", tt.actual, tt.expected, got, tt.want)
38+
}
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)