Skip to content
quarry Artifact [8cba7f2853]
Login

Artifact [8cba7f2853]

Artifact 8cba7f2853af43b871294babbec54641b1f4c942c174c13d7276f440d74d3969:


package api

import (
	"strings"
	"testing"
)

// A test push says what the push services answered, from notify's own
// record — the texts here are the ones zeitforge recorded for an
// account with one Chrome and two iPhone subscriptions — and never
// carries a device's endpoint.
func TestAPushOutcomeSaysWhoRefusedAndWhy(t *testing.T) {
	cases := []struct {
		name    string
		in      PushOutcome
		outcome string
		got, of int
		why     string
	}{
		{"not yet", PushOutcome{}, "unknown", 0, 0, ""},
		{"all", PushOutcome{Found: true, Status: "sent"}, "delivered", 2, 2, ""},
		{"apple signature", PushOutcome{Found: true, Status: "sent",
			Error: `delivered 1/2: 1 permanent failure(s) (webpush: status 403: {"reason":"BadJwtToken"})`}, "partial", 1, 2, "403 BadJwtToken"},
		{"timeout and signature", PushOutcome{Found: true, Status: "sent",
			Error: `delivered 1/3: 2 permanent failure(s) (webpush: status 403: {"reason":"BadJwtToken"}; webpush: send: Post "https://web.push.apple.com/QF_secretEndpoint": context deadline exceeded (Client.Timeout exceeded while awaiting headers))`},
			"partial", 1, 3, "403 BadJwtToken; timed out"},
		{"none", PushOutcome{Found: true, Status: "error",
			Error: `webpush: status 403: {"reason":"BadJwtToken"}; webpush: status 403: {"reason":"BadJwtToken"}`}, "failed", 0, 0, "2× 403 BadJwtToken"},
	}
	for _, c := range cases {
		s := summarizePush(c.in, 2)
		why := strings.Join(s.Why, "; ")
		if s.Outcome != c.outcome || s.Delivered != c.got || s.Of != c.of || why != c.why {
			t.Errorf("FAIL %s: %+v (why %q)", c.name, s, why)
		}
		if strings.Contains(why, "secretEndpoint") {
			t.Errorf("FAIL %s: a device endpoint leaked into the summary", c.name)
		}
	}
}