Repository navigation
Expand file tree
/
Copy pathwebhooks_process_test.go
More file actions
338 lines (278 loc) · 11.3 KB
/
Copy pathwebhooks_process_test.go
File metadata and controls
338 lines (278 loc) · 11.3 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright 2024 Blnk Finance Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blnk
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"testing"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/blnkfinance/blnk/config"
"github.com/hibiken/asynq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// receivedWebhook captures everything the webhook receiver saw for one request.
type receivedWebhook struct {
body []byte
headers http.Header
method string
}
// newWebhookReceiver starts an httptest server responding with status and
// returns an accessor for the captured requests.
func newWebhookReceiver(status int) (*httptest.Server, func() []receivedWebhook) {
var mu sync.Mutex
var received []receivedWebhook
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
mu.Lock()
received = append(received, receivedWebhook{
body: body,
headers: r.Header.Clone(),
method: r.Method,
})
mu.Unlock()
w.WriteHeader(status)
_, _ = w.Write([]byte(`{"received":true}`))
}))
get := func() []receivedWebhook {
mu.Lock()
defer mu.Unlock()
out := make([]receivedWebhook, len(received))
copy(out, received)
return out
}
return server, get
}
// storeWebhookTestConfig stores a configuration pointing the webhook
// notification at url, with a unique webhook queue per test to avoid
// cross-test interference on the shared Redis instance.
func storeWebhookTestConfig(url, secret string, headers map[string]string) (*config.Configuration, string) {
queueName := fmt.Sprintf("webhook_q_%d", time.Now().UnixNano())
cnf := &config.Configuration{
Redis: config.RedisConfig{Dns: "localhost:6379"},
Server: config.ServerConfig{
SecretKey: secret,
},
Queue: config.QueueConfig{
WebhookQueue: queueName,
NumberOfQueues: 1,
},
Notification: config.Notification{
Webhook: config.WebhookConfig{
Url: url,
Headers: headers,
},
},
}
config.ConfigStore.Store(cnf)
return cnf, queueName
}
func TestProcessWebhook_DeliversSignedPayload(t *testing.T) {
server, received := newWebhookReceiver(http.StatusOK)
defer server.Close()
const secret = "webhook-signing-secret"
storeWebhookTestConfig(server.URL, secret, map[string]string{
"X-Custom-Tenant": "tenant-42",
})
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
event := NewWebhook{
Event: "transaction.applied",
Payload: map[string]interface{}{
"transaction_id": "txn_process_test",
"amount": 150.25,
},
}
taskPayload, err := json.Marshal(event)
require.NoError(t, err)
before := time.Now().Unix()
err = b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", taskPayload))
after := time.Now().Unix()
require.NoError(t, err)
reqs := received()
require.Len(t, reqs, 1, "receiver must get exactly one delivery")
req := reqs[0]
// Method and content type.
assert.Equal(t, http.MethodPost, req.method)
assert.Equal(t, "application/json", req.headers.Get("Content-Type"))
// Custom configured header is forwarded.
assert.Equal(t, "tenant-42", req.headers.Get("X-Custom-Tenant"))
// Event envelope: {"event": ..., "data": ...}.
var envelope map[string]interface{}
require.NoError(t, json.Unmarshal(req.body, &envelope))
assert.Equal(t, "transaction.applied", envelope["event"])
data, ok := envelope["data"].(map[string]interface{})
require.True(t, ok, "data field must be the event payload object")
assert.Equal(t, "txn_process_test", data["transaction_id"])
assert.Equal(t, 150.25, data["amount"])
// Timestamp header must be a unix timestamp from the delivery window.
tsHeader := req.headers.Get("X-Blnk-Timestamp")
require.NotEmpty(t, tsHeader, "X-Blnk-Timestamp must be set")
ts, err := strconv.ParseInt(tsHeader, 10, 64)
require.NoError(t, err)
assert.GreaterOrEqual(t, ts, before)
assert.LessOrEqual(t, ts, after)
// Signature must be HMAC-SHA256(secret, timestamp + "." + body) hex-encoded,
// recomputable by the receiver from what it was given.
sigHeader := req.headers.Get("X-Blnk-Signature")
require.NotEmpty(t, sigHeader, "X-Blnk-Signature must be set")
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(tsHeader + "." + string(req.body)))
expectedSig := hex.EncodeToString(mac.Sum(nil))
assert.Equal(t, expectedSig, sigHeader, "signature must verify against timestamp.body with the shared secret")
}
func TestProcessWebhook_MalformedTaskPayloadReturnsError(t *testing.T) {
server, received := newWebhookReceiver(http.StatusOK)
defer server.Close()
storeWebhookTestConfig(server.URL, "secret", nil)
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
err = b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", []byte(`{"event": "broken`)))
assert.Error(t, err, "corrupt task payload must surface an error so asynq can retry/dead-letter")
assert.Empty(t, received(), "no HTTP call should be made for an unparseable payload")
}
func TestProcessWebhook_NoURLConfiguredIsNoOp(t *testing.T) {
storeWebhookTestConfig("", "secret", nil)
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
payload, err := json.Marshal(NewWebhook{Event: "transaction.applied", Payload: map[string]string{"k": "v"}})
require.NoError(t, err)
err = b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", payload))
assert.NoError(t, err, "missing webhook URL should be a silent no-op, not a task failure")
}
func TestProcessWebhook_ConnectionRefusedReturnsError(t *testing.T) {
server, _ := newWebhookReceiver(http.StatusOK)
deadURL := server.URL
server.Close() // receiver is down
storeWebhookTestConfig(deadURL, "secret", nil)
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
payload, err := json.Marshal(NewWebhook{Event: "transaction.applied", Payload: map[string]string{"k": "v"}})
require.NoError(t, err)
err = b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", payload))
assert.Error(t, err, "transport-level failure must propagate so asynq retries the delivery")
}
func TestProcessWebhook_Non2xxTriggersRetry(t *testing.T) {
// Regression: processHTTP used to swallow non-2xx responses (nil error),
// so asynq marked the task succeeded and the webhook was permanently
// lost on receiver-side failures. It must now return an error so asynq's
// retry machinery redelivers.
server, received := newWebhookReceiver(http.StatusServiceUnavailable)
defer server.Close()
storeWebhookTestConfig(server.URL, "secret", nil)
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
payload, err := json.Marshal(NewWebhook{Event: "transaction.applied", Payload: map[string]string{"k": "v"}})
require.NoError(t, err)
err = b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", payload))
assert.Error(t, err, "non-2xx from receiver must fail the task so asynq retries delivery")
assert.Len(t, received(), 1, "exactly one attempt per ProcessWebhook call; retries are asynq's job")
}
func TestSendWebhook_EnqueuesTaskWithPayloadOnConfiguredQueue(t *testing.T) {
cnf, queueName := storeWebhookTestConfig("http://localhost:1/never-called", "secret", nil)
inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: "localhost:6379"})
t.Cleanup(func() {
_, _ = inspector.DeleteAllPendingTasks(queueName)
_ = inspector.DeleteQueue(queueName, true)
})
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
event := NewWebhook{
Event: "transaction.inflight",
Payload: map[string]interface{}{
"transaction_id": gofakeit.UUID(),
"status": "INFLIGHT",
},
}
require.NoError(t, b.SendWebhook(event))
tasks, err := inspector.ListPendingTasks(queueName)
require.NoError(t, err)
require.Len(t, tasks, 1, "exactly one task should land on the webhook queue")
task := tasks[0]
assert.Equal(t, cnf.Queue.WebhookQueue, task.Type, "task type must be the webhook queue name for worker routing")
assert.Equal(t, queueName, task.Queue)
var roundTripped NewWebhook
require.NoError(t, json.Unmarshal(task.Payload, &roundTripped))
assert.Equal(t, "transaction.inflight", roundTripped.Event)
data, ok := roundTripped.Payload.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, "INFLIGHT", data["status"])
}
func TestSendWebhook_NoURLConfiguredSkipsEnqueue(t *testing.T) {
_, queueName := storeWebhookTestConfig("", "secret", nil)
inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: "localhost:6379"})
t.Cleanup(func() {
_ = inspector.DeleteQueue(queueName, true)
})
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
require.NoError(t, b.SendWebhook(NewWebhook{Event: "transaction.applied", Payload: map[string]string{"k": "v"}}))
tasks, err := inspector.ListPendingTasks(queueName)
if err == nil {
assert.Empty(t, tasks, "nothing should be enqueued when no webhook URL is configured")
}
// err != nil means the queue does not exist at all, which is equally correct.
}
func TestSendWebhook_UnmarshalablePayloadReturnsError(t *testing.T) {
_, queueName := storeWebhookTestConfig("http://localhost:1/never-called", "secret", nil)
inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: "localhost:6379"})
t.Cleanup(func() {
_ = inspector.DeleteQueue(queueName, true)
})
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
// channels cannot be JSON-marshaled
err = b.SendWebhook(NewWebhook{Event: "transaction.applied", Payload: make(chan int)})
assert.Error(t, err)
tasks, listErr := inspector.ListPendingTasks(queueName)
if listErr == nil {
assert.Empty(t, tasks, "nothing should be enqueued when payload serialization fails")
}
}
func TestProcessWebhook_NoSignatureHeadersWithEmptySecret(t *testing.T) {
// Regression: an empty Server.SecretKey used to produce an HMAC over the
// empty key — computable (and forgeable) by anyone. The delivery is now
// sent unsigned so receivers cannot mistake a forgeable header for
// authentication.
server, received := newWebhookReceiver(http.StatusOK)
defer server.Close()
storeWebhookTestConfig(server.URL, "", nil)
b, err := NewBlnk(nil)
require.NoError(t, err)
defer func() { _ = b.Close() }()
payload, err := json.Marshal(NewWebhook{Event: "transaction.void", Payload: map[string]string{"id": "1"}})
require.NoError(t, err)
require.NoError(t, b.ProcessWebhook(context.Background(), asynq.NewTask("webhook_delivery", payload)))
reqs := received()
require.Len(t, reqs, 1)
assert.Empty(t, reqs[0].headers.Get("X-Blnk-Signature"), "no signature header may be sent without a configured secret")
assert.Empty(t, reqs[0].headers.Get("X-Blnk-Timestamp"), "no timestamp header may be sent without a configured secret")
}