Files
gitea-mcp-forward-auth/internal/auth/auth_test.go
T
torben ea4aac6641
Build and Test / verify (push) Failing after 22s
Add initial implementation of gitea-mcp-forward-auth microservice
- Create build and release workflows for CI/CD
- Implement Dockerfile for multi-stage builds
- Add core authentication logic with token validation
- Include HTTP handler for authorization checks
- Set up Kubernetes deployment and service manifests
- Update README with usage instructions and configuration details
2026-07-11 21:58:58 +02:00

88 lines
2.1 KiB
Go

package auth
import "testing"
func TestValidator_IsValid(t *testing.T) {
t.Parallel()
validator, err := NewValidator([]string{"token-one", "token-two", "token-three"})
if err != nil {
t.Fatalf("NewValidator() error = %v", err)
}
tests := []struct {
name string
token string
want bool
}{
{name: "valid token one", token: "token-one", want: true},
{name: "valid token two", token: "token-two", want: true},
{name: "unknown token", token: "wrong-token", want: false},
{name: "empty token", token: "", want: false},
{name: "whitespace token", token: " ", want: false},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := validator.IsValid(tc.token); got != tc.want {
t.Fatalf("IsValid() = %v, want %v", got, tc.want)
}
})
}
}
func TestNewValidator_EmptyTokenSet(t *testing.T) {
t.Parallel()
if _, err := NewValidator(nil); err == nil {
t.Fatal("NewValidator(nil) expected error, got nil")
}
if _, err := NewValidator([]string{"", " "}); err == nil {
t.Fatal("NewValidator(empty tokens) expected error, got nil")
}
}
func TestParseBearerToken(t *testing.T) {
t.Parallel()
tests := []struct {
name string
header string
want string
wantErr bool
}{
{name: "valid bearer", header: "Bearer abc123", want: "abc123"},
{name: "valid lowercase prefix", header: "bearer abc123", want: "abc123"},
{name: "valid uppercase prefix", header: "BEARER abc123", want: "abc123"},
{name: "missing header", header: "", wantErr: true},
{name: "wrong scheme", header: "Basic abc123", wantErr: true},
{name: "missing token", header: "Bearer", wantErr: true},
{name: "too many parts", header: "Bearer one two", wantErr: true},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got, err := ParseBearerToken(tc.header)
if tc.wantErr {
if err == nil {
t.Fatal("ParseBearerToken() expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("ParseBearerToken() error = %v", err)
}
if got != tc.want {
t.Fatalf("ParseBearerToken() = %q, want %q", got, tc.want)
}
})
}
}