86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package paymentsvc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/paymentapi"
|
|
)
|
|
|
|
type testProcessor struct {
|
|
entered chan SaleRequest
|
|
release chan struct{}
|
|
saleCalls atomic.Int32
|
|
preauthCalls atomic.Int32
|
|
validation func(SaleRequest) error
|
|
}
|
|
|
|
func (p *testProcessor) ValidatePreauth(req SaleRequest) error {
|
|
if p.validation != nil {
|
|
return p.validation(req)
|
|
}
|
|
return nil
|
|
}
|
|
func (p *testProcessor) Sale(_ context.Context, req SaleRequest, _ StatusHandler) (*Result, error) {
|
|
p.saleCalls.Add(1)
|
|
if p.entered != nil {
|
|
p.entered <- req
|
|
}
|
|
if p.release != nil {
|
|
<-p.release
|
|
}
|
|
return &Result{Terminal: paymentapi.Result{Outcome: "approved", HTTPStatus: 200}}, nil
|
|
}
|
|
func (p *testProcessor) Preauth(context.Context, SaleRequest, StatusHandler) (*Result, error) {
|
|
p.preauthCalls.Add(1)
|
|
return &Result{Terminal: paymentapi.Result{Outcome: "approved", HTTPStatus: 200}}, nil
|
|
}
|
|
|
|
func TestSaleAndPreauthShareAdmission(t *testing.T) {
|
|
p := &testProcessor{entered: make(chan SaleRequest, 1), release: make(chan struct{})}
|
|
s := NewService(p)
|
|
done := make(chan error, 1)
|
|
go func() { _, err := s.Sale(context.Background(), SaleRequest{Amount: 100}, nil); done <- err }()
|
|
<-p.entered
|
|
if _, err := s.Preauth(context.Background(), SaleRequest{Amount: 50}, nil); !errors.Is(err, ErrPaymentInProgress) {
|
|
t.Errorf("Preauth(during Sale) error=%v, want ErrPaymentInProgress", err)
|
|
}
|
|
close(p.release)
|
|
if err := <-done; err != nil {
|
|
t.Fatalf("Sale() error=%v, want nil", err)
|
|
}
|
|
if _, err := s.Preauth(context.Background(), SaleRequest{Amount: 50}, nil); err != nil {
|
|
t.Errorf("Preauth(after Sale) error=%v, want nil", err)
|
|
}
|
|
if p.saleCalls.Load() != 1 || p.preauthCalls.Load() != 1 {
|
|
t.Errorf("Provider calls=%d/%d, want Sale=1/PREAUTH=1", p.saleCalls.Load(), p.preauthCalls.Load())
|
|
}
|
|
}
|
|
|
|
func TestRequiredPreauthValidatedBeforeSale(t *testing.T) {
|
|
p := &testProcessor{validation: func(req SaleRequest) error {
|
|
if req.Amount != 2500 || req.CheckoutDate != "SYNTH-DATE" {
|
|
t.Errorf("ValidatePreauth() request=%+v, want planned 2500 hold/date", req)
|
|
}
|
|
return ErrPreauthUnsupported
|
|
}}
|
|
_, err := NewService(p).Sale(context.Background(), SaleRequest{Amount: 10000, RequiredPreauth: &paymentapi.PreauthRequirement{Amount: 2500, CheckoutDate: "SYNTH-DATE"}}, nil)
|
|
if !errors.Is(err, ErrPreauthUnsupported) || p.saleCalls.Load() != 0 || p.preauthCalls.Load() != 0 {
|
|
t.Errorf("Sale(required unsupported hold) error/calls=%v/%d/%d, want unsupported/no dispatch", err, p.saleCalls.Load(), p.preauthCalls.Load())
|
|
}
|
|
}
|
|
|
|
func TestCMSCurrencyReachesProviderWithoutLocalDefault(t *testing.T) {
|
|
p := &testProcessor{entered: make(chan SaleRequest, 1)}
|
|
s := NewService(p)
|
|
s.SetCurrency("EUR")
|
|
if _, err := s.Sale(context.Background(), SaleRequest{Amount: 100, Currency: "GBP"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := (<-p.entered).Currency; got != "EUR" {
|
|
t.Errorf("Sale() provider currency=%q, want CMS EUR", got)
|
|
}
|
|
}
|