fix: add ShouldFail to Square MockClient for testing payment error paths
This commit is contained in:
@@ -30,6 +30,7 @@ type MockClient struct {
|
||||
refunds map[string]*RefundResult
|
||||
completed map[string]*PaymentResult
|
||||
HoldCheckouts bool
|
||||
ShouldFail bool // if true, CreatePayment/RefundPayment return errors for testing error paths
|
||||
}
|
||||
|
||||
type devProdClient struct{}
|
||||
@@ -80,6 +81,9 @@ func NewDevClient() SquareClient {
|
||||
}
|
||||
|
||||
func (m *MockClient) CreatePayment(ctx context.Context, req CreatePaymentReq) (*PaymentResult, error) {
|
||||
if m.ShouldFail {
|
||||
return nil, fmt.Errorf("mock: payment declined (simulated failure)")
|
||||
}
|
||||
log.Printf("[SQUARE-MOCK] CreatePayment: amount=%d, reference=%s", req.Amount, req.ReferenceID)
|
||||
mockSleep(1 * time.Second)
|
||||
|
||||
@@ -183,6 +187,9 @@ func (m *MockClient) GetCheckout(ctx context.Context, checkoutID string) (*Payme
|
||||
}
|
||||
|
||||
func (m *MockClient) RefundPayment(ctx context.Context, req RefundPaymentReq) (*RefundResult, error) {
|
||||
if m.ShouldFail {
|
||||
return nil, fmt.Errorf("mock: refund declined (simulated failure)")
|
||||
}
|
||||
log.Printf("[SQUARE-MOCK] RefundPayment: payment=%s, amount=%d", req.PaymentID, req.Amount)
|
||||
mockSleep(1 * time.Second)
|
||||
|
||||
|
||||
@@ -361,6 +361,49 @@ func TestDevClient_CreateCardOnFileRaw_UnknownBrand(t *testing.T) {
|
||||
assert.True(t, card.IsDefault)
|
||||
}
|
||||
|
||||
func TestCreatePayment_ShouldFail(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
client.ShouldFail = true
|
||||
|
||||
ctx := context.Background()
|
||||
req := CreatePaymentReq{
|
||||
Amount: 5000,
|
||||
Currency: "GBP",
|
||||
SourceID: "cnon:test-card",
|
||||
IdempotencyKey: "test-key-fail",
|
||||
ReferenceID: "booking-fail",
|
||||
}
|
||||
|
||||
result, err := client.CreatePayment(ctx, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when ShouldFail is true, got nil")
|
||||
}
|
||||
if result != nil {
|
||||
t.Errorf("expected nil result, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRefundPayment_ShouldFail(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
client.ShouldFail = true
|
||||
|
||||
ctx := context.Background()
|
||||
req := RefundPaymentReq{
|
||||
PaymentID: "pay_mock_fail",
|
||||
Amount: 5000,
|
||||
IdempotencyKey: "refund-key-fail",
|
||||
Reason: "simulated failure",
|
||||
}
|
||||
|
||||
result, err := client.RefundPayment(ctx, req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when ShouldFail is true, got nil")
|
||||
}
|
||||
if result != nil {
|
||||
t.Errorf("expected nil result, got %+v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevClient_ConcurrentPayments(t *testing.T) {
|
||||
client := NewDevClient().(*MockClient)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user