-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverification_test.go
More file actions
311 lines (256 loc) · 8.02 KB
/
verification_test.go
File metadata and controls
311 lines (256 loc) · 8.02 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
package paywall
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/opd-ai/paywall/wallet"
)
// TestCryptoChainMonitor_ExponentialBackoff tests that the monitor implements
// exponential backoff when checkPendingPayments returns errors
func TestCryptoChainMonitor_ExponentialBackoff(t *testing.T) {
// Create a mock paywall with a store that always returns errors
mockStore := &mockFailingStore{}
pw := &Paywall{
Store: mockStore,
}
monitor := &CryptoChainMonitor{
paywall: pw,
}
// Create a context that we can cancel
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start the monitor
monitor.Start(ctx)
// Let it run for a short time to ensure it starts
time.Sleep(100 * time.Millisecond)
// Cancel the context to stop the monitor
cancel()
// Test passes if no panic occurs and monitor starts/stops cleanly
// The actual backoff behavior is tested by observing logs in integration tests
}
// TestCheckWalletPayment_MissingClient tests that checkWalletPayment returns an error
// when the requested wallet client is not found
func TestCheckWalletPayment_MissingClient(t *testing.T) {
mockStore := &mockStore{}
pw := &Paywall{
Store: mockStore,
minConfirmations: 3,
}
monitor := &CryptoChainMonitor{
paywall: pw,
client: make(map[wallet.WalletType]CryptoClient),
}
payment := &Payment{
ID: "test-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "test-address"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
}
var mux sync.Mutex
err := monitor.checkWalletPayment(payment, wallet.Bitcoin, &mux)
if err == nil {
t.Fatal("Expected error for missing client, got nil")
}
if err.Error() != "BTC client not found" {
t.Errorf("Expected 'BTC client not found', got '%s'", err.Error())
}
}
// TestCheckWalletPayment_BalanceBelowThreshold tests that payment status remains pending
// when balance is below the required amount
func TestCheckWalletPayment_BalanceBelowThreshold(t *testing.T) {
mockStore := &mockStore{}
pw := &Paywall{
Store: mockStore,
minConfirmations: 3,
}
mockClient := &mockCryptoClient{
balance: 0.0005, // Below required amount
}
monitor := &CryptoChainMonitor{
paywall: pw,
client: map[wallet.WalletType]CryptoClient{wallet.Bitcoin: mockClient},
}
payment := &Payment{
ID: "test-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "test-address"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
}
var mux sync.Mutex
err := monitor.checkWalletPayment(payment, wallet.Bitcoin, &mux)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if payment.Status != StatusPending {
t.Errorf("Expected status to remain pending, got %s", payment.Status)
}
}
// TestCheckWalletPayment_BalanceAboveThreshold tests that payment status is updated
// to confirmed when balance meets or exceeds the required amount
func TestCheckWalletPayment_BalanceAboveThreshold(t *testing.T) {
mockStore := &mockStore{}
pw := &Paywall{
Store: mockStore,
minConfirmations: 3,
}
mockClient := &mockCryptoClient{
balance: 0.002, // Above required amount
}
monitor := &CryptoChainMonitor{
paywall: pw,
client: map[wallet.WalletType]CryptoClient{wallet.Bitcoin: mockClient},
}
payment := &Payment{
ID: "test-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "test-address"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
}
var mux sync.Mutex
err := monitor.checkWalletPayment(payment, wallet.Bitcoin, &mux)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if payment.Status != StatusConfirmed {
t.Errorf("Expected status to be confirmed, got %s", payment.Status)
}
if payment.Confirmations != 3 {
t.Errorf("Expected confirmations to be 3, got %d", payment.Confirmations)
}
if !mockStore.updateCalled {
t.Error("Expected UpdatePayment to be called")
}
}
// TestCheckWalletPayment_GetBalanceError tests that errors from GetAddressBalance
// are properly propagated
func TestCheckWalletPayment_GetBalanceError(t *testing.T) {
mockStore := &mockStore{}
pw := &Paywall{
Store: mockStore,
minConfirmations: 3,
}
mockClient := &mockCryptoClient{
err: errors.New("network error"),
}
monitor := &CryptoChainMonitor{
paywall: pw,
client: map[wallet.WalletType]CryptoClient{wallet.Bitcoin: mockClient},
}
payment := &Payment{
ID: "test-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "test-address"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
}
var mux sync.Mutex
err := monitor.checkWalletPayment(payment, wallet.Bitcoin, &mux)
if err == nil {
t.Fatal("Expected error from GetAddressBalance, got nil")
}
if err.Error() != "network error" {
t.Errorf("Expected 'network error', got '%s'", err.Error())
}
}
// TestCheckWalletPayment_UpdatePaymentError tests that errors from UpdatePayment
// are silently ignored (as per current implementation)
func TestCheckWalletPayment_UpdatePaymentError(t *testing.T) {
mockStore := &mockStore{
updateError: errors.New("storage error"),
}
pw := &Paywall{
Store: mockStore,
minConfirmations: 3,
}
mockClient := &mockCryptoClient{
balance: 0.002, // Above required amount
}
monitor := &CryptoChainMonitor{
paywall: pw,
client: map[wallet.WalletType]CryptoClient{wallet.Bitcoin: mockClient},
}
payment := &Payment{
ID: "test-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "test-address"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
}
var mux sync.Mutex
err := monitor.checkWalletPayment(payment, wallet.Bitcoin, &mux)
// Current implementation doesn't check UpdatePayment error
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if payment.Status != StatusConfirmed {
t.Errorf("Expected status to be confirmed, got %s", payment.Status)
}
}
// mockStore is a mock implementation of PaymentStore for testing
type mockStore struct {
updateCalled bool
updateError error
}
func (m *mockStore) CreatePayment(payment *Payment) error {
return nil
}
func (m *mockStore) GetPayment(id string) (*Payment, error) {
return nil, nil
}
func (m *mockStore) GetPaymentByAddress(address string) (*Payment, error) {
return nil, nil
}
func (m *mockStore) UpdatePayment(payment *Payment) error {
m.updateCalled = true
return m.updateError
}
func (m *mockStore) ListPendingPayments() ([]*Payment, error) {
return nil, nil
}
func (m *mockStore) GetPendingMultisigPayments() ([]*Payment, error) {
return nil, nil
}
func (m *mockStore) GetEscrowsExpiringBefore(deadline time.Time) ([]*Payment, error) {
return nil, nil
}
func (m *mockStore) Close() error {
return nil
}
// mockCryptoClient is a mock implementation of CryptoClient for testing
type mockCryptoClient struct {
balance float64
err error
}
func (m *mockCryptoClient) GetAddressBalance(address string) (float64, error) {
if m.err != nil {
return 0, m.err
}
return m.balance, nil
}
// mockFailingStore always returns errors to trigger backoff behavior
type mockFailingStore struct{}
func (m *mockFailingStore) CreatePayment(payment *Payment) error {
return nil
}
func (m *mockFailingStore) GetPayment(id string) (*Payment, error) {
return nil, nil
}
func (m *mockFailingStore) GetPaymentByAddress(address string) (*Payment, error) {
return nil, nil
}
func (m *mockFailingStore) UpdatePayment(payment *Payment) error {
return nil
}
func (m *mockFailingStore) ListPendingPayments() ([]*Payment, error) {
// Always return an error to trigger backoff
return nil, errors.New("mock store error")
}
func (m *mockFailingStore) GetPendingMultisigPayments() ([]*Payment, error) {
return nil, errors.New("mock store error")
}
func (m *mockFailingStore) GetEscrowsExpiringBefore(deadline time.Time) ([]*Payment, error) {
return nil, errors.New("mock store error")
}
func (m *mockFailingStore) Close() error {
return nil
}