Accounts are centralized in AuthGuard now, so this project's own "first launch, create the admin account" flow needs to know whether AuthGuard already has one - not just whether *this* project's local setup has been claimed. A fresh instance pointed at an AuthGuard that already has a superadmin (created via AuthGuard's own web UI, or a sibling project's setup) was incorrectly still showing its own create-account form. Both the status check and the setup/registration handler now also consult AuthGuard's GET /api/setup-status (via the vendored authclient's new SetupStatus method) before deciding: if AuthGuard already has an account, show the login screen instead, and reject a setup attempt that reaches the handler anyway (e.g. a stale page) the same way an already-claimed local setup is rejected. Also updates the vendored shared/backend/authclient to add PublicUser's totp_enabled field (AuthGuard has reported it for a while; the client just hadn't picked it up). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package authclient
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestSetupStatus(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
response any
|
|
want bool
|
|
}{
|
|
{name: "no accounts yet", response: map[string]bool{"setup_required": true}, want: true},
|
|
{name: "an account already exists", response: map[string]bool{"setup_required": false}, want: false},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.URL.Path != "/api/setup-status" {
|
|
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(tc.response)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := New(server.URL)
|
|
got, err := client.SetupStatus(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("SetupStatus() error = %v", err)
|
|
}
|
|
if got != tc.want {
|
|
t.Fatalf("SetupStatus() = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSetupStatusPropagatesTransportError(t *testing.T) {
|
|
client := New("http://127.0.0.1:0")
|
|
if _, err := client.SetupStatus(context.Background()); err == nil {
|
|
t.Fatal("SetupStatus() expected an error against an unreachable server, got nil")
|
|
}
|
|
}
|