99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
package providerbuild_test
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/wangjia/pay/config"
|
|
"github.com/wangjia/pay/internal/accounts"
|
|
"github.com/wangjia/pay/internal/providerbuild"
|
|
)
|
|
|
|
// TestBuildRegistry 表驱动覆盖 BuildRegistry 的装配/跳过决策:据 accounts.Registry
|
|
// 的 enabled 账户 + 对应 env 凭证是否齐备,决定各渠道是否 Register——缺凭证只
|
|
// log+skip,绝不 fatal(允许只上线部分渠道)。
|
|
//
|
|
// alipay 的"凭证齐备→真注册"分支这里不覆盖(需要构造能通过
|
|
// x509.MarshalPKIXPublicKey/LoadAliPayPublicKey 的 RSA 密钥对,详见
|
|
// internal/provider/alipay/alipay_test.go 的 genKeys——挪来此处会让本测试文件
|
|
// 显著变重,且该构造逻辑本身已被 alipay 包自己的单测覆盖)。这里只验证 alipay
|
|
// 的"缺凭证→跳过、不 fatal、注册表里没有它"分支,以及它在混合场景里不会误伤
|
|
// 同批次里凭证齐备的其它渠道——这正是 BuildRegistry 装配决策本身要保证的行为。
|
|
func TestBuildRegistry(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
accounts []config.AccountConfig
|
|
env map[string]string
|
|
want []string // 期望注册的 Method() 集合
|
|
}{
|
|
{
|
|
name: "无任何 enabled 账户_空注册表",
|
|
accounts: nil,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "crypto_enabled_且凭证齐备_注册crypto",
|
|
accounts: []config.AccountConfig{
|
|
{AccountID: "cry-1", Channel: "crypto", Enabled: true, CredentialEnvPrefix: "t_cry"},
|
|
},
|
|
env: map[string]string{
|
|
"T_CRY_ADDRESS": "TWtest0000000000000000000000000000",
|
|
"T_CRY_TRONGRID_KEY": "k",
|
|
},
|
|
want: []string{"crypto"},
|
|
},
|
|
{
|
|
name: "alipay_enabled_但凭证不全_跳过不fatal_注册表无alipay",
|
|
accounts: []config.AccountConfig{
|
|
{AccountID: "ali-1", Channel: "alipay", Enabled: true, CredentialEnvPrefix: "t_ali"},
|
|
},
|
|
// 故意不设 T_ALI_APP_ID / _APP_PRIVATE_KEY / _ALIPAY_PUBLIC_KEY:
|
|
// 若 BuildRegistry 对此 fatal/panic,本测试直接挂掉,已是断言的一部分。
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "stripe_enabled_且凭证齐备_注册stripe",
|
|
accounts: []config.AccountConfig{
|
|
{AccountID: "st-1", Channel: "stripe", Enabled: true, CredentialEnvPrefix: "t_st"},
|
|
},
|
|
env: map[string]string{
|
|
"T_ST_SECRET_KEY": "sk_test_x",
|
|
"T_ST_WEBHOOK_SECRET": "whsec_x",
|
|
},
|
|
want: []string{"stripe"},
|
|
},
|
|
{
|
|
name: "混合_一个凭证齐备一个不全_只注册齐备的那个",
|
|
accounts: []config.AccountConfig{
|
|
{AccountID: "st-2", Channel: "stripe", Enabled: true, CredentialEnvPrefix: "t_st2"},
|
|
{AccountID: "ali-2", Channel: "alipay", Enabled: true, CredentialEnvPrefix: "t_ali2"},
|
|
},
|
|
env: map[string]string{
|
|
"T_ST2_SECRET_KEY": "sk_test_y",
|
|
"T_ST2_WEBHOOK_SECRET": "whsec_y",
|
|
// ali-2 故意留白凭证。
|
|
},
|
|
want: []string{"stripe"},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
for k, v := range tc.env {
|
|
t.Setenv(k, v)
|
|
}
|
|
acctReg := accounts.New(tc.accounts)
|
|
reg := providerbuild.BuildRegistry(acctReg)
|
|
|
|
got := reg.Methods()
|
|
sort.Strings(got)
|
|
want := append([]string(nil), tc.want...)
|
|
sort.Strings(want)
|
|
if !reflect.DeepEqual(got, want) && !(len(got) == 0 && len(want) == 0) {
|
|
t.Fatalf("Methods() = %v, want %v", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|