forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.go
More file actions
157 lines (137 loc) · 4.09 KB
/
Copy pathwebhook_test.go
File metadata and controls
157 lines (137 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dispatch_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"cdr.dev/slog/v3"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/notifications/dispatch"
"github.com/coder/coder/v2/coderd/notifications/types"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/serpent"
)
func TestWebhook(t *testing.T) {
t.Parallel()
const (
titlePlaintext = "this is the title"
titleMarkdown = "this *is* _the_ title"
bodyPlaintext = "this is the body"
bodyMarkdown = "~this~ is the `body`"
)
msgPayload := types.MessagePayload{
Version: "1.0",
NotificationName: "test",
}
tests := []struct {
name string
serverURL string
serverDeadline time.Time
serverFn func(uuid.UUID, http.ResponseWriter, *http.Request)
expectSuccess bool
expectRetryable bool
expectErr string
}{
{
name: "successful",
serverFn: func(msgID uuid.UUID, w http.ResponseWriter, r *http.Request) {
var payload dispatch.WebhookPayload
err := json.NewDecoder(r.Body).Decode(&payload)
assert.NoError(t, err)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, msgID, payload.MsgID)
assert.Equal(t, msgID.String(), r.Header.Get("X-Message-Id"))
assert.Equal(t, titlePlaintext, payload.Title)
assert.Equal(t, titleMarkdown, payload.TitleMarkdown)
assert.Equal(t, bodyPlaintext, payload.Body)
assert.Equal(t, bodyMarkdown, payload.BodyMarkdown)
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(fmt.Sprintf("received %s", payload.MsgID)))
assert.NoError(t, err)
},
expectSuccess: true,
},
{
name: "invalid endpoint",
// Build a deliberately invalid URL to fail validation.
serverURL: "invalid .com",
expectSuccess: false,
expectErr: "invalid URL escape",
expectRetryable: false,
},
{
name: "timeout",
serverDeadline: time.Now().Add(-time.Hour),
expectSuccess: false,
expectRetryable: true,
serverFn: func(u uuid.UUID, writer http.ResponseWriter, request *http.Request) {
t.Fatalf("should not get here")
},
expectErr: "request timeout",
},
{
name: "non-200 response",
serverFn: func(_ uuid.UUID, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
expectSuccess: false,
expectRetryable: true,
expectErr: "non-2xx response (500)",
},
}
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
// nolint:paralleltest // Irrelevant as of Go v1.22
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var (
ctx context.Context
cancel context.CancelFunc
)
if !tc.serverDeadline.IsZero() {
ctx, cancel = context.WithDeadline(context.Background(), tc.serverDeadline)
} else {
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitLong)
}
t.Cleanup(cancel)
var (
err error
msgID = uuid.New()
)
var endpoint *url.URL
if tc.serverURL != "" {
endpoint = &url.URL{Host: tc.serverURL}
} else {
// Mock server to simulate webhook endpoint.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tc.serverFn(msgID, w, r)
}))
t.Cleanup(server.Close)
endpoint, err = url.Parse(server.URL)
require.NoError(t, err)
}
cfg := codersdk.NotificationsWebhookConfig{
Endpoint: *serpent.URLOf(endpoint),
}
handler := dispatch.NewWebhookHandler(cfg, logger.With(slog.F("test", tc.name)))
deliveryFn, err := handler.Dispatcher(msgPayload, titleMarkdown, bodyMarkdown, helpers())
require.NoError(t, err)
retryable, err := deliveryFn(ctx, msgID)
if tc.expectSuccess {
require.NoError(t, err)
require.False(t, retryable)
return
}
require.ErrorContains(t, err, tc.expectErr)
require.Equal(t, tc.expectRetryable, retryable)
})
}
}