Repository navigation
Expand file tree
/
Copy pathauth_test.go
More file actions
59 lines (50 loc) · 1.43 KB
/
Copy pathauth_test.go
File metadata and controls
59 lines (50 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package api
import (
"net/http"
"testing"
)
type testAuthProvider struct{}
func (provider testAuthProvider) Authenticate(r *http.Request) (*AuthResult, error) {
return &AuthResult{
Subject: "user-123",
Token: r.Header.Get("Authorization"),
Claims: map[string]any{
"role": "admin",
},
}, nil
}
func TestRegisteredAuthProviderCanBeResolved(t *testing.T) {
RegisterAuth("test-auth", testAuthProvider{})
provider, err := AuthByName("test-auth")
if err != nil {
t.Fatalf("expected registered provider, got error: %v", err)
}
req, _ := http.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", "Bearer token")
result, err := provider.Authenticate(req)
if err != nil {
t.Fatalf("authenticate failed: %v", err)
}
if result.Subject != "user-123" || result.Claims["role"] != "admin" {
t.Fatalf("unexpected auth result: %#v", result)
}
}
func TestAuthByNameReturnsErrorForMissingProvider(t *testing.T) {
provider, err := AuthByName("missing-auth")
if err == nil {
t.Fatalf("expected error")
}
if provider != nil {
t.Fatalf("expected nil provider, got %#v", provider)
}
}
func TestRegisterAuthIgnoresInvalidInput(t *testing.T) {
RegisterAuth("", testAuthProvider{})
RegisterAuth("nil-auth", nil)
if _, err := AuthByName(""); err == nil {
t.Fatalf("empty auth name should not be registered")
}
if _, err := AuthByName("nil-auth"); err == nil {
t.Fatalf("nil auth provider should not be registered")
}
}