-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
utilities_test.go
335 lines (304 loc) · 9.86 KB
/
utilities_test.go
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
package paymail
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
)
// TestSanitizePaymail will test the method SanitizePaymail()
func TestSanitizePaymail(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedAlias string
expectedDomain string
expectedPaymail string
}{
{"[email protected]", "test", "domain.com", "[email protected]"},
{"[email protected]", "test", "domain.com", "[email protected]"},
{"[email protected]", "test", "domain.com", "[email protected]"},
{"[email protected]", "test", "domain.com", "[email protected]"},
{"@DomaiN.COM", "", "domain.com", ""},
{"test@", "test", "", ""},
{"test@domain", "test", "domain", "test@domain"},
{"domain.com", "domain.com", "", ""},
{"[email protected]", "1337", testDomain, "1337@" + testDomain},
}
for _, test := range tests {
if outputAlias, outputDomain, outputPaymail := SanitizePaymail(test.input); outputAlias != test.expectedAlias {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expectedAlias, outputAlias)
} else if outputDomain != test.expectedDomain {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expectedDomain, outputDomain)
} else if outputPaymail != test.expectedPaymail {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expectedPaymail, outputPaymail)
}
}
}
// ExampleSanitizePaymail example using SanitizePaymail()
//
// See more examples in /examples/
func ExampleSanitizePaymail() {
alias, domain, address := SanitizePaymail("[email protected]")
fmt.Printf("alias: %s domain: %s address: %s", alias, domain, address)
// Output:alias: paymail domain: domain.com address: [email protected]
}
// BenchmarkSanitizePaymail benchmarks the method SanitizePaymail()
func BenchmarkSanitizePaymail(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = SanitizePaymail("[email protected]")
}
}
// TestValidatePaymail will test the method ValidatePaymail()
func TestValidatePaymail(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedError bool
}{
{"[email protected]", false},
{"test@example", true},
{"@example", true},
{"example", true},
{"test@.", true},
{"[email protected]", true},
{"[email protected]..", true},
{"[email protected].", true},
}
for _, test := range tests {
if err := ValidatePaymail(test.input); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input)
}
}
}
// ExampleValidatePaymail example using ValidatePaymail()
//
// See more examples in /examples/
func ExampleValidatePaymail() {
err := ValidatePaymail("bad@paymail")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("example failed")
}
// Output:paymail address failed format validation: email is not a valid address format
}
// BenchmarkValidatePaymail benchmarks the method ValidatePaymail()
func BenchmarkValidatePaymail(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ValidatePaymail("[email protected]")
}
}
// TestValidateDomain will test the method ValidateDomain()
func TestValidateDomain(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
expectedError bool
}{
{"domain", true},
{"example.com", false},
{"google.com", false},
{"[email protected]", true},
{"example..", true},
{"example.c", false},
}
for _, test := range tests {
if err := ValidateDomain(test.input); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.input, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.input)
}
}
}
// ExampleValidateDomain example using ValidateDomain()
//
// See more examples in /examples/
func ExampleValidateDomain() {
err := ValidateDomain("domain")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("example failed")
}
// Output:domain name is invalid: domain
}
// BenchmarkValidateDomain benchmarks the method ValidateDomain()
func BenchmarkValidateDomain(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ValidateDomain("domain.com")
}
}
// TestConvertHandle will test the method ConvertHandle()
func TestConvertHandle(t *testing.T) {
t.Parallel()
var tests = []struct {
input string
beta bool
expected string
}{
{"$mr-z", false, "[email protected]"},
{"$MR-Z", false, "[email protected]"},
{"invalid$mr-z", false, "invalid$mr-z"},
{"$", false, "@handcash.io"},
{"$", true, "@beta.handcash.io"},
{"1handle", false, "[email protected]"},
{"1337@" + testDomain, false, "1337@" + testDomain},
{"1337", false, "[email protected]"},
{"1PN7K19Jmj7QQCpUg37WHpSRUw5gKhJVRa", false, "1PN7K19Jmj7QQCpUg37WHpSRUw5gKhJVRa"},
{"$misterz", true, "[email protected]"},
}
for _, test := range tests {
if output := ConvertHandle(test.input, test.beta); output != test.expected {
t.Errorf("%s Failed: [%s] inputted and [%s] expected, received: [%s]", t.Name(), test.input, test.expected, output)
}
}
}
// ExampleConvertHandle example using the method ConvertHandle()
//
// See more examples in /examples/
func ExampleConvertHandle() {
paymail := ConvertHandle("$mr-z", false)
fmt.Println(paymail)
// Output:[email protected]
}
// BenchmarkConvertHandle benchmarks the method ConvertHandle()
func BenchmarkConvertHandle(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ConvertHandle("$mr-z", false)
}
}
// TestValidateTimestamp will test the method ValidateTimestamp()
func TestValidateTimestamp(t *testing.T) {
t.Parallel()
var tests = []struct {
timestamp string
expectedError bool
}{
{"", true},
{"0", true},
{"0000-00-00T00:00:00Z", true},
{"0001-01-01 00:00:00 +0000 UTC", true},
{"0001-01-01T00:00:00Z", true},
{"12345", true},
{"2017", true},
{"2018-01-01", true},
{"2020-04-09 12:00", true},
{"2020-04-09 12:00:00", true},
{"2020-04-09 12:00B", true},
{"2020-04-09T12:00:00", true},
{"abcdef", true},
{time.Now().UTC().Add(-1 * time.Minute).Format(time.RFC3339), false},
{time.Now().UTC().Add(-118 * time.Second).Format(time.RFC3339), false},
{time.Now().UTC().Add(-122 * time.Second).Format(time.RFC3339), true},
{time.Now().UTC().Add(-3 * time.Minute).Format(time.RFC3339), true},
{time.Now().UTC().Add(-4 * time.Minute).Format(time.RFC3339), true},
{time.Now().UTC().Add(1 * time.Minute).Format(time.RFC3339), false},
{time.Now().UTC().Add(122 * time.Second).Format(time.RFC3339), true},
{time.Now().UTC().Add(3 * time.Minute).Format(time.RFC3339), true},
{time.Now().UTC().Add(4 * time.Minute).Format(time.RFC3339), true},
{time.Now().UTC().Format(time.RFC3339), false},
}
for _, test := range tests {
if err := ValidateTimestamp(test.timestamp); err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error not expected but got: %s", t.Name(), test.timestamp, err.Error())
} else if err == nil && test.expectedError {
t.Errorf("%s Failed: [%s] inputted and error was expected", t.Name(), test.timestamp)
}
}
}
// ExampleValidateTimestamp example using the method ValidateTimestamp()
//
// See more examples in /examples/
func ExampleValidateTimestamp() {
err := ValidateTimestamp(time.Now().UTC().Format(time.RFC3339))
if err != nil {
fmt.Printf("error occurred: %s", err.Error())
} else {
fmt.Printf("timestamp was valid!")
}
// Output:timestamp was valid!
}
// BenchmarkValidateTimestamp benchmarks the method ValidateTimestamp()
func BenchmarkValidateTimestamp(b *testing.B) {
timestamp := time.Now().UTC().Format(time.RFC3339)
for i := 0; i < b.N; i++ {
_ = ValidateTimestamp(timestamp)
}
}
func TestValidateAndSanitisePaymail(t *testing.T) {
t.Parallel()
tests := map[string]struct {
paymail string
isBeta bool
expected *SanitisedPaymail
error error
}{
"valid paymail address should be allowed": {
paymail: "[email protected]",
isBeta: false,
expected: &SanitisedPaymail{
Alias: "test",
Domain: "domain.com",
Address: "[email protected]",
},
}, "invalid paymail address should error": {
paymail: "test@domain",
isBeta: false,
error: errors.New("paymail address failed format validation: email is not a valid address format"),
}, "handcash should convert and return": {
paymail: "$test",
isBeta: false,
expected: &SanitisedPaymail{
Alias: "test",
Domain: "handcash.io",
Address: "[email protected]",
},
}, "handcash beta should convert and return": {
paymail: "$test",
isBeta: true,
expected: &SanitisedPaymail{
Alias: "test",
Domain: "beta.handcash.io",
Address: "[email protected]",
},
}, "mad casing should standardize": {
paymail: "[email protected]",
isBeta: true,
expected: &SanitisedPaymail{
Alias: "test",
Domain: "test.com",
Address: "[email protected]",
},
}, "relayx should convert and return": {
paymail: "1test",
isBeta: false,
expected: &SanitisedPaymail{
Alias: "test",
Domain: "relayx.io",
Address: "[email protected]",
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
s, err := ValidateAndSanitisePaymail(test.paymail, test.isBeta)
if test.error != nil {
if err == nil || err.Error() != test.error.Error() {
t.Errorf("expected error [%s] does not match actual [%s]", test.error, err)
}
}
if !reflect.DeepEqual(test.expected, s) {
t.Errorf("expected result [%+v] \n does not match actual [%+v]\n", test.expected, s)
}
})
}
}
// BenchmarkTestValidateAndSanitisePaymail benchmarks the method ValidateTimestamp()
func BenchmarkTestValidateAndSanitisePaymail(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = ValidateAndSanitisePaymail("[email protected]", false)
}
}