-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultisig_webhook_test.go
More file actions
375 lines (303 loc) · 10.4 KB
/
multisig_webhook_test.go
File metadata and controls
375 lines (303 loc) · 10.4 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package paywall
import (
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestHTTPWebhookNotifier_NotifySignatureReceived(t *testing.T) {
var callCount int32
var receivedEvent WebhookEvent
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&callCount, 1)
if r.Method != "POST" {
t.Errorf("Expected POST request, got %s", r.Method)
}
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &receivedEvent)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config := HTTPWebhookConfig{
SignatureReceivedURL: server.URL,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if atomic.LoadInt32(&callCount) != 1 {
t.Errorf("Expected 1 webhook call, got %d", callCount)
}
if receivedEvent.EventType != "signature_received" {
t.Errorf("Expected event_type 'signature_received', got '%s'", receivedEvent.EventType)
}
if receivedEvent.PaymentID != "payment123" {
t.Errorf("Expected payment_id 'payment123', got '%s'", receivedEvent.PaymentID)
}
if receivedEvent.SignerID != "signer456" {
t.Errorf("Expected signer_id 'signer456', got '%s'", receivedEvent.SignerID)
}
if receivedEvent.Role != RoleBuyer {
t.Errorf("Expected role 'buyer', got '%s'", receivedEvent.Role)
}
}
func TestHTTPWebhookNotifier_NotifyReadyToBroadcast(t *testing.T) {
var receivedEvent WebhookEvent
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &receivedEvent)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config := HTTPWebhookConfig{
ReadyToBroadcastURL: server.URL,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifyReadyToBroadcast("payment789")
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if receivedEvent.EventType != "ready_to_broadcast" {
t.Errorf("Expected event_type 'ready_to_broadcast', got '%s'", receivedEvent.EventType)
}
if receivedEvent.PaymentID != "payment789" {
t.Errorf("Expected payment_id 'payment789', got '%s'", receivedEvent.PaymentID)
}
}
func TestHTTPWebhookNotifier_NotifyBroadcastComplete(t *testing.T) {
var receivedEvent WebhookEvent
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &receivedEvent)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config := HTTPWebhookConfig{
BroadcastCompleteURL: server.URL,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifyBroadcastComplete("payment999", "tx123abc")
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if receivedEvent.EventType != "broadcast_complete" {
t.Errorf("Expected event_type 'broadcast_complete', got '%s'", receivedEvent.EventType)
}
if receivedEvent.PaymentID != "payment999" {
t.Errorf("Expected payment_id 'payment999', got '%s'", receivedEvent.PaymentID)
}
if receivedEvent.TransactionID != "tx123abc" {
t.Errorf("Expected transaction_id 'tx123abc', got '%s'", receivedEvent.TransactionID)
}
}
func TestHTTPWebhookNotifier_Retry(t *testing.T) {
var callCount int32
attemptToSucceed := int32(2) // Fail first, succeed on second attempt
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count := atomic.AddInt32(&callCount, 1)
if count < attemptToSucceed {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config := HTTPWebhookConfig{
SignatureReceivedURL: server.URL,
MaxRetries: 3,
RetryDelay: 50 * time.Millisecond,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err != nil {
t.Fatalf("Expected eventual success, got error: %v", err)
}
if atomic.LoadInt32(&callCount) != attemptToSucceed {
t.Errorf("Expected %d attempts, got %d", attemptToSucceed, callCount)
}
}
func TestHTTPWebhookNotifier_MaxRetriesExceeded(t *testing.T) {
var callCount int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&callCount, 1)
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
config := HTTPWebhookConfig{
SignatureReceivedURL: server.URL,
MaxRetries: 2,
RetryDelay: 10 * time.Millisecond,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err == nil {
t.Fatal("Expected error after max retries, got nil")
}
// Should try: initial + 2 retries = 3 attempts
expected := int32(3)
if atomic.LoadInt32(&callCount) != expected {
t.Errorf("Expected %d attempts, got %d", expected, callCount)
}
}
func TestHTTPWebhookNotifier_ClientError_NoRetry(t *testing.T) {
var callCount int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&callCount, 1)
w.WriteHeader(http.StatusBadRequest)
}))
defer server.Close()
config := HTTPWebhookConfig{
SignatureReceivedURL: server.URL,
MaxRetries: 3,
RetryDelay: 10 * time.Millisecond,
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err == nil {
t.Fatal("Expected error for 4xx response, got nil")
}
// Client errors (4xx) should not retry
if atomic.LoadInt32(&callCount) != 1 {
t.Errorf("Expected 1 attempt (no retries for 4xx), got %d", callCount)
}
}
func TestHTTPWebhookNotifier_CustomHeaders(t *testing.T) {
var receivedAuthHeader string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedAuthHeader = r.Header.Get("Authorization")
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
config := HTTPWebhookConfig{
SignatureReceivedURL: server.URL,
Headers: map[string]string{
"Authorization": "Bearer secret-token",
},
}
notifier := NewHTTPWebhookNotifier(config)
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
expectedAuth := "Bearer secret-token"
if receivedAuthHeader != expectedAuth {
t.Errorf("Expected Authorization header '%s', got '%s'", expectedAuth, receivedAuthHeader)
}
}
func TestHTTPWebhookNotifier_EmptyURL_NoError(t *testing.T) {
config := HTTPWebhookConfig{
SignatureReceivedURL: "", // Empty URL
}
notifier := NewHTTPWebhookNotifier(config)
// Should return nil (no error) when URL is not configured
err := notifier.NotifySignatureReceived("payment123", "signer456", RoleBuyer)
if err != nil {
t.Errorf("Expected no error for empty URL, got: %v", err)
}
}
func TestHTTPWebhookNotifier_Defaults(t *testing.T) {
config := HTTPWebhookConfig{
SignatureReceivedURL: "http://example.com",
}
notifier := NewHTTPWebhookNotifier(config)
if notifier.config.Timeout != 10*time.Second {
t.Errorf("Expected default timeout 10s, got %v", notifier.config.Timeout)
}
if notifier.config.MaxRetries != 3 {
t.Errorf("Expected default max retries 3, got %d", notifier.config.MaxRetries)
}
if notifier.config.RetryDelay != 1*time.Second {
t.Errorf("Expected default retry delay 1s, got %v", notifier.config.RetryDelay)
}
if notifier.config.Headers == nil {
t.Error("Expected headers map to be initialized, got nil")
}
}
func TestLoggingWebhookNotifier(t *testing.T) {
// Capture log output
var logOutput strings.Builder
logger := log.New(&logOutput, "", 0)
notifier := NewLoggingWebhookNotifier(logger)
// Test signature received
notifier.NotifySignatureReceived("payment1", "signer1", RoleBuyer)
if !strings.Contains(logOutput.String(), "Signature received") {
t.Error("Expected log output for signature received")
}
logOutput.Reset()
// Test ready to broadcast
notifier.NotifyReadyToBroadcast("payment2")
if !strings.Contains(logOutput.String(), "Ready to broadcast") {
t.Error("Expected log output for ready to broadcast")
}
logOutput.Reset()
// Test broadcast complete
notifier.NotifyBroadcastComplete("payment3", "tx123")
if !strings.Contains(logOutput.String(), "Broadcast complete") {
t.Error("Expected log output for broadcast complete")
}
}
func TestLoggingWebhookNotifier_NilLogger(t *testing.T) {
// Should use default logger when nil is provided
notifier := NewLoggingWebhookNotifier(nil)
if notifier.logger == nil {
t.Error("Expected default logger to be set, got nil")
}
// Should not panic
err := notifier.NotifySignatureReceived("payment1", "signer1", RoleBuyer)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
}
func TestNoOpWebhookNotifier(t *testing.T) {
notifier := NewNoOpWebhookNotifier()
// All methods should return nil without doing anything
err := notifier.NotifySignatureReceived("payment1", "signer1", RoleBuyer)
if err != nil {
t.Errorf("Expected no error from no-op notifier, got: %v", err)
}
err = notifier.NotifyReadyToBroadcast("payment2")
if err != nil {
t.Errorf("Expected no error from no-op notifier, got: %v", err)
}
err = notifier.NotifyBroadcastComplete("payment3", "tx123")
if err != nil {
t.Errorf("Expected no error from no-op notifier, got: %v", err)
}
}
func TestWebhookNotifierInterfaces(t *testing.T) {
// Verify all types implement MultisigWebhookNotifier interface
var _ MultisigWebhookNotifier = &HTTPWebhookNotifier{}
var _ MultisigWebhookNotifier = &LoggingWebhookNotifier{}
var _ MultisigWebhookNotifier = &NoOpWebhookNotifier{}
}
func TestWebhookEvent_JSONMarshaling(t *testing.T) {
event := WebhookEvent{
EventType: "test_event",
PaymentID: "payment123",
SignerID: "signer456",
Role: RoleSeller,
TransactionID: "tx789",
Timestamp: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
}
data, err := json.Marshal(event)
if err != nil {
t.Fatalf("Failed to marshal event: %v", err)
}
var decoded WebhookEvent
err = json.Unmarshal(data, &decoded)
if err != nil {
t.Fatalf("Failed to unmarshal event: %v", err)
}
if decoded.EventType != event.EventType {
t.Errorf("EventType mismatch: expected %s, got %s", event.EventType, decoded.EventType)
}
if decoded.PaymentID != event.PaymentID {
t.Errorf("PaymentID mismatch: expected %s, got %s", event.PaymentID, decoded.PaymentID)
}
}