forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_validation_test.go
104 lines (92 loc) · 2.89 KB
/
email_validation_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
package mailgun_test
import (
"context"
"encoding/json"
"testing"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
)
func TestEmailValidationV3(t *testing.T) {
v := mailgun.NewEmailValidator(testKey)
// API Base is set to `http://server/v3`
v.SetAPIBase(server.URL())
ctx := context.Background()
ev, err := v.ValidateEmail(ctx, "[email protected]", false)
ensure.Nil(t, err)
ensure.True(t, ev.IsValid)
ensure.DeepEqual(t, ev.MailboxVerification, "")
ensure.False(t, ev.IsDisposableAddress)
ensure.False(t, ev.IsRoleAddress)
ensure.True(t, ev.Parts.DisplayName == "")
ensure.DeepEqual(t, ev.Parts.LocalPart, "foo")
ensure.DeepEqual(t, ev.Parts.Domain, "mailgun.com")
ensure.DeepEqual(t, ev.Reason, "no-reason")
ensure.True(t, len(ev.Reasons) == 0)
ensure.DeepEqual(t, ev.Risk, "unknown")
}
func TestEmailValidationV4(t *testing.T) {
v := mailgun.NewEmailValidator(testKey)
// API Base is set to `http://server/v4`
v.SetAPIBase(server.URL4())
ctx := context.Background()
ev, err := v.ValidateEmail(ctx, "[email protected]", false)
ensure.Nil(t, err)
ensure.True(t, ev.IsValid)
ensure.DeepEqual(t, ev.MailboxVerification, "")
ensure.False(t, ev.IsDisposableAddress)
ensure.False(t, ev.IsRoleAddress)
ensure.True(t, ev.Parts.DisplayName == "")
ensure.DeepEqual(t, ev.Parts.LocalPart, "foo")
ensure.DeepEqual(t, ev.Parts.Domain, "mailgun.com")
ensure.DeepEqual(t, ev.Reason, "")
ensure.True(t, len(ev.Reasons) != 0)
ensure.DeepEqual(t, ev.Reasons[0], "no-reason")
ensure.DeepEqual(t, ev.Risk, "unknown")
ensure.DeepEqual(t, ev.Result, "deliverable")
}
func TestParseAddresses(t *testing.T) {
v := mailgun.NewEmailValidator(testKey)
v.SetAPIBase(server.URL())
ctx := context.Background()
addressesThatParsed, unparsableAddresses, err := v.ParseAddresses(ctx,
"Alice <[email protected]>",
"example.com")
ensure.Nil(t, err)
hittest := map[string]bool{
"Alice <[email protected]>": true,
"[email protected]": true,
}
for _, a := range addressesThatParsed {
ensure.True(t, hittest[a])
}
ensure.True(t, len(unparsableAddresses) == 1)
}
func TestUnmarshallResponse(t *testing.T) {
payload := []byte(`{
"address": "[email protected]",
"did_you_mean": null,
"is_disposable_address": false,
"is_role_address": false,
"is_valid": true,
"mailbox_verification": "unknown",
"parts":
{
"display_name": null,
"domain": "aol.com",
"local_part": "some_email"
},
"reason": "no-reason"
}`)
var ev mailgun.EmailVerification
err := json.Unmarshal(payload, &ev)
ensure.Nil(t, err)
ensure.True(t, ev.IsValid)
ensure.DeepEqual(t, ev.MailboxVerification, "unknown")
ensure.False(t, ev.IsDisposableAddress)
ensure.False(t, ev.IsRoleAddress)
ensure.True(t, ev.Parts.DisplayName == "")
ensure.DeepEqual(t, ev.Parts.LocalPart, "some_email")
ensure.DeepEqual(t, ev.Parts.Domain, "aol.com")
ensure.DeepEqual(t, ev.Reason, "no-reason")
}