fix(auth): 停用/删除分组后阻断 API Key

This commit is contained in:
wucm667
2026-05-20 15:52:00 +08:00
parent 7ec61eb2f5
commit 22ff1acde3
7 changed files with 169 additions and 18 deletions
@@ -80,7 +80,7 @@ func TestUserRepository_RemoveGroupFromAllowedGroups_RemovesAllOccurrences(t *te
require.NotContains(t, u2After.AllowedGroups, targetGroup.ID)
}
func TestGroupRepository_DeleteCascade_RemovesAllowedGroupsAndClearsApiKeys(t *testing.T) {
func TestGroupRepository_DeleteCascade_PreservesApiKeyGroupID(t *testing.T) {
ctx := context.Background()
tx := testEntTx(t)
entClient := tx.Client()
@@ -138,8 +138,10 @@ func TestGroupRepository_DeleteCascade_RemovesAllowedGroupsAndClearsApiKeys(t *t
require.NotContains(t, uAfter.AllowedGroups, targetGroup.ID)
require.Contains(t, uAfter.AllowedGroups, otherGroup.ID)
// API keys bound to the deleted group should have group_id cleared.
// API keys keep their group_id so auth can reject keys bound to a deleted group.
keyAfter, err := apiKeyRepo.GetByID(ctx, key.ID)
require.NoError(t, err)
require.Nil(t, keyAfter.GroupID)
require.NotNil(t, keyAfter.GroupID)
require.Equal(t, targetGroup.ID, *keyAfter.GroupID)
require.Nil(t, keyAfter.Group)
}