Files
hardlink/internal/trustpayments/process_test.go
T

111 lines
3.8 KiB
Go

package trustpayments
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"time"
)
func TestTrustProcessOneFinancialPostAndBoundedStatus(t *testing.T) {
for _, first := range []string{"Approved", "Declined", "Cancelled", "Pending", "2003", "2005", "2006", "transport", "1011", "mismatch"} {
t.Run(first, func(t *testing.T) {
cfg := fixtureConfig()
var ref, timestamp string
financial, status := 0, 0
cfg.Transport = transportFunc(func(r *http.Request) (*http.Response, error) {
var input map[string]string
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
t.Fatal(err)
}
lookup := strings.HasSuffix(r.URL.Path, "/status")
deadline, ok := r.Context().Deadline()
remaining := time.Until(deadline)
if !lookup {
financial++
ref, timestamp = input["refid"], input["txndate"]
if !ok || remaining > 300*time.Second || remaining < 299*time.Second || input["wait"] != "y" {
t.Errorf("financial deadline/payload=%s/%v", remaining, input)
}
} else {
status++
if input["refid"] != ref || input["txndate"] != timestamp || !ok || remaining > 15*time.Second || remaining < 14*time.Second || input["amount"] != "" {
t.Errorf("status changed identity/budget/payload=%s/%v", remaining, input)
}
}
if !lookup && first == "transport" {
return nil, errors.New("synthetic disconnected provider")
}
body := map[string]any{"refid": ref, "siteid": "site", "terminalserialnumber": "terminal", "type": "sale", "amount": "10.00", "finalamount": "10.00", "resultcode": "Approved", "authcode": "123456"}
if !lookup {
switch first {
case "Declined", "Cancelled":
body["resultcode"], body["authcode"] = first, ""
case "Pending":
body["resultcode"] = first
case "2003", "2005", "2006", "1011":
body["errorcode"] = first
case "mismatch":
body["amount"] = "9.00"
}
}
encoded, _ := json.Marshal(body)
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(encoded)))}, nil
})
client, err := New(cfg)
if err != nil {
t.Fatal(err)
}
result, err := client.Process(context.Background(), Request{Kind: "sale", Amount: 1000, Currency: "GBP"})
wantStatus := 0
if first == "Pending" || first == "transport" || first == "2003" || first == "2005" || first == "2006" {
wantStatus = 1
}
want := Approved
if first == "Declined" {
want = Declined
}
if first == "Cancelled" {
want = Cancelled
}
if first == "1011" || first == "mismatch" {
want = Review
}
if err != nil || financial != 1 || status != wantStatus || result.State != want || len(ref) != 32 {
t.Fatalf("Process(%s) state/error/posts/status=%s/%v/%d/%d, want %s/nil/1/%d", first, result.State, err, financial, status, want, wantStatus)
}
})
}
}
func TestTrustProcessStatusStillUnknownAndPreauth(t *testing.T) {
for _, kind := range []string{"sale", "preauth"} {
t.Run(kind, func(t *testing.T) {
cfg := fixtureConfig()
calls := 0
cfg.Transport = transportFunc(func(r *http.Request) (*http.Response, error) {
calls++
if calls == 1 && !strings.HasSuffix(r.URL.Path, "/"+kind) {
t.Errorf("provider endpoint=%s", r.URL.Path)
}
if calls == 2 && !strings.HasSuffix(r.URL.Path, "/status") {
t.Errorf("second call=%s, want status", r.URL.Path)
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"errorcode":2003}`))}, nil
})
client, err := New(cfg)
if err != nil {
t.Fatal(err)
}
result, err := client.Process(context.Background(), Request{Kind: kind, Amount: 2500, Currency: "GBP"})
if err != nil || result.State != Unknown || calls != 2 {
t.Fatalf("unresolved result=%+v/%v calls=%d", result, err, calls)
}
})
}
}