- 每个商户限制最多 5 个 API 密钥,超限拒绝创建并提示 - API 密钥权限列改用中文标签展示,新增按用途命名提示 - 优化 API 密钥表格列宽,App Key 完整单行展示 - 补充密钥数量上限测试
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"affiliate_dash/internal/model"
|
|
)
|
|
|
|
func TestCreateAPIClientEnforcesPerMerchantLimit(t *testing.T) {
|
|
db := newServiceTestDB(t)
|
|
codec, err := NewSecretCodec("test-master-key")
|
|
if err != nil {
|
|
t.Fatalf("codec: %v", err)
|
|
}
|
|
svc := NewMerchantService(db, codec, NewTenantService(db))
|
|
merchant := model.Merchant{Code: "merchant-api-limit", Name: "限数商户", Status: model.MerchantStatusActive}
|
|
if err := db.Create(&merchant).Error; err != nil {
|
|
t.Fatalf("create merchant: %v", err)
|
|
}
|
|
for i := 0; i < MaxAPIClientsPerMerchant; i++ {
|
|
if _, err := svc.CreateAPIClient(merchant.ID, CreateAPIClientInput{
|
|
Name: fmt.Sprintf("key-%d", i),
|
|
Scopes: "products:read",
|
|
}, 1); err != nil {
|
|
t.Fatalf("create client %d: %v", i, err)
|
|
}
|
|
}
|
|
_, err = svc.CreateAPIClient(merchant.ID, CreateAPIClientInput{
|
|
Name: "overflow",
|
|
Scopes: "products:read",
|
|
}, 1)
|
|
if err == nil || !strings.Contains(err.Error(), fmt.Sprintf("最多可创建 %d 个", MaxAPIClientsPerMerchant)) {
|
|
t.Fatalf("expected per-merchant limit error, got %v", err)
|
|
}
|
|
}
|