2026-09-13 18:43:16 +02:00
|
|
|
package auth
|
|
|
|
|
|
2026-09-14 09:35:45 +02:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// fakeProxyStore is a minimal in-memory ProxyStore for constructing a
|
|
|
|
|
// Service in tests without a real database.
|
|
|
|
|
type fakeProxyStore struct {
|
|
|
|
|
loginRequired bool
|
|
|
|
|
setupClaimed bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *fakeProxyStore) LoginRequired(context.Context) (bool, error) { return s.loginRequired, nil }
|
|
|
|
|
func (s *fakeProxyStore) SetLoginRequired(_ context.Context, v bool) error {
|
|
|
|
|
s.loginRequired = v
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
func (s *fakeProxyStore) IsSetupClaimed(context.Context) (bool, error) { return s.setupClaimed, nil }
|
|
|
|
|
func (s *fakeProxyStore) TryClaimSetup(context.Context) (bool, error) {
|
|
|
|
|
if s.setupClaimed {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
s.setupClaimed = true
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
func (s *fakeProxyStore) UnclaimSetup(context.Context) error {
|
|
|
|
|
s.setupClaimed = false
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestPendingModeStillGatesRequests locks in the fix that made an empty
|
|
|
|
|
// authGuardURL at construction mean "pending" (auth still enabled, calls
|
|
|
|
|
// fail closed until a real URL arrives via SetAuthGuardBaseURL) instead of
|
|
|
|
|
// "disabled" (nothing gated, a fixed superadmin identity) - the bug that
|
|
|
|
|
// made AUTHGUARD_URL a de-facto requirement even though ProjectConfigurator
|
|
|
|
|
// can push it live after startup.
|
|
|
|
|
func TestPendingModeStillGatesRequests(t *testing.T) {
|
|
|
|
|
service := NewWithStore(&fakeProxyStore{}, "session", false, "", "testproject")
|
|
|
|
|
if !service.enabled() {
|
|
|
|
|
t.Fatal("Service with no authGuardURL and no Disabled() option should still be enabled (pending), not disabled")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
|
req.AddCookie(&http.Cookie{Name: "auth_session", Value: "sometoken"})
|
|
|
|
|
if _, err := service.CurrentUser(req); err == nil {
|
|
|
|
|
t.Fatal("pending Service (no AuthGuard URL yet) should fail closed on a real session cookie, not silently resolve a user")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestDisabledOptionSkipsGating confirms the explicit opt-out still works
|
|
|
|
|
// exactly like the old "empty authGuardURL" behavior.
|
|
|
|
|
func TestDisabledOptionSkipsGating(t *testing.T) {
|
|
|
|
|
service := NewWithStore(&fakeProxyStore{}, "session", false, "", "testproject", Disabled())
|
|
|
|
|
if service.enabled() {
|
|
|
|
|
t.Fatal("Service built with Disabled() should report enabled() == false")
|
|
|
|
|
}
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
|
user, err := service.CurrentUser(req)
|
|
|
|
|
if err != nil || user == nil || user.Username != "local" {
|
|
|
|
|
t.Fatalf("Disabled() Service should resolve the fixed local superadmin profile, got user=%+v err=%v", user, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-13 18:43:16 +02:00
|
|
|
|
|
|
|
|
func TestNormalizeProxyCredentials(t *testing.T) {
|
|
|
|
|
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if email != "user@example.com" || username != "user" || password == "" {
|
|
|
|
|
t.Fatalf("normalized credentials = %q, %q, %q", email, username, password)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNormalizeProxyCredentialsAllowsAdminEmailOnly(t *testing.T) {
|
|
|
|
|
if _, _, _, err := normalizeProxyCredentials("admin@example.com", "", "correct horse battery staple"); err != nil {
|
|
|
|
|
t.Fatalf("empty optional username rejected: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNormalizePlatformRole(t *testing.T) {
|
|
|
|
|
if normalizePlatformRole("MODERATOR") != "moderator" {
|
|
|
|
|
t.Fatal("moderator role was not normalized")
|
|
|
|
|
}
|
|
|
|
|
if normalizePlatformRole("unknown") != "user" {
|
|
|
|
|
t.Fatal("unknown role was not reduced to user")
|
|
|
|
|
}
|
|
|
|
|
}
|