test: 补充 API Key 归属与唯一性验证 (#41)

This commit is contained in:
ila
2026-08-24 12:24:35 +08:00
parent 98625ff9fe
commit 2871aba728
@@ -63,6 +63,22 @@ func TestGORMRepositoryMySQL(t *testing.T) {
if err := repository.Create(context.Background(), &key); err != nil {
return err
}
otherUser := model.User{
Email: fmt.Sprintf("api-key-other-%d@chorus.invalid", suffix), PasswordHash: "synthetic",
DisplayName: "Other API Key Test", Status: "active",
}
if err := tx.Create(&otherUser).Error; err != nil {
return err
}
if _, err := repository.ByIDForUser(context.Background(), key.ID, otherUser.ID); !errors.Is(err, ErrAPIKeyNotFound) {
return fmt.Errorf("cross-user API key read error = %v, want ErrAPIKeyNotFound", err)
}
duplicate := key
duplicate.ID = 0
duplicate.UserID = otherUser.ID
if err := repository.Create(context.Background(), &duplicate); err == nil {
return errors.New("duplicate public id was accepted")
}
loaded, err := repository.ByPublicID(context.Background(), publicID)
if err != nil || loaded.ID != key.ID || string(loaded.SecretHash) != string(hash[:]) {
return fmt.Errorf("load created api key: id=%d err=%w", loaded.ID, err)