395a307eca
CI / changes (pull_request) Successful in 11s
CI / unit (pull_request) Successful in 22s
CI / integration (pull_request) Successful in 29s
CI / ui (pull_request) Successful in 1m27s
CI / conformance (pull_request) Successful in 19s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m52s
Two holes on the refund path, both found by asking what happens when a refund does not come from our own `/_gm` button. The merchant cabinet is a second entry point. An operator can refund there, and such a refund never passes through our API — so the money went back while the chips stayed credited, silently. Handle `refund.succeeded`: the refund is re-read from the API (the notification body is no more evidence here than it is for a payment), bound back to its order through the payment id recorded when the payment was minted, and reversed through the same engine. It is idempotent on (provider, refund id), so the event for a refund the console already recorded reverses nothing twice. The reversal engine is full-refund-only by design — it revokes exactly what the pack funded and rejects any other amount — so a partial refund is recorded as nothing at all and logged loudly for an operator. There is no non-arbitrary way to decide how many chips a part-refund costs, and guessing would be worse than asking a human. Second hole: a refund can still be canceled while pending, and the ledger is append-only. Recording on any non-empty refund id therefore risked revoking a customer's chips for money that stayed with us, with no way to take the row back. The console now records only a `succeeded` refund and tells the operator to press again otherwise — the idempotency key returns the same refund rather than paying twice. Tests: unit (GetRefund, the refund notification envelope, a non-final status surfaced to the caller); integration (a cabinet refund is reversed once and a redelivery is a no-op, the event after a console refund changes nothing, a partial refund records nothing, an unconfirmed refund reverses nothing, a pending refund records nothing until it settles and then does). The suite shares one database and the ledger dedupes refunds globally, so the fake provider now mints a refund id per payment — a constant id made one test's refund look like another's duplicate. Decisions D50 (amended) and D52; the notification subscription list in the deploy docs gains refund.succeeded.
121 lines
3.8 KiB
Go
121 lines
3.8 KiB
Go
package yookassa
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestParseNotification(t *testing.T) {
|
|
n, err := ParseNotification([]byte(`{"type":"notification","event":"payment.succeeded",
|
|
"object":{"id":"pay-1","status":"succeeded","paid":true,"metadata":{"order_id":"0197-order"}}}`))
|
|
if err != nil {
|
|
t.Fatalf("parse notification: %v", err)
|
|
}
|
|
if n.Event != EventPaymentSucceeded {
|
|
t.Errorf("event = %q, want %q", n.Event, EventPaymentSucceeded)
|
|
}
|
|
p, err := n.Payment()
|
|
if err != nil {
|
|
t.Fatalf("decode notification payment: %v", err)
|
|
}
|
|
if p.ID != "pay-1" {
|
|
t.Errorf("payment id = %q, want pay-1", p.ID)
|
|
}
|
|
if p.OrderID() != "0197-order" {
|
|
t.Errorf("order id = %q, want the order carried in metadata", p.OrderID())
|
|
}
|
|
}
|
|
|
|
func TestParseNotificationRejectsNonEnvelopes(t *testing.T) {
|
|
for name, body := range map[string]string{
|
|
"not json": `nonsense`,
|
|
"wrong type": `{"type":"payment","event":"payment.succeeded","object":{"id":"pay-1"}}`,
|
|
"no event": `{"type":"notification","object":{"id":"pay-1"}}`,
|
|
"empty object": `{}`,
|
|
} {
|
|
if _, err := ParseNotification([]byte(body)); err == nil {
|
|
t.Errorf("%s: accepted as a notification envelope", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNotificationPaymentNeedsAnID(t *testing.T) {
|
|
// Without an id there is nothing to re-read from the API, and the body itself is never evidence.
|
|
n, err := ParseNotification([]byte(`{"type":"notification","event":"payment.succeeded","object":{"status":"succeeded"}}`))
|
|
if err != nil {
|
|
t.Fatalf("parse notification: %v", err)
|
|
}
|
|
if _, err := n.Payment(); err == nil {
|
|
t.Error("a payment object with no id was accepted")
|
|
}
|
|
}
|
|
|
|
func TestAllowedSender(t *testing.T) {
|
|
allowed := []string{
|
|
"185.71.76.0", "185.71.76.31", // /27 bounds
|
|
"185.71.77.15",
|
|
"77.75.153.0", "77.75.153.127", // /25 bounds
|
|
"77.75.156.11", "77.75.156.35", // the two single addresses
|
|
"77.75.154.128", "77.75.154.255",
|
|
"2a02:5180::1", "2a02:5180:ffff::abcd",
|
|
}
|
|
for _, ip := range allowed {
|
|
if !AllowedSender(ip) {
|
|
t.Errorf("%s rejected, want allowed", ip)
|
|
}
|
|
}
|
|
denied := []string{
|
|
"185.71.76.32", "185.71.75.255", // just outside the /27
|
|
"77.75.153.128", // just outside the /25
|
|
"77.75.156.12", // adjacent to a single allowed address
|
|
"77.75.154.127",
|
|
"2a02:5181::1",
|
|
"8.8.8.8", "127.0.0.1", "",
|
|
"not an address",
|
|
}
|
|
for _, ip := range denied {
|
|
if AllowedSender(ip) {
|
|
t.Errorf("%s allowed, want rejected", ip)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAllowedSenderUnmapsIPv4(t *testing.T) {
|
|
// A dual-stack listener reports an IPv4 peer as ::ffff:a.b.c.d; it must still match the v4 range.
|
|
if !AllowedSender("::ffff:77.75.156.11") {
|
|
t.Error("an IPv4-mapped allowed sender was rejected")
|
|
}
|
|
if AllowedSender("::ffff:8.8.8.8") {
|
|
t.Error("an IPv4-mapped foreign sender was allowed")
|
|
}
|
|
}
|
|
|
|
func TestParseRefundNotification(t *testing.T) {
|
|
n, err := ParseNotification([]byte(`{"type":"notification","event":"refund.succeeded",
|
|
"object":{"id":"refund-1","status":"succeeded","payment_id":"pay-1",
|
|
"amount":{"value":"149.00","currency":"RUB"}}}`))
|
|
if err != nil {
|
|
t.Fatalf("parse notification: %v", err)
|
|
}
|
|
if n.Event != EventRefundSucceeded {
|
|
t.Errorf("event = %q, want %q", n.Event, EventRefundSucceeded)
|
|
}
|
|
r, err := n.Refund()
|
|
if err != nil {
|
|
t.Fatalf("decode notification refund: %v", err)
|
|
}
|
|
// The refund names the payment, not our order — that is how it is resolved back to one.
|
|
if r.ID != "refund-1" || r.PaymentID != "pay-1" {
|
|
t.Errorf("refund = %+v, want refund-1 for pay-1", r)
|
|
}
|
|
}
|
|
|
|
func TestNotificationRefundNeedsAnID(t *testing.T) {
|
|
n, err := ParseNotification([]byte(`{"type":"notification","event":"refund.succeeded","object":{"status":"succeeded"}}`))
|
|
if err != nil {
|
|
t.Fatalf("parse notification: %v", err)
|
|
}
|
|
if _, err := n.Refund(); err == nil {
|
|
t.Error("a refund object with no id was accepted")
|
|
}
|
|
}
|