-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilestoreconfig_test.go
More file actions
200 lines (165 loc) · 5.09 KB
/
filestoreconfig_test.go
File metadata and controls
200 lines (165 loc) · 5.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
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
package paywall
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/opd-ai/paywall/wallet"
)
// TestNewFileStoreWithConfig tests the new FileStoreConfig functionality
func TestNewFileStoreWithConfig(t *testing.T) {
tempDir, err := os.MkdirTemp("", "filestore_config_test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)
t.Run("StandardFileStore", func(t *testing.T) {
config := FileStoreConfig{
DataDir: filepath.Join(tempDir, "standard"),
EncryptionKey: nil, // No encryption
}
store, err := NewFileStoreWithConfig(config)
if err != nil {
t.Fatalf("NewFileStoreWithConfig() failed: %v", err)
}
// Should return a standard FileStore
if _, ok := store.(*FileStore); !ok {
t.Errorf("Expected *FileStore, got %T", store)
}
// Test basic functionality
testPayment := &Payment{
ID: "test-payment-123",
Addresses: map[wallet.WalletType]string{
wallet.Bitcoin: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
},
Amounts: map[wallet.WalletType]float64{
wallet.Bitcoin: 0.001,
},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour),
Status: StatusPending,
}
err = store.CreatePayment(testPayment)
if err != nil {
t.Fatalf("CreatePayment() failed: %v", err)
}
retrieved, err := store.GetPayment("test-payment-123")
if err != nil {
t.Fatalf("GetPayment() failed: %v", err)
}
if retrieved == nil {
t.Fatal("Retrieved payment should not be nil")
}
if retrieved.ID != testPayment.ID {
t.Errorf("Retrieved payment ID = %s, expected %s", retrieved.ID, testPayment.ID)
}
})
t.Run("EncryptedFileStore", func(t *testing.T) {
encryptionKey, err := wallet.GenerateEncryptionKey()
if err != nil {
t.Fatalf("Failed to generate encryption key: %v", err)
}
config := FileStoreConfig{
DataDir: filepath.Join(tempDir, "encrypted"),
EncryptionKey: encryptionKey,
}
store, err := NewFileStoreWithConfig(config)
if err != nil {
t.Fatalf("NewFileStoreWithConfig() failed: %v", err)
}
// Should return an EncryptedFileStore
if _, ok := store.(*EncryptedFileStore); !ok {
t.Errorf("Expected *EncryptedFileStore, got %T", store)
}
// Test basic functionality with encryption
testPayment := &Payment{
ID: "encrypted-payment-456",
Addresses: map[wallet.WalletType]string{
wallet.Bitcoin: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
},
Amounts: map[wallet.WalletType]float64{
wallet.Bitcoin: 0.002,
},
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(time.Hour),
Status: StatusPending,
}
err = store.CreatePayment(testPayment)
if err != nil {
t.Fatalf("CreatePayment() failed: %v", err)
}
retrieved, err := store.GetPayment("encrypted-payment-456")
if err != nil {
t.Fatalf("GetPayment() failed: %v", err)
}
if retrieved == nil {
t.Fatal("Retrieved payment should not be nil")
}
if retrieved.ID != testPayment.ID {
t.Errorf("Retrieved payment ID = %s, expected %s", retrieved.ID, testPayment.ID)
}
// Verify file is actually encrypted (should not contain readable JSON)
encryptedFile := filepath.Join(config.DataDir, "encrypted-payment-456.enc")
data, err := os.ReadFile(encryptedFile)
if err != nil {
t.Fatalf("Failed to read encrypted file: %v", err)
}
// Should not contain plaintext payment data
if len(data) < 16 { // At least nonce size
t.Error("Encrypted file too small")
}
// Should not contain readable JSON
dataStr := string(data)
if len(dataStr) > 0 && (dataStr[0] == '{' || dataStr[0] == '[') {
t.Error("File appears to contain unencrypted JSON")
}
})
t.Run("DefaultDirectory", func(t *testing.T) {
config := FileStoreConfig{
DataDir: "", // Should use default
EncryptionKey: nil,
}
store, err := NewFileStoreWithConfig(config)
if err != nil {
t.Fatalf("NewFileStoreWithConfig() failed: %v", err)
}
// Should create default directory
if _, err := os.Stat("./payments"); os.IsNotExist(err) {
t.Error("Default payments directory should be created")
}
// Clean up
os.RemoveAll("./payments")
_ = store // Ensure store is used
})
t.Run("InvalidEncryptionKeyLength", func(t *testing.T) {
config := FileStoreConfig{
DataDir: filepath.Join(tempDir, "invalid"),
EncryptionKey: []byte("too_short"), // Invalid length
}
store, err := NewFileStoreWithConfig(config)
if err == nil {
t.Error("NewFileStoreWithConfig() should fail with invalid key length")
}
if store != nil {
t.Error("Store should be nil on error")
}
expectedErr := "encryption key must be 32 bytes, got 9"
if err.Error() != expectedErr {
t.Errorf("Error message = %q, expected %q", err.Error(), expectedErr)
}
})
t.Run("DirectoryCreationFailure", func(t *testing.T) {
// Use an invalid path that cannot be created
config := FileStoreConfig{
DataDir: "/root/invalid/readonly/path",
EncryptionKey: nil,
}
store, err := NewFileStoreWithConfig(config)
if err == nil {
t.Error("NewFileStoreWithConfig() should fail with invalid directory")
}
if store != nil {
t.Error("Store should be nil on error")
}
})
}