64 lines
3.0 KiB
Go
64 lines
3.0 KiB
Go
package payment
|
|
|
|
import (
|
|
"kra/internal/biz/system"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidatePaymentConfigRequiresDouyinAppIDWhenEnabled(t *testing.T) {
|
|
values := map[string]any{
|
|
"merchant_id": "merchant-douyin", "serial_no": "merchant-serial", "api_key": "01234567890123456789012345678901",
|
|
"private_key": "merchant-private-key", "platform_cert": "platform-public-key", "platform_serial_no": "platform-serial",
|
|
}
|
|
err := system.ValidateIntegrationConfig(system.IntegrationKindPayment, system.PaymentDouyin, values)
|
|
if err == nil || !strings.Contains(err.Error(), "app_id") {
|
|
t.Fatalf("missing app_id error = %v", err)
|
|
}
|
|
values["app_id"] = "douyin-app"
|
|
if err = system.ValidateIntegrationConfig(system.IntegrationKindPayment, system.PaymentDouyin, values); err != nil {
|
|
t.Fatalf("valid Douyin configuration rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidatePaymentConfigAcceptsProviderAliases(t *testing.T) {
|
|
err := validatePaymentConfig(system.PaymentDouyin, map[string]any{
|
|
"app_id": "douyin-app", "merchant_id": "merchant-douyin", "serial_no": "merchant-serial", "api_key": "01234567890123456789012345678901",
|
|
"private_key": "merchant-private-key", "platform_cert": "platform-public-key", "platform_cert_serial": "platform-serial",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("platform_cert_serial alias rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidatePaymentConfigProviderRules(t *testing.T) {
|
|
tests := []struct {
|
|
name, provider, want string
|
|
values map[string]any
|
|
}{
|
|
{"allinpay order type", system.PaymentAllinPay, "reqsn", map[string]any{"cus_id": "customer", "app_id": "app", "private_key": "private-key", "public_key": "public-key", "query_order_type": "payinfo"}},
|
|
{"paypal webhook", system.PaymentPayPal, "webhook_id", map[string]any{"client_id": "client-id", "client_secret": "client-secret"}},
|
|
{"wechat v2 refund cert", system.PaymentWechatV2, "client_cert", map[string]any{"app_id": "app", "merchant_id": "merchant", "mch_key": "key"}},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
err := system.ValidateIntegrationConfig(system.IntegrationKindPayment, test.provider, test.values)
|
|
if err == nil || !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidatePaymentConfigGenericRequiresRuntimeFields(t *testing.T) {
|
|
values := system.DefaultIntegrationConfig(system.IntegrationKindPayment, system.PaymentChinaums)
|
|
if err := system.ValidateIntegrationConfig(system.IntegrationKindPayment, system.PaymentChinaums, values); err == nil {
|
|
t.Fatal("empty generic payment config unexpectedly accepted")
|
|
}
|
|
values["app_id"], values["merchant_id"] = "app", "merchant"
|
|
values["create_url"], values["query_url"], values["refund_url"], values["app_key"] = "https://pay.test/create", "https://pay.test/query", "https://pay.test/refund", "secret"
|
|
if err := system.ValidateIntegrationConfig(system.IntegrationKindPayment, system.PaymentChinaums, values); err == nil {
|
|
t.Fatal("generic config with only identity/endpoints unexpectedly accepted")
|
|
}
|
|
}
|