-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendgrid_test.go
More file actions
142 lines (128 loc) · 3.68 KB
/
Copy pathsendgrid_test.go
File metadata and controls
142 lines (128 loc) · 3.68 KB
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
package commo_test
import (
"context"
"errors"
"testing"
sgmail "github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/stretchr/testify/require"
"go.rtnl.ai/commo"
"go.rtnl.ai/x/backoff"
)
func TestNewSGEmail(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
testCases := []struct {
email string
expected *sgmail.Email
}{
{
"jlong@example.com",
&sgmail.Email{Name: "", Address: "jlong@example.com"},
},
{
"Jersey Long <jlong@example.com>",
&sgmail.Email{Name: "Jersey Long", Address: "jlong@example.com"},
},
}
for i, tc := range testCases {
sgm, err := commo.NewSGEmail(tc.email)
require.NoError(t, err, "test case %d errored", i)
require.Equal(t, tc.expected, sgm, "test case %d mismatch", i)
require.Equal(t, tc.expected, commo.MustNewSGEmail(tc.email), "test case %d panic", i)
}
})
t.Run("Invalid", func(t *testing.T) {
testCases := []string{
"foo",
"Lacy Credence <foo>",
"foo@@foo",
}
for i, email := range testCases {
sgm, err := commo.NewSGEmail(email)
require.Error(t, err, "test case %d did not error", i)
require.Nil(t, sgm, "test case %d message was not nil", i)
require.Panics(t, func() { commo.MustNewSGEmail(email) }, "test case %d did not panic", i)
}
})
}
func TestNewSGEmails(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
testCases := []struct {
emails []string
expected []*sgmail.Email
}{
{
nil,
[]*sgmail.Email{},
},
{
[]string{"jlong@example.com"},
[]*sgmail.Email{{Name: "", Address: "jlong@example.com"}},
},
{
[]string{"Jersey Long <jlong@example.com>"},
[]*sgmail.Email{{Name: "Jersey Long", Address: "jlong@example.com"}},
},
{
[]string{"jlong@example.com", "Frieda Short <fshort@example.com>"},
[]*sgmail.Email{{Name: "", Address: "jlong@example.com"}, {Name: "Frieda Short", Address: "fshort@example.com"}},
},
}
for i, tc := range testCases {
sgm, err := commo.NewSGEmails(tc.emails)
require.NoError(t, err, "test case %d errored", i)
require.Equal(t, tc.expected, sgm, "test case %d mismatch", i)
require.Equal(t, tc.expected, commo.MustNewSGEmails(tc.emails), "test case %d panic", i)
}
})
t.Run("Invalid", func(t *testing.T) {
testCases := [][]string{
{
"foo",
},
{
"Larry Helmand <lh@example.com>", "lh@example.com", "bad",
},
{
"foo",
"Lacy Credence <foo>",
"foo@@foo",
},
}
for i, email := range testCases {
sgm, err := commo.NewSGEmails(email)
require.Error(t, err, "test case %d did not error", i)
require.Nil(t, sgm, "test case %d message was not nil", i)
require.Panics(t, func() { commo.MustNewSGEmails(email) }, "test case %d did not panic", i)
}
})
}
// Ensures that Sendgrid errors are handled correctly.
func TestSendGridResponseError(t *testing.T) {
t.Run("4xx is not retried", func(t *testing.T) {
err := commo.SendGridResponseError(400, `{"errors":[{"message":"bad"}]}`)
var stop *backoff.NoRetryError
require.ErrorAs(t, err, &stop)
attempts := 0
_, err = backoff.Retry(context.Background(), func() (any, error) {
attempts++
return nil, commo.SendGridResponseError(400, "bad request")
})
require.Error(t, err)
require.Equal(t, 1, attempts)
})
t.Run("5xx is retried", func(t *testing.T) {
err := commo.SendGridResponseError(503, "unavailable")
var stop *backoff.NoRetryError
require.False(t, errors.As(err, &stop))
attempts := 0
_, err = backoff.Retry(context.Background(), func() (any, error) {
attempts++
if attempts < 3 {
return nil, commo.SendGridResponseError(503, "unavailable")
}
return nil, nil
}, backoff.WithMaxTries(3))
require.NoError(t, err)
require.Equal(t, 3, attempts)
})
}