-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
courier_test.go
168 lines (144 loc) · 4.39 KB
/
courier_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
package courier_test
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
"github.com/cenkalti/backoff"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/ory/dockertest"
"github.com/ory/viper"
dhelper "github.com/ory/x/sqlcon/dockertest"
templates "github.com/ory/kratos/courier/template"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/internal"
)
var resources []*dockertest.Resource
// nolint:staticcheck
func TestMain(m *testing.M) {
atexit := dhelper.NewOnExit()
atexit.Add(func() {
for _, resource := range resources {
resource.Close()
}
})
atexit.Exit(m.Run())
}
func runTestSMTP(t *testing.T) (smtp, api string) {
if smtp, api := os.Getenv("TEST_MAILHOG_SMTP"), os.Getenv("TEST_MAILHOG_API"); smtp != "" && api != "" {
t.Logf("Skipping Docker setup because environment variables TEST_MAILHOG_SMTP and TEST_MAILHOG_API are both set.")
return smtp, api
} else if len(smtp)+len(api) > 0 {
t.Fatal("Environment variables TEST_MAILHOG_SMTP, TEST_MAILHOG_API must both be set!")
return "", ""
}
pool, err := dockertest.NewPool("")
require.NoError(t, err)
resource, err := pool.
RunWithOptions(&dockertest.RunOptions{
Repository: "mailhog/mailhog",
Tag: "v1.0.0",
Cmd: []string{
"-invite-jim",
"-jim-linkspeed-affect=0.05",
"-jim-reject-auth=0.05",
"-jim-reject-recipient=0.05",
"-jim-reject-sender=0.05",
"-jim-disconnect=0.05",
"-jim-linkspeed-min=1250",
"-jim-linkspeed-max=12500",
},
})
require.NoError(t, err)
resources = append(resources, resource)
smtp = fmt.Sprintf("smtp://test:test@127.0.0.1:%s", resource.GetPort("1025/tcp"))
api = fmt.Sprintf("http://127.0.0.1:%s", resource.GetPort("8025/tcp"))
require.NoError(t, backoff.Retry(func() error {
res, err := http.Get(api + "/api/v2/messages")
if err != nil {
t.Logf("Unable to connect to mailhog: %s", err)
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err := errors.Errorf("expected status code 200 but got: %d", res.StatusCode)
t.Logf("Unable to connect to mailhog: %s", err)
return err
}
return nil
}, backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), 15)))
return smtp, api
}
func TestSMTP(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
smtp, api := runTestSMTP(t)
t.Logf("SMTP URL: %s", smtp)
t.Logf("API URL: %s", api)
conf, reg := internal.NewRegistryDefault(t)
viper.Set(configuration.ViperKeyCourierSMTPURL, smtp)
viper.Set(configuration.ViperKeyCourierSMTPFrom, "test-stub@ory.sh")
c := reg.Courier()
go func() {
require.NoError(t, c.Work())
}()
t.Run("case=queue messages", func(t *testing.T) {
id, err := c.QueueEmail(context.Background(), templates.NewTestStub(conf, &templates.TestStubModel{
To: "test-recipient-1@example.org",
Subject: "test-subject-1",
Body: "test-body-1",
}))
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, id)
id, err = c.QueueEmail(context.Background(), templates.NewTestStub(conf, &templates.TestStubModel{
To: "test-recipient-2@example.org",
Subject: "test-subject-2",
Body: "test-body-2",
}))
require.NoError(t, err)
require.NotEqual(t, uuid.Nil, id)
})
t.Run("case=check for delivery", func(t *testing.T) {
var err error
var body []byte
for k := 0; k < 30; k++ {
time.Sleep(time.Second)
err = func() error {
res, err := http.Get(api + "/api/v2/messages")
if err != nil {
return err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if http.StatusOK != res.StatusCode {
return errors.Errorf("expected status code 200 but got %d with body: %s", res.StatusCode, body)
}
if total := gjson.GetBytes(body, "total").Int(); total != 2 {
return errors.Errorf("expected to have delivered at least 2 messages but got count %d with body: %s", total, body)
}
return nil
}()
if err == nil {
break
}
}
require.NoError(t, err)
for k := 1; k <= 2; k++ {
assert.Contains(t, string(body), fmt.Sprintf("test-subject-%d", k))
assert.Contains(t, string(body), fmt.Sprintf("test-body-%d", k))
assert.Contains(t, string(body), fmt.Sprintf("test-recipient-%d@example.org", k))
assert.Contains(t, string(body), "test-stub@ory.sh")
}
})
}