droboticflow/internal/api/phone_validation_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

86 lines
2.7 KiB
Go

package api
import (
"context"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
// TestIsValidChinaMobilePhone covers the phone-format validation helper that
// backs account create/edit and login (net/mail email validation has been
// removed from those flows; phone is now the account identifier).
// It matches spec.md decision 3's regex exactly: ^1[3-9]\d{9}$.
func TestIsValidChinaMobilePhone(t *testing.T) {
valid := []string{
"13800000001",
"15912345678",
"19999999999",
" 13800000001 ", // trimmed before matching
}
for _, phone := range valid {
if !isValidChinaMobilePhone(phone) {
t.Errorf("expected %q to be a valid China mobile phone", phone)
}
}
invalid := []string{
"",
"1234567890", // 10 digits
"123456789012", // 12 digits
"12800000001", // second digit not in 3-9
"+8613800000001", // international prefix not supported
"010-12345678", // landline
"abcdefghijk",
"1380000000a",
}
for _, phone := range invalid {
if isValidChinaMobilePhone(phone) {
t.Errorf("expected %q to be rejected as an invalid phone", phone)
}
}
}
// TestValidateUserPhoneAvailable covers the uniqueness-check helper that
// backs account create/edit/login: available when unset or unused, available
// when it belongs to the current user, unavailable when another user
// already has it.
func TestValidateUserPhoneAvailable(t *testing.T) {
ctx := context.Background()
dataStore := store.NewMemoryStore()
srv := &server{store: dataStore}
org := domain.OrganizationRecord{ID: "org-1", Name: "Org", Slug: "org-1", Status: domain.OrganizationStatusActive}
if err := dataStore.UpsertOrganization(ctx, org); err != nil {
t.Fatalf("upsert organization: %v", err)
}
user, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
OrganizationID: org.ID,
Name: "User One",
Phone: "13900006001",
Role: domain.UserRoleAnnotator,
})
if err != nil {
t.Fatalf("create user: %v", err)
}
user.Phone = "13800000001"
if err := dataStore.UpsertUser(ctx, *user); err != nil {
t.Fatalf("set phone on user: %v", err)
}
if issue, err := srv.validateUserPhoneAvailable(ctx, "13800000099", ""); err != nil || issue != nil {
t.Fatalf("expected unused phone to be available, got issue=%#v err=%v", issue, err)
}
if issue, err := srv.validateUserPhoneAvailable(ctx, "13800000001", user.ID); err != nil || issue != nil {
t.Fatalf("expected own phone to remain available to the same user, got issue=%#v err=%v", issue, err)
}
issue, err := srv.validateUserPhoneAvailable(ctx, "13800000001", "")
if err != nil {
t.Fatalf("validate phone: %v", err)
}
if issue == nil || issue.Field != "phone" {
t.Fatalf("expected phone uniqueness issue for another user, got %#v", issue)
}
}