1
1
package server
2
2
3
3
import (
4
- "context"
5
4
"testing"
6
5
"time"
7
6
@@ -10,51 +9,145 @@ import (
10
9
"github.com/tonicpow/go-paymail"
11
10
)
12
11
13
- // Mock implementation of a service provider
14
- type mockServiceProvider struct {
15
- // Extend your dependencies or custom values
12
+ // testConfig loads a basic test configuration
13
+ func testConfig (t * testing.T , domain string ) * Configuration {
14
+ c , err := NewConfig (
15
+ new (mockServiceProvider ),
16
+ WithDomain (domain ),
17
+ WithGenericCapabilities (),
18
+ )
19
+ require .NoError (t , err )
20
+ require .NotNil (t , c )
21
+ return c
16
22
}
17
23
18
- // GetPaymailByAlias is a demo implementation of this interface
19
- func ( m * mockServiceProvider ) GetPaymailByAlias ( _ context. Context , alias , domain string ,
20
- _ * RequestMetadata ) ( * paymail. AddressInformation , error ) {
24
+ // TestConfiguration_Validate will test the method Validate()
25
+ func TestConfiguration_Validate ( t * testing. T ) {
26
+ t . Parallel ()
21
27
22
- // Get the data from the demo database
23
- return nil , nil
24
- }
28
+ t .Run ("missing domain" , func (t * testing.T ) {
29
+ c := & Configuration {}
30
+ err := c .Validate ()
31
+ require .Error (t , err )
32
+ assert .ErrorIs (t , err , ErrDomainMissing )
33
+ })
25
34
26
- // CreateAddressResolutionResponse is a demo implementation of this interface
27
- func (m * mockServiceProvider ) CreateAddressResolutionResponse (ctx context.Context , alias , domain string ,
28
- senderValidation bool , _ * RequestMetadata ) (* paymail.ResolutionInformation , error ) {
35
+ t .Run ("missing port" , func (t * testing.T ) {
36
+ c := & Configuration {
37
+ PaymailDomains : []* Domain {{Name : "test.com" }},
38
+ }
39
+ err := c .Validate ()
40
+ require .Error (t , err )
41
+ assert .ErrorIs (t , err , ErrPortMissing )
42
+ })
29
43
30
- // Generate a new destination / output for the basic address resolution
31
- return nil , nil
32
- }
44
+ t .Run ("missing service name" , func (t * testing.T ) {
45
+ c := & Configuration {
46
+ Port : 12345 ,
47
+ PaymailDomains : []* Domain {{Name : "test.com" }},
48
+ }
49
+ err := c .Validate ()
50
+ require .Error (t , err )
51
+ assert .ErrorIs (t , err , ErrServiceNameMissing )
52
+ })
33
53
34
- // CreateP2PDestinationResponse is a demo implementation of this interface
35
- func (m * mockServiceProvider ) CreateP2PDestinationResponse (ctx context.Context , alias , domain string ,
36
- satoshis uint64 , _ * RequestMetadata ) (* paymail.PaymentDestinationInformation , error ) {
54
+ t .Run ("invalid service name" , func (t * testing.T ) {
55
+ c := & Configuration {
56
+ Port : 12345 ,
57
+ ServiceName : "$*%*" ,
58
+ PaymailDomains : []* Domain {{Name : "test.com" }},
59
+ }
60
+ err := c .Validate ()
61
+ require .Error (t , err )
62
+ assert .ErrorIs (t , err , ErrServiceNameMissing )
63
+ })
37
64
38
- // Generate a new destination for the p2p request
39
- return nil , nil
40
- }
65
+ t .Run ("missing capabilities" , func (t * testing.T ) {
66
+ c := & Configuration {
67
+ Port : 12345 ,
68
+ ServiceName : "test" ,
69
+ PaymailDomains : []* Domain {{Name : "test.com" }},
70
+ }
71
+ err := c .Validate ()
72
+ require .Error (t , err )
73
+ assert .ErrorIs (t , err , ErrCapabilitiesMissing )
74
+ })
41
75
42
- // RecordTransaction is a demo implementation of this interface
43
- func (m * mockServiceProvider ) RecordTransaction (ctx context.Context ,
44
- p2pTx * paymail.P2PTransaction , _ * RequestMetadata ) (* paymail.P2PTransactionInformation , error ) {
76
+ t .Run ("invalid capabilities" , func (t * testing.T ) {
77
+ c := & Configuration {
78
+ Port : 12345 ,
79
+ ServiceName : "test" ,
80
+ PaymailDomains : []* Domain {{Name : "test.com" }},
81
+ Capabilities : & Capabilities {
82
+ BsvAlias : "" ,
83
+ },
84
+ }
85
+ err := c .Validate ()
86
+ require .Error (t , err )
87
+ assert .ErrorIs (t , err , ErrBsvAliasMissing )
88
+ })
45
89
46
- // Record the tx into your datastore layer
47
- return nil , nil
48
- }
90
+ t .Run ("zero capabilities" , func (t * testing.T ) {
91
+ c := & Configuration {
92
+ Port : 12345 ,
93
+ ServiceName : "test" ,
94
+ PaymailDomains : []* Domain {{Name : "test.com" }},
95
+ Capabilities : & Capabilities {
96
+ BsvAlias : "test" ,
97
+ },
98
+ }
99
+ err := c .Validate ()
100
+ require .Error (t , err )
101
+ assert .ErrorIs (t , err , ErrCapabilitiesMissing )
102
+ })
49
103
50
- // TestConfiguration_Validate will test the method Validate()
51
- func TestConfiguration_Validate (t * testing.T ) {
52
- // todo: finish test!
104
+ t .Run ("basic valid configuration" , func (t * testing.T ) {
105
+ c := & Configuration {
106
+ Port : 12345 ,
107
+ ServiceName : "test" ,
108
+ PaymailDomains : []* Domain {{Name : "test.com" }},
109
+ Capabilities : genericCapabilities ("test" , false ),
110
+ }
111
+ err := c .Validate ()
112
+ require .NoError (t , err )
113
+ })
53
114
}
54
115
55
116
// TestConfiguration_IsAllowedDomain will test the method IsAllowedDomain()
56
117
func TestConfiguration_IsAllowedDomain (t * testing.T ) {
57
- // todo: finish test!
118
+ t .Parallel ()
119
+
120
+ t .Run ("empty domain" , func (t * testing.T ) {
121
+ c := testConfig (t , "test.com" )
122
+ require .NotNil (t , c )
123
+
124
+ success := c .IsAllowedDomain ("" )
125
+ assert .Equal (t , false , success )
126
+ })
127
+
128
+ t .Run ("domain found" , func (t * testing.T ) {
129
+ c := testConfig (t , "test.com" )
130
+ require .NotNil (t , c )
131
+
132
+ success := c .IsAllowedDomain ("test.com" )
133
+ assert .Equal (t , true , success )
134
+ })
135
+
136
+ t .Run ("sanitized domain found" , func (t * testing.T ) {
137
+ c := testConfig (t , "test.com" )
138
+ require .NotNil (t , c )
139
+
140
+ success := c .IsAllowedDomain ("WWW.test.COM" )
141
+ assert .Equal (t , true , success )
142
+ })
143
+
144
+ t .Run ("both domains are sanitized" , func (t * testing.T ) {
145
+ c := testConfig (t , "WwW.Test.Com" )
146
+ require .NotNil (t , c )
147
+
148
+ success := c .IsAllowedDomain ("WWW.test.COM" )
149
+ assert .Equal (t , true , success )
150
+ })
58
151
}
59
152
60
153
// TestConfiguration_AddDomain will test the method AddDomain()
@@ -63,29 +156,40 @@ func TestConfiguration_AddDomain(t *testing.T) {
63
156
64
157
t .Run ("no domain" , func (t * testing.T ) {
65
158
testDomain := "test.com"
66
- c , err := NewConfig (new (mockServiceProvider ), WithDomain (testDomain ), WithGenericCapabilities ())
67
- require .NoError (t , err )
159
+ c := testConfig (t , testDomain )
68
160
require .NotNil (t , c )
69
161
70
- err = c .AddDomain ("" )
162
+ err : = c .AddDomain ("" )
71
163
assert .Error (t , err )
72
164
assert .ErrorIs (t , err , ErrDomainMissing )
73
165
})
74
166
75
167
t .Run ("sanitized domain" , func (t * testing.T ) {
76
168
testDomain := "WWW.TEST.COM"
77
169
addDomain := "testER.com"
78
- c , err := NewConfig (new (mockServiceProvider ), WithDomain (testDomain ), WithGenericCapabilities ())
79
- require .NoError (t , err )
170
+ c := testConfig (t , testDomain )
80
171
require .NotNil (t , c )
81
172
82
- err = c .AddDomain (addDomain )
83
- assert .NoError (t , err )
173
+ err : = c .AddDomain (addDomain )
174
+ require .NoError (t , err )
84
175
85
176
assert .Equal (t , 2 , len (c .PaymailDomains ))
86
177
assert .Equal (t , "test.com" , c .PaymailDomains [0 ].Name )
87
178
assert .Equal (t , "tester.com" , c .PaymailDomains [1 ].Name )
88
179
})
180
+
181
+ t .Run ("domain already exists" , func (t * testing.T ) {
182
+ testDomain := "test.com"
183
+ addDomain := "test.com"
184
+ c := testConfig (t , testDomain )
185
+ require .NotNil (t , c )
186
+
187
+ err := c .AddDomain (addDomain )
188
+ require .NoError (t , err )
189
+
190
+ assert .Equal (t , 1 , len (c .PaymailDomains ))
191
+ assert .Equal (t , "test.com" , c .PaymailDomains [0 ].Name )
192
+ })
89
193
}
90
194
91
195
// TestConfiguration_EnrichCapabilities will test the method EnrichCapabilities()
@@ -94,8 +198,7 @@ func TestConfiguration_EnrichCapabilities(t *testing.T) {
94
198
95
199
t .Run ("basic enrich" , func (t * testing.T ) {
96
200
testDomain := "test.com"
97
- c , err := NewConfig (new (mockServiceProvider ), WithDomain (testDomain ), WithGenericCapabilities ())
98
- require .NoError (t , err )
201
+ c := testConfig (t , testDomain )
99
202
require .NotNil (t , c )
100
203
101
204
capabilities := c .EnrichCapabilities (testDomain )
@@ -110,8 +213,7 @@ func TestConfiguration_EnrichCapabilities(t *testing.T) {
110
213
111
214
t .Run ("multiple times" , func (t * testing.T ) {
112
215
testDomain := "test.com"
113
- c , err := NewConfig (new (mockServiceProvider ), WithDomain ("test.com" ), WithGenericCapabilities ())
114
- require .NoError (t , err )
216
+ c := testConfig (t , testDomain )
115
217
require .NotNil (t , c )
116
218
117
219
capabilities := c .EnrichCapabilities (testDomain )
@@ -163,14 +265,14 @@ func TestNewConfig(t *testing.T) {
163
265
164
266
t .Run ("no values and no provider" , func (t * testing.T ) {
165
267
c , err := NewConfig (nil )
166
- assert .Error (t , err )
268
+ require .Error (t , err )
167
269
assert .ErrorIs (t , err , ErrServiceProviderNil )
168
270
assert .Nil (t , c )
169
271
})
170
272
171
273
t .Run ("missing domain" , func (t * testing.T ) {
172
274
c , err := NewConfig (new (mockServiceProvider ))
173
- assert .Error (t , err )
275
+ require .Error (t , err )
174
276
assert .ErrorIs (t , err , ErrDomainMissing )
175
277
assert .Nil (t , c )
176
278
})
@@ -180,8 +282,8 @@ func TestNewConfig(t *testing.T) {
180
282
new (mockServiceProvider ),
181
283
WithDomain ("test.com" ),
182
284
)
183
- assert .NoError (t , err )
184
- assert .NotNil (t , c )
285
+ require .NoError (t , err )
286
+ require .NotNil (t , c )
185
287
assert .Equal (t , 5 , len (c .Capabilities .Capabilities ))
186
288
assert .Equal (t , "test.com" , c .PaymailDomains [0 ].Name )
187
289
})
@@ -192,8 +294,8 @@ func TestNewConfig(t *testing.T) {
192
294
WithDomain ("test.com" ),
193
295
WithPort (12345 ),
194
296
)
195
- assert .NoError (t , err )
196
- assert .NotNil (t , c )
297
+ require .NoError (t , err )
298
+ require .NotNil (t , c )
197
299
assert .Equal (t , 12345 , c .Port )
198
300
})
199
301
@@ -203,8 +305,8 @@ func TestNewConfig(t *testing.T) {
203
305
WithDomain ("test.com" ),
204
306
WithTimeout (10 * time .Second ),
205
307
)
206
- assert .NoError (t , err )
207
- assert .NotNil (t , c )
308
+ require .NoError (t , err )
309
+ require .NotNil (t , c )
208
310
assert .Equal (t , 10 * time .Second , c .Timeout )
209
311
})
210
312
@@ -214,8 +316,8 @@ func TestNewConfig(t *testing.T) {
214
316
WithDomain ("test.com" ),
215
317
WithServiceName ("custom" ),
216
318
)
217
- assert .NoError (t , err )
218
- assert .NotNil (t , c )
319
+ require .NoError (t , err )
320
+ require .NotNil (t , c )
219
321
assert .Equal (t , "custom" , c .ServiceName )
220
322
})
221
323
@@ -225,20 +327,32 @@ func TestNewConfig(t *testing.T) {
225
327
WithDomain ("test.com" ),
226
328
WithSenderValidation (),
227
329
)
228
- assert .NoError (t , err )
229
- assert .NotNil (t , c )
330
+ require .NoError (t , err )
331
+ require .NotNil (t , c )
230
332
assert .Equal (t , true , c .SenderValidationEnabled )
231
333
})
232
334
335
+ t .Run ("with custom capabilities" , func (t * testing.T ) {
336
+ c , err := NewConfig (
337
+ new (mockServiceProvider ),
338
+ WithDomain ("test.com" ),
339
+ WithCapabilities (genericCapabilities ("test" , false )),
340
+ )
341
+ require .NoError (t , err )
342
+ require .NotNil (t , c )
343
+ assert .Equal (t , 5 , len (c .Capabilities .Capabilities ))
344
+ assert .Equal (t , "test" , c .Capabilities .BsvAlias )
345
+ })
346
+
233
347
t .Run ("with basic routes" , func (t * testing.T ) {
234
348
c , err := NewConfig (
235
349
new (mockServiceProvider ),
236
350
WithDomain ("test.com" ),
237
351
WithBasicRoutes (),
238
352
)
239
- assert .NoError (t , err )
240
- assert .NotNil (t , c )
241
- assert .NotNil (t , c .BasicRoutes )
353
+ require .NoError (t , err )
354
+ require .NotNil (t , c )
355
+ require .NotNil (t , c .BasicRoutes )
242
356
assert .Equal (t , true , c .BasicRoutes .Add404Route )
243
357
assert .Equal (t , true , c .BasicRoutes .AddIndexRoute )
244
358
assert .Equal (t , true , c .BasicRoutes .AddHealthRoute )
0 commit comments