2026-09-01 01:34:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-13 21:11:53 +02:00
|
|
|
"context"
|
2026-09-01 01:34:48 +02:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
// The bulk of what auth_test.go used to cover (username/email normalization,
|
|
|
|
|
// session-cookie parsing, AuthGuard-unreachable error surfacing, AuthGuard
|
|
|
|
|
// error forwarding) is now sharedauth's own responsibility and covered by
|
|
|
|
|
// that package's tests (shared/backend/auth). What's left to test here is
|
|
|
|
|
// CollabTable's own tiny addition: the collabTableAuthStore adapter wired
|
|
|
|
|
// into newAuthService (see auth.go).
|
2026-09-01 01:34:48 +02:00
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
func TestCollabTableAuthStoreLoginRequiredAlwaysTrue(t *testing.T) {
|
|
|
|
|
store := collabTableAuthStore{}
|
|
|
|
|
required, err := store.LoginRequired(context.Background())
|
2026-09-01 01:34:48 +02:00
|
|
|
if err != nil {
|
2026-09-13 21:11:53 +02:00
|
|
|
t.Fatalf("LoginRequired() error = %v", err)
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
2026-09-13 21:11:53 +02:00
|
|
|
if !required {
|
|
|
|
|
t.Fatal("LoginRequired() = false, want true: CollabTable always requires a session when AuthGuard is configured")
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
func TestCollabTableAuthStoreSetLoginRequiredIsNoOp(t *testing.T) {
|
|
|
|
|
store := collabTableAuthStore{}
|
|
|
|
|
if err := store.SetLoginRequired(context.Background(), false); err != nil {
|
|
|
|
|
t.Fatalf("SetLoginRequired() error = %v, want nil (CollabTable has no such toggle)", err)
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
2026-09-13 21:11:53 +02:00
|
|
|
// Confirm it really didn't change anything observable.
|
|
|
|
|
required, err := store.LoginRequired(context.Background())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("LoginRequired() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if !required {
|
|
|
|
|
t.Fatal("SetLoginRequired(false) unexpectedly changed LoginRequired()'s answer")
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
func TestNewAuthServiceBuildsAService(t *testing.T) {
|
|
|
|
|
// newAuthService must not panic given a nil *DB and no AuthGuard URL
|
2026-09-14 14:12:34 +02:00
|
|
|
// ("pending" mode) -- the store adapter's IsSetupClaimed/TryClaimSetup/
|
|
|
|
|
// UnclaimSetup only get called once a real request comes in and AuthGuard
|
|
|
|
|
// is enabled, so construction alone must be safe.
|
|
|
|
|
if service := newAuthService(nil, "", false, false); service == nil {
|
|
|
|
|
t.Fatal("newAuthService() returned nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNewAuthServiceBuildsADisabledService(t *testing.T) {
|
|
|
|
|
// authDisabled=true must also not panic given a nil *DB and no AuthGuard
|
|
|
|
|
// URL -- this is the explicit auth-disabled mode (sharedauth.Disabled()).
|
|
|
|
|
if service := newAuthService(nil, "", false, true); service == nil {
|
2026-09-13 21:11:53 +02:00
|
|
|
t.Fatal("newAuthService() returned nil")
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
func TestExtractBearerRequiresBearerPrefix(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/lists", nil)
|
|
|
|
|
req.Header.Set("Authorization", "Bearer sometoken")
|
|
|
|
|
if got := extractBearer(req); got != "sometoken" {
|
|
|
|
|
t.Fatalf("extractBearer() = %q, want %q", got, "sometoken")
|
|
|
|
|
}
|
2026-09-01 01:34:48 +02:00
|
|
|
|
2026-09-13 21:11:53 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/api/lists", nil)
|
|
|
|
|
req.Header.Set("Authorization", "sometoken")
|
|
|
|
|
if got := extractBearer(req); got != "" {
|
|
|
|
|
t.Fatalf("extractBearer() = %q, want empty for a header missing the Bearer prefix", got)
|
2026-09-01 01:34:48 +02:00
|
|
|
}
|
|
|
|
|
}
|