-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailing_test.go
112 lines (96 loc) · 3.01 KB
/
mailing_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
package mails_test
import (
"bytes"
"context"
"io"
"testing"
"github.com/spacetab-io/configuration-structs-go/v2/mailing"
"github.com/spacetab-io/configuration-structs-go/v2/mime"
"github.com/spacetab-io/mails-go"
"github.com/spacetab-io/mails-go/contracts"
"github.com/spacetab-io/mails-go/providers"
"github.com/stretchr/testify/assert"
)
func TestMailing_Send(t *testing.T) {
type inStruct struct {
mailing func(writer io.Writer) mails.Mailing
msg contracts.Message
}
type testCase struct {
name string
in inStruct
exp func() string
err error
}
basicMsg := contracts.Message{
To: mailing.MailAddressList{mailing.MailAddress{Email: "[email protected]", Name: "To One"}, mailing.MailAddress{Email: "[email protected]", Name: "To Two"}},
MimeType: mime.TextPlain,
Subject: "Test email",
Content: []byte("test email content"),
Attachments: nil,
}
fullMsg := basicMsg
fullMsg.From = mailing.MailAddress{Email: "[email protected]", Name: "FromName"}
fullMsg.ReplyTo = mailing.MailAddress{Email: "[email protected]", Name: "replyToName"}
fullMsg.Cc = mailing.MailAddressList{mailing.MailAddress{Email: "[email protected]", Name: "Carbon Copy"}}
fullMsg.Bcc = mailing.MailAddressList{mailing.MailAddress{Email: "[email protected]", Name: "Blind Carbon Copy"}}
basicMailCfg := mailing.MessagingConfig{
From: mailing.MailAddress{Email: "[email protected]", Name: "Spacetab Robot"},
ReplyTo: mailing.MailAddress{Email: "[email protected]", Name: "Spacetab Feedback"},
}
fullMailCfg := basicMailCfg
fullMailCfg.SubjectPrefix = "[test]"
prefix := "email by [logs]:\n"
tcs := []testCase{
{
name: "basic email config and full message",
in: inStruct{
msg: fullMsg,
mailing: func(writer io.Writer) mails.Mailing {
mockLogger := mails.NewLogger(writer)
mockProvider, _ := providers.NewLogProvider(mailing.LogsConfig{}, mockLogger)
return mails.NewMailingForProvider(mockProvider, basicMailCfg)
},
},
exp: func() string {
return prefix + fullMsg.String()
},
},
{
name: "full email config and basic message",
in: inStruct{msg: basicMsg, mailing: func(writer io.Writer) mails.Mailing {
mockLogger := mails.NewLogger(writer)
mockProvider, _ := providers.NewLogProvider(mailing.LogsConfig{}, mockLogger)
return mails.NewMailingForProvider(mockProvider, fullMailCfg)
}},
exp: func() string {
msg := basicMsg
msg.From = fullMailCfg.From
msg.Subject = fullMailCfg.SubjectPrefix + " " + msg.Subject
msg.ReplyTo = fullMailCfg.ReplyTo
return prefix + msg.String()
},
},
}
t.Parallel()
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
bb := &bytes.Buffer{}
m := tc.in.mailing(bb)
ctx := context.Background()
err := m.Send(ctx, &tc.in.msg)
if tc.err != nil {
if !assert.NoError(t, err) {
t.FailNow()
}
} else {
if !assert.ErrorIs(t, tc.err, err) {
t.FailNow()
}
}
assert.Equal(t, tc.exp(), bb.String())
})
}
}