Files
CaddyProxyManager/backend/internal/auth/oidc_test.go
Pacerino 8019bb9861 [Backend] Add configurable authentication (local or OIDC)
Support CPM_AUTH_MODE=local|oidc. OIDC uses go-oidc with JIT user
provisioning and an optional email-domain allowlist. Adds auth config,
login and callback endpoints, a JWT signing-key bootstrap and
Provider/Subject fields on the user model. Includes OIDC allowlist tests.
2026-06-14 02:35:13 +02:00

55 lines
1.6 KiB
Go

package auth
import (
"testing"
"github.com/Pacerino/CaddyProxyManager/internal/config"
)
func TestIsAllowedDomain(t *testing.T) {
original := config.Configuration.Auth.OIDC.AllowedDomains
t.Cleanup(func() { config.Configuration.Auth.OIDC.AllowedDomains = original })
tests := []struct {
name string
allowed []string
email string
want bool
}{
{"empty allowlist permits all", nil, "user@anything.com", true},
{"exact match", []string{"example.com"}, "user@example.com", true},
{"case insensitive", []string{"Example.COM"}, "user@example.com", true},
{"whitespace trimmed", []string{" example.com "}, "user@example.com", true},
{"not allowed", []string{"example.com"}, "user@evil.com", false},
{"no at sign", []string{"example.com"}, "notanemail", false},
{"one of many", []string{"a.com", "b.com"}, "user@b.com", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Configuration.Auth.OIDC.AllowedDomains = tt.allowed
if got := isAllowedDomain(tt.email); got != tt.want {
t.Errorf("isAllowedDomain(%q) with %v = %v, want %v", tt.email, tt.allowed, got, tt.want)
}
})
}
}
func TestIsOIDCEnabled(t *testing.T) {
original := config.Configuration.Auth.Mode
t.Cleanup(func() { config.Configuration.Auth.Mode = original })
config.Configuration.Auth.Mode = "oidc"
if !IsOIDCEnabled() {
t.Error("expected oidc enabled")
}
config.Configuration.Auth.Mode = "OIDC"
if !IsOIDCEnabled() {
t.Error("expected oidc enabled (case-insensitive)")
}
config.Configuration.Auth.Mode = "local"
if IsOIDCEnabled() {
t.Error("expected oidc disabled")
}
}