fix(ci): align auth and payment verification tests

This commit is contained in:
IanShaw027
2026-04-22 02:32:53 +08:00
parent 6d51834a95
commit b13e34f831
21 changed files with 335 additions and 339 deletions

View File

@@ -4,7 +4,11 @@ package service
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"strconv"
"testing"
"time"
@@ -52,6 +56,28 @@ func newWebhookProviderTestLoadBalancer(client *dbent.Client) payment.LoadBalanc
return payment.NewDefaultLoadBalancer(client, []byte(webhookProviderTestEncryptionKey))
}
func encryptValidWebhookWxpayConfig(t *testing.T, suffix string) string {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
privDER, err := x509.MarshalPKCS8PrivateKey(key)
require.NoError(t, err)
pubDER, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
require.NoError(t, err)
return encryptWebhookProviderConfig(t, map[string]string{
"appId": "wx-app-" + suffix,
"mchId": "mch-" + suffix,
"privateKey": string(pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})),
"apiV3Key": webhookProviderTestEncryptionKey,
"publicKey": string(pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: pubDER})),
"publicKeyId": "public-key-id-" + suffix,
"certSerial": "cert-serial-" + suffix,
})
}
func TestGetOrderProviderInstanceResolvesUniqueLegacyProviderKey(t *testing.T) {
ctx := context.Background()
client := newPaymentConfigServiceTestClient(t)
@@ -275,24 +301,8 @@ func TestGetOrderProviderInstanceRejectsMissingSnapshotInstanceWithoutLegacyFall
func TestGetWebhookProviderRejectsAmbiguousRegistryFallback(t *testing.T) {
ctx := context.Background()
client := newPaymentConfigServiceTestClient(t)
wxpayConfigA := encryptWebhookProviderConfig(t, map[string]string{
"appId": "wx-app-a",
"mchId": "mch-a",
"privateKey": "private-key-a",
"apiV3Key": webhookProviderTestEncryptionKey,
"publicKey": "public-key-a",
"publicKeyId": "public-key-id-a",
"certSerial": "cert-serial-a",
})
wxpayConfigB := encryptWebhookProviderConfig(t, map[string]string{
"appId": "wx-app-b",
"mchId": "mch-b",
"privateKey": "private-key-b",
"apiV3Key": webhookProviderTestEncryptionKey,
"publicKey": "public-key-b",
"publicKeyId": "public-key-id-b",
"certSerial": "cert-serial-b",
})
wxpayConfigA := encryptValidWebhookWxpayConfig(t, "a")
wxpayConfigB := encryptValidWebhookWxpayConfig(t, "b")
_, err := client.PaymentProviderInstance.Create().
SetProviderKey(payment.TypeWxpay).
SetName("wxpay-a").
@@ -442,24 +452,8 @@ func TestGetWebhookProviderUsesProviderSnapshotBeforeWxpayFallback(t *testing.T)
Save(ctx)
require.NoError(t, err)
wxpayConfigA := encryptWebhookProviderConfig(t, map[string]string{
"appId": "wx-app-snapshot-a",
"mchId": "mch-snapshot-a",
"privateKey": "private-key-snapshot-a",
"apiV3Key": webhookProviderTestEncryptionKey,
"publicKey": "public-key-snapshot-a",
"publicKeyId": "public-key-id-snapshot-a",
"certSerial": "cert-serial-snapshot-a",
})
wxpayConfigB := encryptWebhookProviderConfig(t, map[string]string{
"appId": "wx-app-snapshot-b",
"mchId": "mch-snapshot-b",
"privateKey": "private-key-snapshot-b",
"apiV3Key": webhookProviderTestEncryptionKey,
"publicKey": "public-key-snapshot-b",
"publicKeyId": "public-key-id-snapshot-b",
"certSerial": "cert-serial-snapshot-b",
})
wxpayConfigA := encryptValidWebhookWxpayConfig(t, "snapshot-a")
wxpayConfigB := encryptValidWebhookWxpayConfig(t, "snapshot-b")
instA, err := client.PaymentProviderInstance.Create().
SetProviderKey(payment.TypeWxpay).
SetName("wxpay-snapshot-a").