|
| 1 | +package dispatch_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/uuid" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "cdr.dev/slog" |
| 17 | + "cdr.dev/slog/sloggers/slogtest" |
| 18 | + "github.com/coder/serpent" |
| 19 | + |
| 20 | + "github.com/coder/coder/v2/coderd/notifications/dispatch" |
| 21 | + "github.com/coder/coder/v2/coderd/notifications/types" |
| 22 | + "github.com/coder/coder/v2/codersdk" |
| 23 | + "github.com/coder/coder/v2/testutil" |
| 24 | +) |
| 25 | + |
| 26 | +func TestWebhook(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + const ( |
| 30 | + titleTemplate = "this is the title ({{.Labels.foo}})" |
| 31 | + bodyTemplate = "this is the body ({{.Labels.baz}})" |
| 32 | + ) |
| 33 | + |
| 34 | + msgPayload := types.MessagePayload{ |
| 35 | + Version: "1.0", |
| 36 | + NotificationName: "test", |
| 37 | + Labels: map[string]string{ |
| 38 | + "foo": "bar", |
| 39 | + "baz": "quux", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + serverURL string |
| 46 | + serverTimeout time.Duration |
| 47 | + serverFn func(uuid.UUID, http.ResponseWriter, *http.Request) |
| 48 | + |
| 49 | + expectSuccess bool |
| 50 | + expectRetryable bool |
| 51 | + expectErr string |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "successful", |
| 55 | + serverFn: func(msgID uuid.UUID, w http.ResponseWriter, r *http.Request) { |
| 56 | + var payload dispatch.WebhookPayload |
| 57 | + err := json.NewDecoder(r.Body).Decode(&payload) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 60 | + assert.Equal(t, msgID, payload.MsgID) |
| 61 | + assert.Equal(t, msgID.String(), r.Header.Get("X-Message-Id")) |
| 62 | + |
| 63 | + w.WriteHeader(http.StatusOK) |
| 64 | + _, err = w.Write([]byte(fmt.Sprintf("received %s", payload.MsgID))) |
| 65 | + assert.NoError(t, err) |
| 66 | + }, |
| 67 | + expectSuccess: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "invalid endpoint", |
| 71 | + // Build a deliberately invalid URL to fail validation. |
| 72 | + serverURL: "invalid .com", |
| 73 | + expectSuccess: false, |
| 74 | + expectErr: "invalid URL escape", |
| 75 | + expectRetryable: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "timeout", |
| 79 | + serverTimeout: time.Nanosecond, |
| 80 | + expectSuccess: false, |
| 81 | + expectRetryable: true, |
| 82 | + expectErr: "request timeout", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "non-200 response", |
| 86 | + serverFn: func(_ uuid.UUID, w http.ResponseWriter, r *http.Request) { |
| 87 | + w.WriteHeader(http.StatusInternalServerError) |
| 88 | + }, |
| 89 | + expectSuccess: false, |
| 90 | + expectRetryable: true, |
| 91 | + expectErr: "non-2xx response (500)", |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 96 | + |
| 97 | + // nolint:paralleltest // Irrelevant as of Go v1.22 |
| 98 | + for _, tc := range tests { |
| 99 | + t.Run(tc.name, func(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + |
| 102 | + timeout := testutil.WaitLong |
| 103 | + if tc.serverTimeout > 0 { |
| 104 | + timeout = tc.serverTimeout |
| 105 | + } |
| 106 | + |
| 107 | + var ( |
| 108 | + err error |
| 109 | + ctx = testutil.Context(t, timeout) |
| 110 | + msgID = uuid.New() |
| 111 | + ) |
| 112 | + |
| 113 | + var endpoint *url.URL |
| 114 | + if tc.serverURL != "" { |
| 115 | + endpoint = &url.URL{Host: tc.serverURL} |
| 116 | + } else { |
| 117 | + // Mock server to simulate webhook endpoint. |
| 118 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 119 | + tc.serverFn(msgID, w, r) |
| 120 | + })) |
| 121 | + defer server.Close() |
| 122 | + |
| 123 | + endpoint, err = url.Parse(server.URL) |
| 124 | + require.NoError(t, err) |
| 125 | + } |
| 126 | + |
| 127 | + cfg := codersdk.NotificationsWebhookConfig{ |
| 128 | + Endpoint: *serpent.URLOf(endpoint), |
| 129 | + } |
| 130 | + handler := dispatch.NewWebhookHandler(cfg, logger.With(slog.F("test", tc.name))) |
| 131 | + deliveryFn, err := handler.Dispatcher(msgPayload, titleTemplate, bodyTemplate) |
| 132 | + require.NoError(t, err) |
| 133 | + |
| 134 | + retryable, err := deliveryFn(ctx, msgID) |
| 135 | + if tc.expectSuccess { |
| 136 | + require.NoError(t, err) |
| 137 | + require.False(t, retryable) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + require.ErrorContains(t, err, tc.expectErr) |
| 142 | + require.Equal(t, tc.expectRetryable, retryable) |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments