99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package paymentgateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRegisterResultSuccess(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
r RegisterResult
|
|
want bool
|
|
}{
|
|
{"ok blank code", RegisterResult{OrderId: "o1", FormUrl: "https://f"}, true},
|
|
{"ok zero code", RegisterResult{OrderId: "o1", FormUrl: "https://f", ErrorCode: "0"}, true},
|
|
{"no orderId", RegisterResult{FormUrl: "https://f"}, false},
|
|
{"no formUrl", RegisterResult{OrderId: "o1"}, false},
|
|
{"error code", RegisterResult{OrderId: "o1", FormUrl: "https://f", ErrorCode: "5"}, false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := c.r.Success(); got != c.want {
|
|
t.Errorf("%s: Success()=%v want %v", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLogClientAlwaysPaid(t *testing.T) {
|
|
c := NewLogClient(nil)
|
|
reg, err := c.Register(context.Background(), RegisterRequest{OrderNumber: "vmo-1-2", ReturnURL: "https://x/cb"})
|
|
if err != nil || !reg.Success() {
|
|
t.Fatalf("stub register should succeed: %+v err=%v", reg, err)
|
|
}
|
|
st, err := c.CheckStatus(context.Background(), StatusRequest{OrderId: reg.OrderId})
|
|
if err != nil {
|
|
t.Fatalf("stub status err: %v", err)
|
|
}
|
|
// Documents debug behaviour: the stub reports EVERY payment as paid.
|
|
if !st.Paid || st.OrderStatus != 2 {
|
|
t.Errorf("stub CheckStatus should report paid/status=2, got paid=%v status=%d", st.Paid, st.OrderStatus)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClientRegisterParsesResponse(t *testing.T) {
|
|
var gotPath, gotAmount, gotCurrency, gotUser string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
gotAmount = r.URL.Query().Get("amount")
|
|
gotCurrency = r.URL.Query().Get("currency")
|
|
gotUser = r.URL.Query().Get("userName")
|
|
w.Write([]byte(`{"orderId":"GB-123","formUrl":"https://pay/GB-123"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
c := &HTTPClient{httpClient: srv.Client(), baseURL: srv.URL + "/"}
|
|
res, err := c.Register(context.Background(), RegisterRequest{
|
|
OrderNumber: "vmo-1-2", AmountMinor: 12345, Username: "u", Password: "p", ReturnURL: "https://cb",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("register: %v", err)
|
|
}
|
|
if res.OrderId != "GB-123" || res.FormUrl != "https://pay/GB-123" || !res.Success() {
|
|
t.Fatalf("bad parse: %+v", res)
|
|
}
|
|
if gotPath != "/register.do" || gotAmount != "12345" || gotCurrency != "934" || gotUser != "u" {
|
|
t.Errorf("bad request: path=%s amount=%s currency=%s user=%s", gotPath, gotAmount, gotCurrency, gotUser)
|
|
}
|
|
}
|
|
|
|
func TestHTTPClientCheckStatusPaidMapping(t *testing.T) {
|
|
cases := []struct {
|
|
body string
|
|
paid bool
|
|
}{
|
|
{`{"orderStatus":2,"actionCode":"0"}`, true}, // 2 = deposited
|
|
{`{"orderStatus":1,"actionCode":0}`, false}, // numeric actionCode still decodes
|
|
{`{"orderStatus":6,"actionCode":"declined"}`, false},
|
|
}
|
|
for _, tc := range cases {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.Contains(r.Header.Get("Content-Type"), "x-www-form-urlencoded") {
|
|
t.Errorf("status check must be form-encoded POST, got %q", r.Header.Get("Content-Type"))
|
|
}
|
|
w.Write([]byte(tc.body))
|
|
}))
|
|
c := &HTTPClient{httpClient: srv.Client(), baseURL: srv.URL + "/"}
|
|
st, err := c.CheckStatus(context.Background(), StatusRequest{OrderId: "x"})
|
|
srv.Close()
|
|
if err != nil {
|
|
t.Fatalf("status: %v", err)
|
|
}
|
|
if st.Paid != tc.paid {
|
|
t.Errorf("body %s: Paid=%v want %v", tc.body, st.Paid, tc.paid)
|
|
}
|
|
}
|
|
}
|