payment_order_jsapi_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/payment"
  6. )
  7. func TestUsesOfficialWxpayVisibleMethodDerivesFromEnabledProviderInstance(t *testing.T) {
  8. ctx := context.Background()
  9. client := newPaymentConfigServiceTestClient(t)
  10. _, err := client.PaymentProviderInstance.Create().
  11. SetProviderKey(payment.TypeWxpay).
  12. SetName("Official WeChat").
  13. SetConfig("{}").
  14. SetSupportedTypes("wxpay").
  15. SetEnabled(true).
  16. SetSortOrder(1).
  17. Save(ctx)
  18. if err != nil {
  19. t.Fatalf("create official wxpay instance: %v", err)
  20. }
  21. svc := &PaymentService{
  22. configService: &PaymentConfigService{entClient: client},
  23. }
  24. if !svc.usesOfficialWxpayVisibleMethod(ctx) {
  25. t.Fatal("expected official wxpay visible method to be detected from enabled provider instance")
  26. }
  27. }
  28. func TestUsesOfficialWxpayVisibleMethodRespectsConfiguredSourceWhenMultipleProvidersEnabled(t *testing.T) {
  29. tests := []struct {
  30. name string
  31. source string
  32. wantOfficial bool
  33. }{
  34. {
  35. name: "official source selected",
  36. source: VisibleMethodSourceOfficialWechat,
  37. wantOfficial: true,
  38. },
  39. {
  40. name: "easypay source selected",
  41. source: VisibleMethodSourceEasyPayWechat,
  42. wantOfficial: false,
  43. },
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. ctx := context.Background()
  48. client := newPaymentConfigServiceTestClient(t)
  49. _, err := client.PaymentProviderInstance.Create().
  50. SetProviderKey(payment.TypeWxpay).
  51. SetName("Official WeChat").
  52. SetConfig("{}").
  53. SetSupportedTypes("wxpay").
  54. SetEnabled(true).
  55. SetSortOrder(1).
  56. Save(ctx)
  57. if err != nil {
  58. t.Fatalf("create official wxpay instance: %v", err)
  59. }
  60. _, err = client.PaymentProviderInstance.Create().
  61. SetProviderKey(payment.TypeEasyPay).
  62. SetName("EasyPay WeChat").
  63. SetConfig("{}").
  64. SetSupportedTypes("wxpay").
  65. SetEnabled(true).
  66. SetSortOrder(2).
  67. Save(ctx)
  68. if err != nil {
  69. t.Fatalf("create easypay wxpay instance: %v", err)
  70. }
  71. svc := &PaymentService{
  72. configService: &PaymentConfigService{
  73. entClient: client,
  74. settingRepo: &paymentConfigSettingRepoStub{
  75. values: map[string]string{
  76. SettingPaymentVisibleMethodWxpaySource: tt.source,
  77. },
  78. },
  79. },
  80. }
  81. if got := svc.usesOfficialWxpayVisibleMethod(ctx); got != tt.wantOfficial {
  82. t.Fatalf("usesOfficialWxpayVisibleMethod() = %v, want %v", got, tt.wantOfficial)
  83. }
  84. })
  85. }
  86. }