Admin account-management UI and API now validate/enforce phone instead of email on create/edit. Production Fanyu data migration deferred to deployment time per spec decision 7 — not executed here. Merge conflict resolution + gap fixes found while reconciling 02 and 04: - src/types.ts / UserContext.tsx / users.ts: kept Email required + Phone optional (matches current migration state — login still uses email until ticket 03); createUser/updateCurrentUser signatures reconciled to the versions that are actually wired to real backend behavior. - internal/api/project_members.go: projectMemberView never exposed Phone (ticket 04's frontend rename to userPhone had nothing to read). Renamed UserEmail->UserPhone and populated it from user.Phone. - internal/store/memory.go + mongo.go: ListProjectMemberUsers's field whitelist projection dropped Phone entirely (pre-existed the Phone field, never updated) — same root cause as project_members.go, one layer down. - internal/api/auth.go: handleCurrentUserProfileUpdate/ currentUserProfilePatch still only supported Email; self-service profile edit silently no-opped on phone. Wired to phone validation mirroring the ticket-02 admin create/edit pattern. - Updated 3 tests whose expectations depended on the self-profile-update gap above (TestSessionAuthTenantIsolationAndRolePermissions, TestUserEmailUniquenessValidation, TestProjectMemberDisplayScopedBatch) to phone; verified full internal/api suite back to the 75-failure baseline (byte-identical failing test names) and frontend suite at 599/586/13 baseline. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
122 lines
4.0 KiB
Go
122 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type memberDisplayStore struct {
|
|
store.Store
|
|
ids []string
|
|
calls int
|
|
fail bool
|
|
}
|
|
|
|
func (s *memberDisplayStore) ListProjectMemberUsers(ctx context.Context, org string, ids []string) ([]domain.UserRecord, error) {
|
|
s.calls++
|
|
s.ids = append([]string{}, ids...)
|
|
if s.fail {
|
|
return nil, errors.New("directory unavailable")
|
|
}
|
|
return s.Store.ListProjectMemberUsers(ctx, org, ids)
|
|
}
|
|
|
|
func TestProjectMemberDisplayScopedBatch(t *testing.T) {
|
|
ctx := context.Background()
|
|
ds := store.NewMemoryStore()
|
|
project, err := ds.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: "display-org", Name: "Display"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, u := range []domain.UserRecord{
|
|
{ID: "a", OrganizationID: "display-org", Name: "成员甲", Phone: "13800000011", PasswordHash: "never-return", Status: domain.UserStatusActive},
|
|
{ID: "b", OrganizationID: "display-org", Name: "成员乙", Phone: "13800000012", Status: domain.UserStatusDisabled},
|
|
{ID: "foreign", OrganizationID: "other-org", Name: "禁止泄露", Phone: "13800000013"},
|
|
} {
|
|
if err := ds.UpsertUser(ctx, u); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, id := range []string{"a", "b", "missing", "foreign"} {
|
|
supplier := "supplier-a"
|
|
if id == "b" {
|
|
supplier = "supplier-b"
|
|
}
|
|
if err := ds.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "m-" + id, OrganizationID: "display-org", ProjectID: project.ID, UserID: id, SupplierID: supplier, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
wrapped := &memberDisplayStore{Store: ds}
|
|
server := &server{store: wrapped}
|
|
request := func(user domain.UserRecord) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodGet, "/api/projects/"+project.ID+"/members", nil)
|
|
rc := chi.NewRouteContext()
|
|
rc.URLParams.Add("id", project.ID)
|
|
c := context.WithValue(req.Context(), chi.RouteCtxKey, rc)
|
|
c = context.WithValue(c, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: "display-org"}})
|
|
rec := httptest.NewRecorder()
|
|
server.handleProjectMembersList(rec, req.WithContext(c))
|
|
return rec
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
user domain.UserRecord
|
|
count int
|
|
}{
|
|
{"admin", domain.UserRecord{ID: "admin", Role: domain.UserRoleAdmin}, 4},
|
|
{"supplier", domain.UserRecord{ID: "manager", Role: domain.UserRoleSupplierManager, SupplierID: "supplier-a"}, 3},
|
|
{"ordinary", domain.UserRecord{ID: "a", Role: domain.UserRoleAnnotator}, 1},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
wrapped.calls = 0
|
|
rec := request(tc.user)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
var body struct {
|
|
Items []projectMemberView `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(body.Items) != tc.count || len(wrapped.ids) != tc.count || wrapped.calls != 1 {
|
|
t.Fatalf("items=%d ids=%v calls=%d", len(body.Items), wrapped.ids, wrapped.calls)
|
|
}
|
|
for _, m := range body.Items {
|
|
switch m.UserID {
|
|
case "a":
|
|
if m.UserName != "成员甲" || m.UserPhone != "13800000011" || m.UserStatus != "active" {
|
|
t.Fatalf("display %+v", m)
|
|
}
|
|
case "b":
|
|
if m.UserStatus != "disabled" || m.UserName != "成员乙" {
|
|
t.Fatalf("disabled %+v", m)
|
|
}
|
|
default:
|
|
if m.UserStatus != "missing" || m.UserName != "" || m.UserPhone != "" {
|
|
t.Fatalf("missing/foreign %+v", m)
|
|
}
|
|
}
|
|
}
|
|
for _, private := range []string{"passwordHash", "never-return", "禁止泄露", "13800000013"} {
|
|
if strings.Contains(rec.Body.String(), private) {
|
|
t.Fatalf("leaked %s", private)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
wrapped.fail = true
|
|
if rec := request(domain.UserRecord{ID: "admin", Role: domain.UserRoleAdmin}); rec.Code != 500 {
|
|
t.Fatalf("directory failure must fail visibly: %d", rec.Code)
|
|
}
|
|
}
|