@@ -551,6 +551,64 @@ func TestSQLiteUsageStatsBaselineHasBillingColumns(t *testing.T) {
551551 }
552552}
553553
554+ func TestDeleteAccountGroupDoesNotBroadenScopedAPIKey (t * testing.T ) {
555+ dbPath := filepath .Join (t .TempDir (), "codex2api.db" )
556+
557+ db , err := New ("sqlite" , dbPath )
558+ if err != nil {
559+ t .Fatalf ("New(sqlite) 返回错误: %v" , err )
560+ }
561+ defer db .Close ()
562+
563+ ctx := context .Background ()
564+ groupA , err := db .CreateAccountGroup (ctx , "Group A" , "" , "#2563eb" , 0 )
565+ if err != nil {
566+ t .Fatalf ("CreateAccountGroup A 返回错误: %v" , err )
567+ }
568+ groupB , err := db .CreateAccountGroup (ctx , "Group B" , "" , "#16a34a" , 1 )
569+ if err != nil {
570+ t .Fatalf ("CreateAccountGroup B 返回错误: %v" , err )
571+ }
572+
573+ keyOnlyA , err := db .InsertAPIKeyWithOptions (ctx , APIKeyInput {
574+ Name : "Only A" ,
575+ Key : "sk-only-a-1234567890" ,
576+ AllowedGroupIDs : []int64 {groupA },
577+ })
578+ if err != nil {
579+ t .Fatalf ("InsertAPIKeyWithOptions only-a 返回错误: %v" , err )
580+ }
581+ keyAB , err := db .InsertAPIKeyWithOptions (ctx , APIKeyInput {
582+ Name : "A and B" ,
583+ Key : "sk-a-b-1234567890" ,
584+ AllowedGroupIDs : []int64 {groupA , groupB },
585+ })
586+ if err != nil {
587+ t .Fatalf ("InsertAPIKeyWithOptions a-b 返回错误: %v" , err )
588+ }
589+
590+ if err := db .DeleteAccountGroup (ctx , groupA , true ); err != nil {
591+ t .Fatalf ("DeleteAccountGroup 返回错误: %v" , err )
592+ }
593+
594+ rows , err := db .ListAPIKeys (ctx )
595+ if err != nil {
596+ t .Fatalf ("ListAPIKeys 返回错误: %v" , err )
597+ }
598+
599+ got := make (map [int64 ][]int64 )
600+ for _ , row := range rows {
601+ got [row .ID ] = row .AllowedGroupIDs
602+ }
603+
604+ if actual := got [keyOnlyA ]; len (actual ) != 1 || actual [0 ] != groupA {
605+ t .Fatalf ("keyOnlyA allowed groups = %v, want stale [%d] to preserve deny-all semantics" , actual , groupA )
606+ }
607+ if actual := got [keyAB ]; len (actual ) != 1 || actual [0 ] != groupB {
608+ t .Fatalf ("keyAB allowed groups = %v, want [%d]" , actual , groupB )
609+ }
610+ }
611+
554612func TestUsageLogsPersistEffectiveModel (t * testing.T ) {
555613 dbPath := filepath .Join (t .TempDir (), "codex2api.db" )
556614
0 commit comments