This commit is contained in:
1 parent
8d7c2f69ff
commit
a018f3da4c
18 files changed
+747
-46
No files matched your search
@@ -1,6 +1,69 @@
|
||||
package auth
|
||||
|
||||
import "testing"
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeProxyCredentials(t *testing.T) {
|
||||
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
|
||||
|
||||
Reference in new issue
Block a user