Debian Codegraph @

pkg/services/email/smtp/smtp_config_test.go

raw
1 package smtp
2
3 import (
4 "crypto/tls"
5 "errors"
6 "net/url"
7 "time"
8
9 "github.com/stretchr/testify/mock"
10
11 ginkgo "github.com/onsi/ginkgo/v2"
12 gomega "github.com/onsi/gomega"
13
14 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
15 "github.com/nicholas-fedor/shoutrrr/pkg/format"
16 typesmocks "github.com/nicholas-fedor/shoutrrr/pkg/types/mocks"
17 )
18
19 var _ = ginkgo.Describe("Config", func() {
20 ginkgo.Describe("Clone", func() {
21 ginkgo.It("should be identical to the original", func() {
22 config := &Config{}
23 gomega.Expect(config.SetURL(testutils.URLMust(urlWithAllProps))).To(gomega.Succeed())
24 gomega.Expect(config.Clone()).To(gomega.Equal(*config))
25 })
26
27 ginkgo.It("should copy ToAddresses so later mutations are independent", func() {
28 config := &Config{ToAddresses: []string{"[email protected]"}}
29 clone := config.Clone()
30 clone.ToAddresses[0] = "[email protected]"
31 gomega.Expect(config.ToAddresses[0]).To(gomega.Equal("[email protected]"))
32 })
33 })
34
35 ginkgo.Describe("Enums", func() {
36 ginkgo.It("should expose Auth and Encryption formatters", func() {
37 enums := (&Config{}).Enums()
38 gomega.Expect(enums).To(gomega.HaveLen(2))
39 gomega.Expect(enums).To(gomega.HaveKey("Auth"))
40 gomega.Expect(enums).To(gomega.HaveKey("Encryption"))
41 gomega.Expect(enums["Auth"]).To(gomega.Equal(AuthTypes.Enum))
42 gomega.Expect(enums["Encryption"]).To(gomega.Equal(EncMethods.Enum))
43 })
44 })
45
46 ginkgo.Describe("GetURL and SetURL", func() {
47 ginkgo.It("should be identical after de-/serialization", func() {
48 url := testutils.URLMust(urlWithAllProps)
49 config := &Config{}
50 pkr := format.NewPropKeyResolver(config)
51 gomega.Expect(config.setURL(&pkr, url)).To(gomega.Succeed())
52
53 outputURL := config.GetURL()
54 ginkgo.GinkgoT().Logf("\n\n%s\n%s\n\n-", outputURL, urlWithAllProps)
55 gomega.Expect(outputURL.String()).To(gomega.Equal(urlWithAllProps))
56 })
57
58 ginkgo.When("skiptlsverify is set to yes", func() {
59 ginkgo.It("should set SkipTLSVerify to true", func() {
60 config := &Config{}
61 gomega.Expect(config.SetURL(testutils.URLMust(
62 "smtp://user:[email protected]:2225/[email protected]&[email protected]&skiptlsverify=yes",
63 ))).To(gomega.Succeed())
64 gomega.Expect(config.SkipTLSVerify).To(gomega.BeTrue())
65 })
66 })
67
68 ginkgo.When("skiptlsverify is set to no", func() {
69 ginkgo.It("should set SkipTLSVerify to false", func() {
70 config := &Config{}
71 gomega.Expect(config.SetURL(testutils.URLMust(
72 "smtp://user:[email protected]:2225/[email protected]&[email protected]&skiptlsverify=no",
73 ))).To(gomega.Succeed())
74 gomega.Expect(config.SkipTLSVerify).To(gomega.BeFalse())
75 })
76 })
77
78 ginkgo.When("fromAddress is missing", func() {
79 ginkgo.It("should return ErrFromAddressMissing", func() {
80 gomega.Expect((&Config{}).SetURL(testutils.URLMust(
81 "smtp://user:[email protected]:2225/[email protected],[email protected]",
82 ))).To(gomega.MatchError(ErrFromAddressMissing))
83 })
84 })
85
86 ginkgo.When("toAddresses are missing", func() {
87 ginkgo.It("should return ErrToAddressMissing", func() {
88 gomega.Expect((&Config{}).SetURL(testutils.URLMust(
89 "smtp://user:[email protected]:2225/[email protected]",
90 ))).To(gomega.MatchError(ErrToAddressMissing))
91 })
92 })
93
94 ginkgo.It("should not allow getting invalid query values", func() {
95 testutils.TestConfigGetInvalidQueryValue(&Config{})
96 })
97
98 ginkgo.It("should not allow setting invalid query values", func() {
99 testutils.TestConfigSetInvalidQueryValue(
100 &Config{},
101 "smtp://example.com/[email protected]&[email protected]&foo=bar",
102 )
103 })
104
105 ginkgo.It("should have the expected number of fields and enums", func() {
106 config := &Config{}
107 testutils.TestConfigGetEnumsCount(config, 2)
108 testutils.TestConfigGetFieldsCount(config, 16)
109 })
110
111 ginkgo.It("should include requirestarttls and skiptlsverify only when true", func() {
112 config := &Config{
113 Host: "example.com",
114 Port: 25,
115 FromAddress: "[email protected]",
116 ToAddresses: []string{"[email protected]"},
117 RequireStartTLS: true,
118 SkipTLSVerify: true,
119 }
120
121 query := config.GetURL().Query()
122 gomega.Expect(query.Get("requirestarttls")).To(gomega.Equal("Yes"))
123 gomega.Expect(query.Get("skiptlsverify")).To(gomega.Equal("Yes"))
124
125 config.RequireStartTLS = false
126 config.SkipTLSVerify = false
127 query = config.GetURL().Query()
128 gomega.Expect(query.Get("requirestarttls")).To(gomega.BeEmpty())
129 gomega.Expect(query.Get("skiptlsverify")).To(gomega.BeEmpty())
130 })
131 })
132
133 ginkgo.Describe("getURL", func() {
134 ginkgo.It("should skip keys the resolver cannot get", func() {
135 config := &Config{Host: "example.com", Port: 25}
136 resolver := typesmocks.NewMockConfigQueryResolver(ginkgo.GinkgoT())
137 resolver.On("Get", mock.Anything).Return("", errors.New("missing")).Times(10)
138
139 got := config.getURL(resolver)
140 gomega.Expect(got.Scheme).To(gomega.Equal(Scheme))
141 gomega.Expect(got.Host).To(gomega.Equal("example.com:25"))
142 gomega.Expect(got.RawQuery).To(gomega.BeEmpty())
143 })
144
145 ginkgo.It("should serialize resolver values into the query", func() {
146 config := &Config{Host: "mail.example.com", Port: 587, Username: "user", Password: "pass"}
147 resolver := typesmocks.NewMockConfigQueryResolver(ginkgo.GinkgoT())
148 resolver.On("Get", mock.Anything).Return("value", nil).Times(10)
149
150 got := config.getURL(resolver)
151 gomega.Expect(got.User.Username()).To(gomega.Equal("user"))
152 pass, ok := got.User.Password()
153 gomega.Expect(ok).To(gomega.BeTrue())
154 gomega.Expect(pass).To(gomega.Equal("pass"))
155 gomega.Expect(got.RawQuery).To(gomega.ContainSubstring("auth=value"))
156 gomega.Expect(got.RawQuery).To(gomega.ContainSubstring("fromaddress=value"))
157 })
158 })
159
160 ginkgo.Describe("setURL", func() {
161 ginkgo.It("should wrap resolver Set errors", func() {
162 resolver := typesmocks.NewMockConfigQueryResolver(ginkgo.GinkgoT())
163 resolver.On("Set", "foo", "bar").Return(errors.New("nope"))
164
165 err := (&Config{}).setURL(resolver, testutils.URLMust("smtp://[email protected]?foo=bar"))
166 gomega.Expect(err).To(gomega.HaveOccurred())
167 gomega.Expect(err.Error()).To(gomega.ContainSubstring(`setting query parameter "foo" to "bar"`))
168 })
169
170 ginkgo.It("should allow the dummy URL without from or to addresses", func() {
171 gomega.Expect((&Config{}).SetURL(testutils.URLMust("smtp://[email protected]"))).To(gomega.Succeed())
172 })
173
174 ginkgo.It("should parse an IPv6 host", func() {
175 config := &Config{}
176 gomega.Expect(config.SetURL(testutils.URLMust(
177 "smtp://[::1]:587/[email protected]&[email protected]",
178 ))).To(gomega.Succeed())
179 gomega.Expect(config.Host).To(gomega.Equal("::1"))
180 gomega.Expect(config.Port).To(gomega.Equal(uint16(587)))
181 gomega.Expect(config.GetURL().Host).To(gomega.Equal("[::1]:587"))
182 gomega.Expect(config.GetURL().Hostname()).To(gomega.Equal("::1"))
183 })
184
185 ginkgo.It("should keep the previous port when the URL port is out of range", func() {
186 config := &Config{Port: DefaultSMTPPort}
187 gomega.Expect(config.SetURL(testutils.URLMust(
188 "smtp://example.com:99999/[email protected]&[email protected]",
189 ))).To(gomega.Succeed())
190 gomega.Expect(config.Port).To(gomega.Equal(uint16(DefaultSMTPPort)))
191 })
192
193 ginkgo.It("should treat an encoded semicolon as part of a single recipient", func() {
194 config := &Config{}
195 gomega.Expect(config.SetURL(testutils.URLMust(
196 "smtp://example.com/[email protected]&[email protected]%[email protected]",
197 ))).To(gomega.Succeed())
198 gomega.Expect(config.ToAddresses).To(gomega.Equal([]string{"[email protected];[email protected]"}))
199 })
200
201 ginkgo.It("should keep display-name spaces in angle-bracket recipients", func() {
202 config := &Config{}
203 gomega.Expect(config.SetURL(testutils.URLMust(
204 "smtp://example.com/[email protected]&toAddresses=" +
205 url.QueryEscape("Name <[email protected]>"),
206 ))).To(gomega.Succeed())
207 gomega.Expect(config.ToAddresses).To(gomega.Equal([]string{"Name <[email protected]>"}))
208 })
209
210 ginkgo.It("should decode a password that contains @ and spaces", func() {
211 config := &Config{}
212 gomega.Expect(config.SetURL(testutils.URLMust(
213 "smtp://user:p%40ss%[email protected]:587/[email protected]&[email protected]",
214 ))).To(gomega.Succeed())
215 gomega.Expect(config.Username).To(gomega.Equal("user"))
216 gomega.Expect(config.Password).To(gomega.Equal("p@ss word"))
217 })
218 })
219
220 ginkgo.Describe("restorePlusAddresses", func() {
221 ginkgo.It("should restore plus tags that URL parsing turned into spaces", func() {
222 config := &Config{
223 FromAddress: "sender [email protected]",
224 ToAddresses: []string{"rec1 [email protected]", "[email protected]"},
225 }
226 config.restorePlusAddresses()
227 gomega.Expect(config.FromAddress).To(gomega.Equal("[email protected]"))
228 gomega.Expect(config.ToAddresses).To(gomega.Equal([]string{
229 "[email protected]",
230 "[email protected]",
231 }))
232 })
233
234 ginkgo.It("should keep display-name spaces and restore plus tags in the addr-spec", func() {
235 config := &Config{
236 FromAddress: "Sender Name <sender [email protected]>",
237 ToAddresses: []string{"Ops Team <rec1 [email protected]>"},
238 }
239 config.restorePlusAddresses()
240 gomega.Expect(config.FromAddress).To(gomega.Equal("Sender Name <[email protected]>"))
241 gomega.Expect(config.ToAddresses).To(gomega.Equal([]string{
242 "Ops Team <[email protected]>",
243 }))
244 })
245 })
246
247 ginkgo.Describe("newTLSConfig", func() {
248 ginkgo.It("should set the server name and TLS 1.2 minimum without a version ceiling", func() {
249 got := newTLSConfig(&Config{Host: "mail.example.com"})
250 gomega.Expect(got.ServerName).To(gomega.Equal("mail.example.com"))
251 gomega.Expect(got.MinVersion).To(gomega.Equal(uint16(tls.VersionTLS12)))
252 gomega.Expect(got.MaxVersion).To(gomega.BeZero())
253 gomega.Expect(got.InsecureSkipVerify).To(gomega.BeFalse())
254 })
255
256 ginkgo.It("should skip certificate verification when configured", func() {
257 got := newTLSConfig(&Config{Host: "mail.example.com", SkipTLSVerify: true})
258 gomega.Expect(got.InsecureSkipVerify).To(gomega.BeTrue())
259 })
260 })
261
262 ginkgo.When("applying the default props", func() {
263 ginkgo.It("should apply the tagged timeout default", func() {
264 config := &Config{}
265 resolver := format.NewPropKeyResolver(config)
266
267 gomega.Expect(resolver.SetDefaultProps(config)).To(gomega.Succeed())
268 gomega.Expect(config.Timeout).To(gomega.Equal(defaultTimeout))
269 gomega.Expect(config.Subject).To(gomega.BeEmpty())
270 })
271 })
272
273 ginkgo.When("serializing the timeout", func() {
274 ginkgo.It("should render it as a duration rather than a nanosecond count", func() {
275 config := &Config{Timeout: 10 * time.Second}
276 resolver := format.NewPropKeyResolver(config)
277
278 value, err := resolver.Get("timeout")
279 gomega.Expect(err).NotTo(gomega.HaveOccurred())
280 gomega.Expect(value).To(gomega.Equal("10s"))
281 })
282 })
283 })