-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_test.go
More file actions
341 lines (327 loc) · 9.04 KB
/
migration_test.go
File metadata and controls
341 lines (327 loc) · 9.04 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
package paywall
import (
"encoding/json"
"testing"
"time"
"github.com/opd-ai/paywall/wallet"
)
func TestMigratePayment(t *testing.T) {
tests := []struct {
name string
payment *Payment
wantErr bool
check func(*testing.T, *Payment)
}{
{
name: "nil payment returns error",
payment: nil,
wantErr: true,
},
{
name: "payment missing ID returns error",
payment: &Payment{
Addresses: map[wallet.WalletType]string{},
Amounts: map[wallet.WalletType]float64{},
},
wantErr: true,
},
{
name: "payment missing Addresses returns error",
payment: &Payment{
ID: "test-id",
Amounts: map[wallet.WalletType]float64{},
},
wantErr: true,
},
{
name: "payment missing Amounts returns error",
payment: &Payment{
ID: "test-id",
Addresses: map[wallet.WalletType]string{},
},
wantErr: true,
},
{
name: "legacy payment migrates successfully",
payment: &Payment{
ID: "legacy-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "bc1qtest"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
Status: StatusPending,
CreatedAt: time.Now(),
},
wantErr: false,
check: func(t *testing.T, p *Payment) {
if p.MultisigEnabled {
t.Error("legacy payment should not have multisig enabled")
}
},
},
{
name: "multisig payment initializes nil maps",
payment: &Payment{
ID: "multisig-payment",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "bc1qtest"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
MultisigEnabled: true,
Status: StatusPending,
CreatedAt: time.Now(),
// MultisigMetadata, RequiredSignatures, Signatures are nil
},
wantErr: false,
check: func(t *testing.T, p *Payment) {
if p.MultisigMetadata == nil {
t.Error("multisig payment should have initialized MultisigMetadata map")
}
if p.RequiredSignatures == nil {
t.Error("multisig payment should have initialized RequiredSignatures map")
}
if p.Signatures == nil {
t.Error("multisig payment should have initialized Signatures map")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := MigratePayment(tt.payment)
if (err != nil) != tt.wantErr {
t.Errorf("MigratePayment() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && tt.check != nil {
tt.check(t, tt.payment)
}
})
}
}
func TestValidatePaymentJSON(t *testing.T) {
tests := []struct {
name string
json string
wantErr bool
check func(*testing.T, *Payment)
}{
{
name: "invalid JSON returns error",
json: `{invalid json}`,
wantErr: true,
},
{
name: "missing required fields returns error",
json: `{"id":"test"}`,
wantErr: true,
},
{
name: "legacy payment JSON validates successfully",
json: `{
"id": "legacy-001",
"addresses": {"BTC": "bc1qtest"},
"amounts": {"BTC": 0.001},
"created_at": "2024-01-01T00:00:00Z",
"expires_at": "2024-01-02T00:00:00Z",
"status": "pending",
"confirmations": 0
}`,
wantErr: false,
check: func(t *testing.T, p *Payment) {
if p.ID != "legacy-001" {
t.Errorf("expected ID legacy-001, got %s", p.ID)
}
if p.MultisigEnabled {
t.Error("legacy payment should not have multisig enabled")
}
},
},
{
name: "multisig payment JSON validates successfully",
json: `{
"id": "multisig-001",
"addresses": {"BTC": "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"},
"amounts": {"BTC": 0.001},
"created_at": "2024-01-01T00:00:00Z",
"expires_at": "2024-01-02T00:00:00Z",
"status": "pending",
"confirmations": 0,
"multisig_enabled": true,
"required_signatures": {"BTC": 2}
}`,
wantErr: false,
check: func(t *testing.T, p *Payment) {
if !p.MultisigEnabled {
t.Error("expected multisig to be enabled")
}
if p.MultisigMetadata == nil {
t.Error("expected MultisigMetadata to be initialized")
}
if p.RequiredSignatures[wallet.Bitcoin] != 2 {
t.Errorf("expected required signatures 2, got %d", p.RequiredSignatures[wallet.Bitcoin])
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
payment, err := ValidatePaymentJSON([]byte(tt.json))
if (err != nil) != tt.wantErr {
t.Errorf("ValidatePaymentJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && tt.check != nil {
tt.check(t, payment)
}
})
}
}
func TestIsLegacyPayment(t *testing.T) {
tests := []struct {
name string
payment *Payment
want bool
}{
{
name: "nil payment returns false",
payment: nil,
want: false,
},
{
name: "payment without multisig fields is legacy",
payment: &Payment{
ID: "legacy",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "bc1qtest"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
},
want: true,
},
{
name: "payment with multisig enabled is not legacy",
payment: &Payment{
ID: "multisig",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
MultisigEnabled: true,
},
want: false,
},
{
name: "payment with multisig metadata is not legacy",
payment: &Payment{
ID: "multisig",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
MultisigMetadata: map[wallet.WalletType]*wallet.MultisigMetadata{
wallet.Bitcoin: {},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsLegacyPayment(tt.payment); got != tt.want {
t.Errorf("IsLegacyPayment() = %v, want %v", got, tt.want)
}
})
}
}
func TestNormalizePayment(t *testing.T) {
tests := []struct {
name string
payment *Payment
check func(*testing.T, *Payment)
}{
{
name: "nil payment doesn't panic",
payment: nil,
check: func(t *testing.T, p *Payment) {},
},
{
name: "multisig disabled payment clears multisig fields",
payment: &Payment{
ID: "test",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "bc1qtest"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
MultisigEnabled: false,
MultisigMetadata: map[wallet.WalletType]*wallet.MultisigMetadata{
wallet.Bitcoin: {},
},
RequiredSignatures: map[wallet.WalletType]int{wallet.Bitcoin: 2},
Signatures: map[wallet.WalletType][]SignatureData{},
},
check: func(t *testing.T, p *Payment) {
if p.MultisigMetadata != nil {
t.Error("expected MultisigMetadata to be nil after normalization")
}
if p.RequiredSignatures != nil {
t.Error("expected RequiredSignatures to be nil after normalization")
}
if p.Signatures != nil {
t.Error("expected Signatures to be nil after normalization")
}
},
},
{
name: "multisig enabled payment preserves multisig fields",
payment: &Payment{
ID: "test",
Addresses: map[wallet.WalletType]string{wallet.Bitcoin: "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC"},
Amounts: map[wallet.WalletType]float64{wallet.Bitcoin: 0.001},
MultisigEnabled: true,
MultisigMetadata: map[wallet.WalletType]*wallet.MultisigMetadata{
wallet.Bitcoin: {},
},
RequiredSignatures: map[wallet.WalletType]int{wallet.Bitcoin: 2},
},
check: func(t *testing.T, p *Payment) {
if p.MultisigMetadata == nil {
t.Error("expected MultisigMetadata to be preserved")
}
if p.RequiredSignatures == nil {
t.Error("expected RequiredSignatures to be preserved")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NormalizePayment(tt.payment)
tt.check(t, tt.payment)
})
}
}
// TestBackwardCompatibility ensures old payment JSON can be loaded and used
func TestBackwardCompatibility(t *testing.T) {
// Simulate a payment created before multisig support
legacyJSON := `{
"id": "pre-multisig-payment",
"addresses": {
"BTC": "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
},
"amounts": {
"BTC": 0.001
},
"created_at": "2024-01-01T00:00:00Z",
"expires_at": "2024-01-02T00:00:00Z",
"status": "pending",
"confirmations": 0
}`
// Unmarshal the legacy payment
var payment Payment
if err := json.Unmarshal([]byte(legacyJSON), &payment); err != nil {
t.Fatalf("failed to unmarshal legacy payment: %v", err)
}
// Migrate the payment
if err := MigratePayment(&payment); err != nil {
t.Fatalf("failed to migrate legacy payment: %v", err)
}
// Verify the payment is usable
if payment.ID != "pre-multisig-payment" {
t.Errorf("expected ID pre-multisig-payment, got %s", payment.ID)
}
if payment.MultisigEnabled {
t.Error("legacy payment should not have multisig enabled")
}
if !IsLegacyPayment(&payment) {
t.Error("expected payment to be identified as legacy")
}
}