-
Notifications
You must be signed in to change notification settings - Fork 146
/
email_validation_test.go
87 lines (78 loc) · 2.35 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
package mailgun_test
import (
"context"
"encoding/json"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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)
require.NoError(t, err)
assert.True(t, ev.IsValid)
assert.Equal(t, "", ev.MailboxVerification)
assert.False(t, ev.IsDisposableAddress)
assert.False(t, ev.IsRoleAddress)
assert.Equal(t, "", ev.Parts.DisplayName)
assert.Equal(t, "foo", ev.Parts.LocalPart)
assert.Equal(t, "mailgun.com", ev.Parts.Domain)
assert.Equal(t, "", ev.Reason)
assert.True(t, len(ev.Reasons) != 0)
assert.Equal(t, "no-reason", ev.Reasons[0])
assert.Equal(t, "unknown", ev.Risk)
assert.Equal(t, "deliverable", ev.Result)
assert.Equal(t, "disengaged", ev.Engagement.Behavior)
assert.False(t, ev.Engagement.Engaging)
assert.False(t, ev.Engagement.IsBot)
}
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")
require.NoError(t, err)
hittest := map[string]bool{
"Alice <[email protected]>": true,
"[email protected]": true,
}
for _, a := range addressesThatParsed {
require.True(t, hittest[a])
}
require.Len(t, 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)
require.NoError(t, err)
assert.True(t, ev.IsValid)
assert.Equal(t, "unknown", ev.MailboxVerification)
assert.False(t, ev.IsDisposableAddress)
assert.False(t, ev.IsRoleAddress)
assert.Equal(t, "", ev.Parts.DisplayName)
assert.Equal(t, "some_email", ev.Parts.LocalPart)
assert.Equal(t, "aol.com", ev.Parts.Domain)
assert.Equal(t, "no-reason", ev.Reason)
}