package recharge import ( "context" "encoding/json" "errors" "strings" "testing" integrationbiz "kra/internal/biz/integration" ) type providerConfigRepo struct { configs []*integrationbiz.IntegrationConfig err error } func (r *providerConfigRepo) ListIntegrationConfigs(context.Context, string) ([]*integrationbiz.IntegrationConfig, error) { return r.configs, r.err } func (*providerConfigRepo) FindIntegrationConfig(context.Context, string, string) (*integrationbiz.IntegrationConfig, error) { return nil, errors.New("not implemented") } func (*providerConfigRepo) SaveIntegrationConfig(context.Context, *integrationbiz.IntegrationConfig) error { return errors.New("not implemented") } func (*providerConfigRepo) DeleteIntegrationConfig(context.Context, string, string) error { return errors.New("not implemented") } func TestProvidersOnlyReturnsEnabledSupportedPaymentConfigs(t *testing.T) { repo := &providerConfigRepo{configs: []*integrationbiz.IntegrationConfig{ {Kind: integrationbiz.IntegrationKindPayment, Provider: "wechat-v3", Enabled: true, Values: []byte(`{"api_key":"must-not-leak"}`)}, {Kind: integrationbiz.IntegrationKindPayment, Provider: "alipay", Enabled: false}, {Kind: integrationbiz.IntegrationKindPayment, Provider: "unknown-provider", Enabled: true}, {Kind: integrationbiz.IntegrationKindPayment, Provider: "wechat-v3", Enabled: true}, nil, }} integrations := integrationbiz.NewIntegrationConfigUsecase(repo, nil) service := NewService(nil, integrations) providers, err := service.Providers(context.Background()) if err != nil { t.Fatal(err) } if len(providers) != 1 || providers[0].ID != "wechat-v3" || providers[0].Name == "" { t.Fatalf("providers = %+v", providers) } encoded, err := json.Marshal(providers) if err != nil { t.Fatal(err) } if strings.Contains(string(encoded), "must-not-leak") || strings.Contains(string(encoded), "api_key") { t.Fatalf("provider response leaked integration config: %s", encoded) } } func TestProvidersPropagatesConfigReadFailure(t *testing.T) { want := errors.New("load failed") integrations := integrationbiz.NewIntegrationConfigUsecase(&providerConfigRepo{err: want}, nil) service := NewService(nil, integrations) if _, err := service.Providers(context.Background()); !errors.Is(err, want) { t.Fatalf("Providers() error = %v, want %v", err, want) } }