| @@ -0,0 +1,120 @@ |
| 1 | package push |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "gitbay.org/gitbay/internal/config" |
| 14 | ) |
| 15 | |
| 16 | // fakeAPNs stands in for Apple. It speaks HTTP/1.1; the real transport is |
| 17 | // h2 by ALPN, which is stdlib behaviour and not this repository's to test. |
| 18 | func fakeAPNs(t *testing.T, h http.HandlerFunc) (*Client, *httptest.Server) { |
| 19 | t.Helper() |
| 20 | srv := httptest.NewServer(h) |
| 21 | t.Cleanup(srv.Close) |
| 22 | t.Setenv("GITBAY_APNS_HOST", strings.TrimPrefix(srv.URL, "http://")) |
| 23 | c, err := NewClient(config.Push{ |
| 24 | Enabled: true, KeyID: "K", TeamID: "T", |
| 25 | Topic: "org.gitbay.gitbay", Environment: "production", |
| 26 | }) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | c.key = testKey(t) |
| 31 | c.tokens = newTokenSource(c.key, "K", "T") |
| 32 | c.scheme = "http" |
| 33 | return c, srv |
| 34 | } |
| 35 | |
| 36 | func TestSendShapesTheRequest(t *testing.T) { |
| 37 | var gotPath, gotTopic, gotType, gotAuth string |
| 38 | var payload map[string]any |
| 39 | c, _ := fakeAPNs(t, func(w http.ResponseWriter, r *http.Request) { |
| 40 | gotPath, gotTopic = r.URL.Path, r.Header.Get("apns-topic") |
| 41 | gotType, gotAuth = r.Header.Get("apns-push-type"), r.Header.Get("authorization") |
| 42 | raw, _ := io.ReadAll(r.Body) |
| 43 | json.Unmarshal(raw, &payload) |
| 44 | w.WriteHeader(200) |
| 45 | }) |
| 46 | res, _, err := c.Send(context.Background(), "DEVTOKEN", "krz/gitbay", "cmc opened issue #12", "krz/gitbay/issues/12") |
| 47 | if err != nil || res != resultSent { |
| 48 | t.Fatalf("res = %v, err = %v", res, err) |
| 49 | } |
| 50 | if gotPath != "/3/device/DEVTOKEN" { |
| 51 | t.Fatalf("path = %q", gotPath) |
| 52 | } |
| 53 | if gotTopic != "org.gitbay.gitbay" || gotType != "alert" { |
| 54 | t.Fatalf("topic = %q, push-type = %q", gotTopic, gotType) |
| 55 | } |
| 56 | if !strings.HasPrefix(gotAuth, "bearer ") { |
| 57 | t.Fatalf("authorization = %q", gotAuth) |
| 58 | } |
| 59 | aps := payload["aps"].(map[string]any) |
| 60 | alert := aps["alert"].(map[string]any) |
| 61 | if alert["title"] != "krz/gitbay" || alert["body"] != "cmc opened issue #12" { |
| 62 | t.Fatalf("alert = %v", alert) |
| 63 | } |
| 64 | if aps["thread-id"] != "krz/gitbay" { |
| 65 | t.Fatalf("thread-id = %v", aps["thread-id"]) |
| 66 | } |
| 67 | if payload["path"] != "krz/gitbay/issues/12" { |
| 68 | t.Fatalf("path = %v", payload["path"]) |
| 69 | } |
| 70 | // Collapsing is wrong here: two comments are two notices. |
| 71 | if _, ok := payload["apns-collapse-id"]; ok { |
| 72 | t.Fatal("collapse id set") |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestSendMapsResponses(t *testing.T) { |
| 77 | cases := []struct { |
| 78 | name string |
| 79 | status int |
| 80 | body string |
| 81 | retryAfter string |
| 82 | want result |
| 83 | wantAfter time.Duration |
| 84 | }{ |
| 85 | {"ok", 200, "", "", resultSent, 0}, |
| 86 | {"gone", 410, `{"reason":"Unregistered"}`, "", resultReap, 0}, |
| 87 | {"bad token", 400, `{"reason":"BadDeviceToken"}`, "", resultReap, 0}, |
| 88 | {"other 400 is permanent", 400, `{"reason":"PayloadTooLarge"}`, "", resultDead, 0}, |
| 89 | {"forbidden is permanent", 403, `{"reason":"InvalidProviderToken"}`, "", resultDead, 0}, |
| 90 | {"too many requests retries", 429, `{"reason":"TooManyRequests"}`, "7", resultRetry, 7 * time.Second}, |
| 91 | {"server error retries", 503, `{"reason":"ServiceUnavailable"}`, "", resultRetry, 0}, |
| 92 | } |
| 93 | for _, tc := range cases { |
| 94 | t.Run(tc.name, func(t *testing.T) { |
| 95 | c, _ := fakeAPNs(t, func(w http.ResponseWriter, r *http.Request) { |
| 96 | if tc.retryAfter != "" { |
| 97 | w.Header().Set("Retry-After", tc.retryAfter) |
| 98 | } |
| 99 | w.WriteHeader(tc.status) |
| 100 | io.WriteString(w, tc.body) |
| 101 | }) |
| 102 | res, after, err := c.Send(context.Background(), "T", "t", "b", "p") |
| 103 | // Only a delivered push has no error. Every other result |
| 104 | // carries the status and reason, which is what the drainer |
| 105 | // records on the queue row. |
| 106 | if tc.want == resultSent && err != nil { |
| 107 | t.Fatalf("err = %v", err) |
| 108 | } |
| 109 | if tc.want != resultSent && err == nil { |
| 110 | t.Fatalf("want an error explaining %v, got nil", tc.want) |
| 111 | } |
| 112 | if res != tc.want { |
| 113 | t.Fatalf("res = %v, want %v", res, tc.want) |
| 114 | } |
| 115 | if after != tc.wantAfter { |
| 116 | t.Fatalf("retryAfter = %v, want %v", after, tc.wantAfter) |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | } |