package api import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "strings" "sync/atomic" "testing" "time" "droboticflow/internal/domain" "droboticflow/internal/store" ) type authReadCounter struct { store.Store sessions, users, suppliers, organizations atomic.Int64 } func (s *authReadCounter) GetSessionByTokenHash(ctx context.Context, tokenHash string) (*domain.SessionRecord, error) { s.sessions.Add(1) return s.Store.GetSessionByTokenHash(ctx, tokenHash) } func (s *authReadCounter) GetUser(ctx context.Context, id string) (*domain.UserRecord, error) { s.users.Add(1) return s.Store.GetUser(ctx, id) } func (s *authReadCounter) GetSupplier(ctx context.Context, id string) (*domain.SupplierRecord, error) { s.suppliers.Add(1) return s.Store.GetSupplier(ctx, id) } func (s *authReadCounter) GetOrganization(ctx context.Context, id string) (*domain.OrganizationRecord, error) { s.organizations.Add(1) return s.Store.GetOrganization(ctx, id) } func newAuthCacheFixture(t *testing.T) (*authReadCounter, *server, string, string) { t.Helper() ctx := context.Background() dataStore := store.NewMemoryStore() if _, err := dataStore.EnsureDefaultOrganization(ctx); err != nil { t.Fatal(err) } user := domain.UserRecord{ID: "auth-cache-user", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive, Name: "缓存管理员", Phone: "13800000001"} if err := dataStore.UpsertUser(ctx, user); err != nil { t.Fatal(err) } counter := &authReadCounter{Store: dataStore} instance := &server{store: counter, requireSessionAuth: true} token := "auth-cache-session-token" expiresAt := time.Now().Add(time.Hour) if _, err := dataStore.CreateSession(ctx, domain.SessionRecord{ UserID: user.ID, TokenHash: sessionTokenHash(token), ExpiresAt: expiresAt.Format(time.RFC3339), ExpiresAtTime: expiresAt, CreatedAt: time.Now().Format(time.RFC3339), }); err != nil { t.Fatal(err) } invalidateAuthCache() return counter, instance, token, user.ID } func authedRequest(instance *server, path string, token string) *httptest.ResponseRecorder { request := httptest.NewRequest(http.MethodGet, path, nil) request.AddCookie(&http.Cookie{Name: sessionCookieName, Value: token}) recorder := httptest.NewRecorder() instance.sessionAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { if _, ok := currentPrincipal(request); ok { writer.WriteHeader(http.StatusOK) return } writer.WriteHeader(http.StatusUnauthorized) })).ServeHTTP(recorder, request) return recorder } func TestAuthenticateRequestServesWarmRequestsFromCache(t *testing.T) { counter, instance, token, userID := newAuthCacheFixture(t) first := authedRequest(instance, "/api/users", token) if first.Code != http.StatusOK { t.Fatalf("first request status=%d", first.Code) } coldCalls := int(counter.sessions.Load() + counter.users.Load() + counter.organizations.Load() + counter.suppliers.Load()) if coldCalls < 2 || counter.sessions.Load() != 1 { t.Fatalf("cold path must hit the store, got sessions=%d users=%d orgs=%d suppliers=%d", counter.sessions.Load(), counter.users.Load(), counter.organizations.Load(), counter.suppliers.Load()) } for index := 0; index < 3; index++ { warm := authedRequest(instance, "/api/users", token) if warm.Code != http.StatusOK { t.Fatalf("warm request %d status=%d", index, warm.Code) } } warmCalls := int(counter.sessions.Load() + counter.users.Load() + counter.organizations.Load() + counter.suppliers.Load()) if warmCalls != coldCalls { t.Fatalf("cache hit must perform zero auth store reads: cold=%d warm=%d (sessions=%d users=%d orgs=%d suppliers=%d)", coldCalls, warmCalls, counter.sessions.Load(), counter.users.Load(), counter.organizations.Load(), counter.suppliers.Load()) } // Token-level invalidation (logout) takes effect immediately. invalidateAuthCacheForToken(sessionTokenHash(token)) after := authedRequest(instance, "/api/users", token) if after.Code != http.StatusOK { t.Fatalf("post-invalidation status=%d", after.Code) } if counter.sessions.Load() != 2 { t.Fatalf("token invalidation must force a fresh session read, sessions=%d", counter.sessions.Load()) } // User-level invalidation (role/supplier/password changes) evicts all of // the user's sessions, including other live tokens. invalidateAuthCacheForUser(userID) afterUser := authedRequest(instance, "/api/users", token) if afterUser.Code != http.StatusOK || counter.sessions.Load() != 3 { t.Fatalf("user invalidation must force a fresh session read, status=%d sessions=%d", afterUser.Code, counter.sessions.Load()) } } func TestLogoutHandlerInvalidatesCachedPrincipal(t *testing.T) { counter, instance, token, _ := newAuthCacheFixture(t) if response := authedRequest(instance, "/api/users", token); response.Code != http.StatusOK { t.Fatalf("warm-up status=%d", response.Code) } if counter.sessions.Load() != 1 { t.Fatalf("expected one cold session read, got %d", counter.sessions.Load()) } request := httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil) request.AddCookie(&http.Cookie{Name: sessionCookieName, Value: token}) recorder := httptest.NewRecorder() instance.handleAuthLogout(recorder, request) if recorder.Code != http.StatusNoContent { t.Fatalf("logout status=%d", recorder.Code) } // The session is deleted, so the next request must not authenticate even // though the cache would still be warm without invalidation (that would // wrongly yield 200 from the stale principal). response := authedRequest(instance, "/api/users", token) if response.Code != http.StatusUnauthorized { t.Fatalf("post-logout request must be unauthorized, got %d", response.Code) } if counter.sessions.Load() != 3 { t.Fatalf("logout must invalidate the cached principal, sessions=%d", counter.sessions.Load()) } } func TestUsersListLimitAndSearch(t *testing.T) { ctx := context.Background() dataStore := store.NewMemoryStore() if _, err := dataStore.EnsureDefaultOrganization(ctx); err != nil { t.Fatal(err) } admin := domain.UserRecord{ID: "users-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive} if err := dataStore.UpsertUser(ctx, admin); err != nil { t.Fatal(err) } // Another tenant's user must stay invisible regardless of pagination. outsider := domain.UserRecord{ID: "users-outsider", OrganizationID: "org-other", Role: domain.UserRoleAdmin, Status: domain.UserStatusActive, Name: "外租户用户"} if err := dataStore.UpsertUser(ctx, outsider); err != nil { t.Fatal(err) } for index := 0; index < 250; index++ { name, phone := "运营成员", fmt.Sprintf("138%08d", index) if index%50 == 0 { name = "工程师-" + string(rune('A'+index/50)) phone = fmt.Sprintf("137%08d", index) } user := domain.UserRecord{ ID: domain.NewID("user"), OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive, Name: name, Phone: phone, } if err := dataStore.UpsertUser(ctx, user); err != nil { t.Fatal(err) } } handler := NewHandler(dataStore, nil) principal := &authenticatedPrincipal{User: admin, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}} requestWith := func(target string) *httptest.ResponseRecorder { request := httptest.NewRequest(http.MethodGet, target, nil) request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, principal)) recorder := httptest.NewRecorder() handler.ServeHTTP(recorder, request) return recorder } // Default conservative cap: 251 visible users truncated to 200. defaultResponse := requestWith("/api/users") if defaultResponse.Code != http.StatusOK { t.Fatalf("default status=%d body=%s", defaultResponse.Code, defaultResponse.Body.String()) } var payload struct { Items []domain.UserRecord `json:"items"` HasMore bool `json:"hasMore"` } if err := json.Unmarshal(defaultResponse.Body.Bytes(), &payload); err != nil { t.Fatal(err) } if len(payload.Items) != 200 || !payload.HasMore { t.Fatalf("default cap items=%d hasMore=%v", len(payload.Items), payload.HasMore) } for _, item := range payload.Items { if item.OrganizationID != domain.DefaultOrganizationID { t.Fatalf("tenant isolation broken by pagination: %s", item.ID) } } // Explicit limit and q search combine; small result sets omit hasMore. // "137" matches the five 工程师-* phones plus the one 13800000137 member. search := requestWith("/api/users?limit=10&q=137") if search.Code != http.StatusOK { t.Fatalf("search status=%d", search.Code) } payload.HasMore = false // fresh decode of a body without hasMore must stay false if err := json.Unmarshal(search.Body.Bytes(), &payload); err != nil { t.Fatal(err) } if len(payload.Items) != 6 || payload.HasMore { t.Fatalf("search items=%d hasMore=%v", len(payload.Items), payload.HasMore) } engineers := 0 for _, item := range payload.Items { if !strings.Contains(item.Phone, "137") { t.Fatalf("search matched wrong user %s phone=%s", item.ID, item.Phone) } if strings.HasPrefix(item.Phone, "137") { engineers++ } } if engineers != 5 { t.Fatalf("expected 5 engineer matches, got %d", engineers) } // Invalid limit is rejected; legacy no-parameter shape stays {"items"}. bad := requestWith("/api/users?limit=0") if bad.Code != http.StatusBadRequest { t.Fatalf("invalid limit status=%d", bad.Code) } } func TestAuthCacheConcurrentAccess(t *testing.T) { counter, instance, token, userID := newAuthCacheFixture(t) // Concurrent readers and invalidators must not race or deadlock; the // final request must still authenticate correctly. done := make(chan struct{}) for index := 0; index < 8; index++ { go func(i int) { defer func() { done <- struct{}{} }() for round := 0; round < 25; round++ { switch i % 3 { case 0: _ = authedRequest(instance, "/api/users", token) case 1: invalidateAuthCacheForUser(userID) default: invalidateAuthCacheForToken(sessionTokenHash(token)) } } }(index) } for index := 0; index < 8; index++ { <-done } final := authedRequest(instance, "/api/users", token) if final.Code != http.StatusOK { t.Fatalf("final request status=%d", final.Code) } _ = counter }