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") } }