| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package service
- import (
- "context"
- "testing"
- "github.com/Wei-Shaw/sub2api/internal/payment"
- )
- func TestUsesOfficialWxpayVisibleMethodDerivesFromEnabledProviderInstance(t *testing.T) {
- ctx := context.Background()
- client := newPaymentConfigServiceTestClient(t)
- _, err := client.PaymentProviderInstance.Create().
- SetProviderKey(payment.TypeWxpay).
- SetName("Official WeChat").
- SetConfig("{}").
- SetSupportedTypes("wxpay").
- SetEnabled(true).
- SetSortOrder(1).
- Save(ctx)
- if err != nil {
- t.Fatalf("create official wxpay instance: %v", err)
- }
- svc := &PaymentService{
- configService: &PaymentConfigService{entClient: client},
- }
- if !svc.usesOfficialWxpayVisibleMethod(ctx) {
- t.Fatal("expected official wxpay visible method to be detected from enabled provider instance")
- }
- }
- func TestUsesOfficialWxpayVisibleMethodRespectsConfiguredSourceWhenMultipleProvidersEnabled(t *testing.T) {
- tests := []struct {
- name string
- source string
- wantOfficial bool
- }{
- {
- name: "official source selected",
- source: VisibleMethodSourceOfficialWechat,
- wantOfficial: true,
- },
- {
- name: "easypay source selected",
- source: VisibleMethodSourceEasyPayWechat,
- wantOfficial: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- ctx := context.Background()
- client := newPaymentConfigServiceTestClient(t)
- _, err := client.PaymentProviderInstance.Create().
- SetProviderKey(payment.TypeWxpay).
- SetName("Official WeChat").
- SetConfig("{}").
- SetSupportedTypes("wxpay").
- SetEnabled(true).
- SetSortOrder(1).
- Save(ctx)
- if err != nil {
- t.Fatalf("create official wxpay instance: %v", err)
- }
- _, err = client.PaymentProviderInstance.Create().
- SetProviderKey(payment.TypeEasyPay).
- SetName("EasyPay WeChat").
- SetConfig("{}").
- SetSupportedTypes("wxpay").
- SetEnabled(true).
- SetSortOrder(2).
- Save(ctx)
- if err != nil {
- t.Fatalf("create easypay wxpay instance: %v", err)
- }
- svc := &PaymentService{
- configService: &PaymentConfigService{
- entClient: client,
- settingRepo: &paymentConfigSettingRepoStub{
- values: map[string]string{
- SettingPaymentVisibleMethodWxpaySource: tt.source,
- },
- },
- },
- }
- if got := svc.usesOfficialWxpayVisibleMethod(ctx); got != tt.wantOfficial {
- t.Fatalf("usesOfficialWxpayVisibleMethod() = %v, want %v", got, tt.wantOfficial)
- }
- })
- }
- }
|