-
Notifications
You must be signed in to change notification settings - Fork 4
/
email_test.go
96 lines (94 loc) · 6.12 KB
/
email_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
package dads
import (
"net/mail"
"os"
"testing"
)
func TestParseAddresses(t *testing.T) {
var ctx Ctx
FatalOnError(os.Setenv("DA_DS", "ds"))
ctx.Init()
ctx.Debug = 2
sameResult := func(a1, a2 []*mail.Address) bool {
m1 := make(map[[2]string]struct{})
m2 := make(map[[2]string]struct{})
for _, a := range a1 {
m1[[2]string{a.Name, a.Address}] = struct{}{}
}
for _, a := range a2 {
m2[[2]string{a.Name, a.Address}] = struct{}{}
}
for k := range m1 {
_, ok := m2[k]
if !ok {
return false
}
}
for k := range m2 {
_, ok := m1[k]
if !ok {
return false
}
}
return true
}
var testCases = []struct {
addr string
expectedEmails []*mail.Address
expectedOK bool
}{
{addr: "Lukasz Gryglicki <[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{addr: "Lukasz Gryglicki [email protected]", expectedOK: false, expectedEmails: []*mail.Address{}},
{addr: `"Lukasz Gryglicki" <[email protected]>`, expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{addr: " Lukasz Gryglicki\t <[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{addr: " Lukasz Gryglicki\t <lgryglicki at cncf.io>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{addr: "Lukasz Gryglicki <lgryglicki_at_cncf.io>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{addr: "Lukasz Gryglicki <lgryglicki en cncf.io>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}}},
{
addr: "Lukasz Gryglicki<[email protected]>,Justyna Gryglicka<[email protected]>",
expectedOK: true,
expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}, {Name: "Justyna Gryglicka", Address: "[email protected]"}},
},
{
addr: "Lukasz Gryglicki<[email protected]>\t , \tJustyna Gryglicka<[email protected]>",
expectedOK: true,
expectedEmails: []*mail.Address{{Name: "Lukasz Gryglicki", Address: "[email protected]"}, {Name: "Justyna Gryglicka", Address: "[email protected]"}},
},
{addr: "a<b@c>,d<e@f>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "d", Address: "e@f"}, {Name: "a", Address: "b@c"}}},
{addr: `a"b<[email protected]>`, expectedOK: true, expectedEmails: []*mail.Address{{Name: "ab", Address: "[email protected]"}}},
{addr: "[email protected]", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}}},
{addr: `'"mia"' <'[email protected]'>`, expectedOK: true, expectedEmails: []*mail.Address{{Name: "mia", Address: "[email protected]"}}},
{addr: " luke\t \t <[email protected]>\t", expectedOK: true, expectedEmails: []*mail.Address{{Name: "luke", Address: "[email protected]"}}},
{addr: " luke\t \t < [email protected]\t>\t", expectedOK: true, expectedEmails: []*mail.Address{{Name: "luke", Address: "[email protected]"}}},
{addr: "\t i have\twhitespace \t < \t [email protected]\t \t>\t \t", expectedOK: true, expectedEmails: []*mail.Address{{Name: "i have whitespace", Address: "[email protected]"}}},
{addr: "<[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}}},
{addr: "=?76dea4628?&<[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "mail", Address: "[email protected]"}}},
// Jeremy Selan <[email protected]> - some groups cut email addrss - we cannot parse this because we cannot guess what the cut value is, example group: SF+ocio-dev
{addr: "bsloan <[email protected]>", expectedOK: false, expectedEmails: []*mail.Address{}},
{addr: "a, z<b@c>, d, y<e@f>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "d, y", Address: "e@f"}, {Name: "a, z", Address: "b@c"}}},
{addr: "<[email protected]> , <[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}, {Name: "you", Address: "[email protected]"}}},
{addr: "[email protected],<[email protected]>", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}, {Name: "you", Address: "[email protected]"}}},
{addr: "< [email protected] >, [email protected]", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}, {Name: "you", Address: "[email protected]"}}},
{addr: "[email protected],[email protected]", expectedOK: true, expectedEmails: []*mail.Address{{Name: "me", Address: "[email protected]"}, {Name: "you", Address: "[email protected]"}}},
// we don't support such messy addresses, original code didn't support this neither
{addr: "=?iso-8859-2?Q?Michal_=C8marada?= <[email protected]>", expectedOK: false, expectedEmails: []*mail.Address{}},
{addr: "=?Windows-1252?Q?Ivan_Hra=9Ako?= <[email protected]>", expectedOK: false, expectedEmails: []*mail.Address{}},
{addr: "=?iso-8859-2?Q?Radek_Krej=E8a?= <[email protected]>", expectedOK: false, expectedEmails: []*mail.Address{}},
{addr: `[email protected] <[email protected]>`, expectedOK: true, expectedEmails: []*mail.Address{{Name: "robert.konc", Address: "[email protected]"}}},
{
addr: ` =?windows-1257?Q?B=B8e=2C_Sebastian?= <[email protected]>,"[email protected]" <[email protected]>,"[email protected]" <[email protected]>`,
expectedOK: true,
expectedEmails: []*mail.Address{{Name: "robert.konc", Address: "[email protected]"}, {Name: "devel", Address: "[email protected]"}},
},
}
for index, test := range testCases {
gotEmails, gotOK := ParseAddresses(&ctx, test.addr, GroupsioMaxRecipients)
if gotOK != test.expectedOK {
t.Errorf("test number %d, expected '%s' ok %v, got %v", index+1, test.addr, test.expectedOK, gotOK)
} else {
if !sameResult(gotEmails, test.expectedEmails) {
t.Errorf("test number %d, expected '%s' to parse to %+v, got %+v", index+1, test.addr, test.expectedEmails, gotEmails)
}
}
}
}