@@ -2393,3 +2393,124 @@ func TestService_ListGroupsByPrincipal(t *testing.T) {
23932393 })
23942394 }
23952395}
2396+
2397+ func TestService_ListProjectsByPrincipal (t * testing.T ) {
2398+ ctx := context .Background ()
2399+
2400+ userID := uuid .New ().String ()
2401+ patID := uuid .New ().String ()
2402+ orgA := uuid .New ().String ()
2403+ projDirect := uuid .New ().String ()
2404+ projPATScope := uuid .New ().String ()
2405+
2406+ t .Run ("user principal with NonInherited=true skips org-inheritance branch" , func (t * testing.T ) {
2407+ mp := mocks .NewPolicyService (t )
2408+ // Direct project policies fetch.
2409+ mp .EXPECT ().List (ctx , policy.Filter {
2410+ PrincipalID : userID ,
2411+ PrincipalType : schema .UserPrincipal ,
2412+ ResourceType : schema .ProjectNamespace ,
2413+ RolePermissions : schema .ProjectDirectVisibilityPerms ,
2414+ }).Return ([]policy.Policy {{ResourceID : projDirect }}, nil )
2415+ // Group expansion: principal has no groups (NonInherited=true on inner call).
2416+ mp .EXPECT ().List (ctx , policy.Filter {
2417+ PrincipalID : userID ,
2418+ PrincipalType : schema .UserPrincipal ,
2419+ ResourceType : schema .GroupNamespace ,
2420+ }).Return ([]policy.Policy {}, nil )
2421+ // NO org-inheritance fetch must happen — that's the NonInherited contract.
2422+
2423+ svc := membership .NewService (
2424+ slog .New (slog .NewTextHandler (io .Discard , nil )),
2425+ mp ,
2426+ mocks .NewRelationService (t ),
2427+ mocks .NewRoleService (t ),
2428+ mocks .NewOrgService (t ),
2429+ mocks .NewUserService (t ),
2430+ mocks .NewProjectService (t ),
2431+ mocks .NewGroupService (t ),
2432+ mocks .NewServiceuserService (t ),
2433+ mocks .NewAuditRecordRepository (t ),
2434+ )
2435+
2436+ got , err := svc .ListProjectsByPrincipal (
2437+ ctx ,
2438+ authenticate.Principal {ID : userID , Type : schema .UserPrincipal },
2439+ "" ,
2440+ true ,
2441+ )
2442+ assert .NoError (t , err )
2443+ assert .ElementsMatch (t , []string {projDirect }, got )
2444+ })
2445+
2446+ t .Run ("PAT principal — runs both user-side and PAT-side queries and intersects (unlike groups)" , func (t * testing.T ) {
2447+ mp := mocks .NewPolicyService (t )
2448+ // User-side: direct project policies + (no groups) + org-inheritance branch.
2449+ mp .EXPECT ().List (ctx , policy.Filter {
2450+ PrincipalID : userID ,
2451+ PrincipalType : schema .UserPrincipal ,
2452+ ResourceType : schema .ProjectNamespace ,
2453+ RolePermissions : schema .ProjectDirectVisibilityPerms ,
2454+ }).Return ([]policy.Policy {
2455+ {ResourceID : projDirect },
2456+ {ResourceID : projPATScope },
2457+ }, nil )
2458+ mp .EXPECT ().List (ctx , policy.Filter {
2459+ PrincipalID : userID ,
2460+ PrincipalType : schema .UserPrincipal ,
2461+ ResourceType : schema .GroupNamespace ,
2462+ }).Return ([]policy.Policy {}, nil )
2463+ mp .EXPECT ().List (ctx , policy.Filter {
2464+ PrincipalID : userID ,
2465+ PrincipalType : schema .UserPrincipal ,
2466+ ResourceType : schema .OrganizationNamespace ,
2467+ RolePermissions : schema .OrganizationProjectInheritPerms ,
2468+ }).Return ([]policy.Policy {}, nil )
2469+
2470+ // PAT-side: same fanout under PAT principal type — PAT only scopes projPATScope.
2471+ mp .EXPECT ().List (ctx , policy.Filter {
2472+ PrincipalID : patID ,
2473+ PrincipalType : schema .PATPrincipal ,
2474+ ResourceType : schema .ProjectNamespace ,
2475+ RolePermissions : schema .ProjectDirectVisibilityPerms ,
2476+ }).Return ([]policy.Policy {{ResourceID : projPATScope }}, nil )
2477+ mp .EXPECT ().List (ctx , policy.Filter {
2478+ PrincipalID : patID ,
2479+ PrincipalType : schema .PATPrincipal ,
2480+ ResourceType : schema .GroupNamespace ,
2481+ }).Return ([]policy.Policy {}, nil )
2482+ mp .EXPECT ().List (ctx , policy.Filter {
2483+ PrincipalID : patID ,
2484+ PrincipalType : schema .PATPrincipal ,
2485+ ResourceType : schema .OrganizationNamespace ,
2486+ RolePermissions : schema .OrganizationProjectInheritPerms ,
2487+ }).Return ([]policy.Policy {}, nil )
2488+
2489+ svc := membership .NewService (
2490+ slog .New (slog .NewTextHandler (io .Discard , nil )),
2491+ mp ,
2492+ mocks .NewRelationService (t ),
2493+ mocks .NewRoleService (t ),
2494+ mocks .NewOrgService (t ),
2495+ mocks .NewUserService (t ),
2496+ mocks .NewProjectService (t ),
2497+ mocks .NewGroupService (t ),
2498+ mocks .NewServiceuserService (t ),
2499+ mocks .NewAuditRecordRepository (t ),
2500+ )
2501+
2502+ got , err := svc .ListProjectsByPrincipal (
2503+ ctx ,
2504+ authenticate.Principal {
2505+ ID : userID ,
2506+ Type : schema .UserPrincipal ,
2507+ PAT : & pat.PAT {ID : patID , UserID : userID , OrgID : orgA },
2508+ },
2509+ "" ,
2510+ false ,
2511+ )
2512+ assert .NoError (t , err )
2513+ // PAT narrows: user sees [direct, patScope]; PAT sees [patScope]; intersect → [patScope].
2514+ assert .ElementsMatch (t , []string {projPATScope }, got )
2515+ })
2516+ }
0 commit comments