droboticflow/internal/api/legacy_admin_retirement_test.go
eust-w 106235bb02 ✨ Remove Email field, complete phone-only account identity (ticket 05)
Contract step of the account-phone-login expand-migrate-contract rollout.
Phone is now the sole account identifier end to end:

- domain.UserRecord/CreateUserInput: delete Email, drop Phone's omitempty
  (required going forward).
- store: delete email_unique Mongo index; phone_unique is now non-sparse
  (unique, not sparse) since every account creation path requires a phone.
  Delete FindUserByEmail, ensureUserEmailAvailableLocked, and
  ErrDuplicateUserEmail (confirmed dead via grep - all real call sites were
  already migrated to the *Phone equivalents by tickets 01-03). Mongo
  duplicate-key fallback and the account-mutation-guard's stale-write
  snapshot/filter now key off Phone instead of Email.
- api: drop the now-impossible ErrDuplicateUserEmail branch in
  renderUserMutationError. The account-hierarchy audit endpoint and the
  supplier-deletion receipt struct both switch their identifying field from
  Email to Phone, since both are populated fresh from current account state
  (not frozen historical snapshots) - continuing to read a "email" key would
  silently go blank for every account created after this change.
- frontend: src/types.ts User.phone is now required (email removed);
  AccountHierarchyIssue.email -> .phone in services/api/users.ts and
  pages/Users.tsx to match the backend audit endpoint's renamed field.
- Rewrote the wide blast radius of Go test fixtures that constructed users
  by email across internal/api/*_test.go and internal/store/*_test.go,
  including the two dedicated memory-store uniqueness tests (merged into
  phone-only equivalents) and several opt-in "live" acceptance-fixture
  tests gated behind env vars for real Fanyu deployments.

Repo-wide grep of Go and TS/TSX for Email/email/邮箱 turned up no remaining
account-identifier references outside of a few harmless test-only cosmetic
labels (e.g. "Email Tenant" as an org display name in a seed helper) and
assertions that explicitly check "邮箱" text is absent from API responses.

go build/vet clean. go test ./internal/api/... ./internal/store/...
./internal/domain/... : 75 failures, identical test names to the
pre-ticket-05 baseline (diffed against a throwaway worktree at the prior
tip). npm run lint:web (tsc --noEmit) clean. npm run test:frontend:
599/586/13, matching the stated baseline exactly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 14:30:50 +08:00

73 lines
2.9 KiB
Go

package api
import (
"bytes"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"net/http"
"testing"
"time"
)
func TestLegacyOrganizationAdminRetiredByDefault(t *testing.T) {
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
t.Setenv("DROBOTICFLOW_ENABLE_LEGACY_ORGANIZATION_ADMIN", "")
ds := store.NewMemoryStore()
ctx := t.Context()
org, err := ds.EnsureDefaultOrganization(ctx)
if err != nil {
t.Fatal(err)
}
password := "retirement-test-password"
hash, err := hashPassword(password)
if err != nil {
t.Fatal(err)
}
old := domain.UserRecord{ID: "old-platform-admin", OrganizationID: org.ID, Phone: "13800009101", Role: domain.UserRoleAdmin, IsSuperAdmin: true, Status: domain.UserStatusActive, PasswordHash: hash}
ops := domain.UserRecord{ID: "business-admin", OrganizationID: org.ID, Phone: "13800009102", Role: domain.UserRoleAdmin, Status: domain.UserStatusActive, PasswordHash: hash}
for _, u := range []domain.UserRecord{old, ops} {
if err := ds.UpsertUser(ctx, u); err != nil {
t.Fatal(err)
}
}
token := "previously-issued-legacy-session"
expires := time.Now().Add(time.Hour)
if _, err := ds.CreateSession(ctx, domain.SessionRecord{UserID: old.ID, OrganizationID: org.ID, TokenHash: sessionTokenHash(token), ExpiresAt: expires.Format(time.RFC3339), ExpiresAtTime: expires}); err != nil {
t.Fatal(err)
}
h := NewHandler(ds, nil)
oldCookie := &http.Cookie{Name: sessionCookieName, Value: token}
for _, path := range []string{"/api/auth/me", "/api/admin/organizations", "/api/users"} {
r := serveJSONWithCookie(h, "GET", path, nil, oldCookie)
if r.Code != 401 {
t.Fatalf("old session %s: %d %s", path, r.Code, r.Body.String())
}
}
r := serveJSONWithCookie(h, "POST", "/api/auth/login", []byte(`{"phone":"13800009101","password":"retirement-test-password"}`), nil)
if r.Code != 401 {
t.Fatalf("correct legacy password admitted: %d", r.Code)
}
r = serveJSONWithCookie(h, "POST", "/api/auth/bootstrap-super-admin", []byte(`{"name":"Another root","phone":"13900009999","password":"retirement-test-password"}`), nil)
if r.Code != 410 {
t.Fatalf("legacy bootstrap admitted: %d", r.Code)
}
r = serveJSONWithCookie(h, "GET", "/api/auth/bootstrap-status", nil, nil)
if r.Code != 200 || !bytes.Contains(r.Body.Bytes(), []byte(`"bootstrapRequired":false`)) {
t.Fatalf("bootstrap UI exposed: %s", r.Body.String())
}
cookie := loginSession(t, h, ops.Phone, password)
r = serveJSONWithCookie(h, "GET", "/api/users", nil, cookie)
if r.Code != 200 {
t.Fatalf("business admin blocked: %d %s", r.Code, r.Body.String())
}
for _, method := range []string{"GET", "POST"} {
r = serveJSONWithCookie(h, method, "/api/admin/organizations", []byte(`{"name":"unauthorized","slug":"unauthorized"}`), cookie)
if r.Code != 410 {
t.Fatalf("retired organization endpoint %s: %d", method, r.Code)
}
}
if u, err := ds.GetUser(ctx, old.ID); err != nil || u == nil {
t.Fatal("legacy audit identity unexpectedly removed")
}
}