65 lines
3.3 KiB
Go
65 lines
3.3 KiB
Go
package payment
|
|
|
|
import (
|
|
integrationbiz "kra/internal/biz/integration"
|
|
bizpayment "kra/internal/biz/payment"
|
|
"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 := integrationbiz.ValidateIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.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 = integrationbiz.ValidateIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.PaymentDouyin, values); err != nil {
|
|
t.Fatalf("valid Douyin configuration rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidatePaymentConfigAcceptsProviderAliases(t *testing.T) {
|
|
err := integrationbiz.ValidateIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.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", bizpayment.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", bizpayment.PaymentPayPal, "webhook_id", map[string]any{"client_id": "client-id", "client_secret": "client-secret"}},
|
|
{"wechat v2 refund cert", bizpayment.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 := integrationbiz.ValidateIntegrationConfig(integrationbiz.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 := integrationbiz.DefaultIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.PaymentChinaums)
|
|
if err := integrationbiz.ValidateIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.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 := integrationbiz.ValidateIntegrationConfig(integrationbiz.IntegrationKindPayment, bizpayment.PaymentChinaums, values); err == nil {
|
|
t.Fatal("generic config with only identity/endpoints unexpectedly accepted")
|
|
}
|
|
}
|