| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"log" |
| 7 |
"net/smtp" |
| 8 |
"net/url" |
| 9 |
"time" |
| 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 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 |
typesmocks "github.com/nicholas-fedor/shoutrrr/pkg/types/mocks" |
| 18 |
) |
| 19 |
|
| 20 |
var _ = ginkgo.Describe("Service", func() { |
| 21 |
ginkgo.BeforeEach(func() { |
| 22 |
service = &Service{} |
| 23 |
}) |
| 24 |
|
| 25 |
ginkgo.Describe("GetID", func() { |
| 26 |
ginkgo.It("should return smtp", func() { |
| 27 |
gomega.Expect(service.GetID()).To(gomega.Equal(Scheme)) |
| 28 |
}) |
| 29 |
}) |
| 30 |
|
| 31 |
ginkgo.Describe("Initialize", func() { |
| 32 |
ginkgo.It("should apply the default SMTP port and timeout when omitted", func() { |
| 33 |
mockLogger := typesmocks.NewMockStdLogger(ginkgo.GinkgoT()) |
| 34 |
serviceURL := testutils.URLMust( |
| 35 |
"smtp://example.com/[email protected]&[email protected]", |
| 36 |
) |
| 37 |
|
| 38 |
gomega.Expect(service.Initialize(serviceURL, mockLogger)).To(gomega.Succeed()) |
| 39 |
gomega.Expect(service.Config.Port).To(gomega.Equal(uint16(DefaultSMTPPort))) |
| 40 |
gomega.Expect(service.Config.Timeout).To(gomega.Equal(defaultTimeout)) |
| 41 |
}) |
| 42 |
|
| 43 |
ginkgo.It("should infer AuthPlain when a username is present", func() { |
| 44 |
serviceURL := testutils.URLMust( |
| 45 |
"smtp://user:[email protected]/[email protected]&[email protected]", |
| 46 |
) |
| 47 |
|
| 48 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 49 |
gomega.Expect(service.Config.Auth).To(gomega.Equal(AuthTypes.Plain)) |
| 50 |
}) |
| 51 |
|
| 52 |
ginkgo.It("should infer AuthNone when no username is present", func() { |
| 53 |
serviceURL := testutils.URLMust( |
| 54 |
"smtp://example.com/[email protected]&[email protected]", |
| 55 |
) |
| 56 |
|
| 57 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 58 |
gomega.Expect(service.Config.Auth).To(gomega.Equal(AuthTypes.None)) |
| 59 |
}) |
| 60 |
|
| 61 |
ginkgo.It("should keep an explicit auth method", func() { |
| 62 |
serviceURL := testutils.URLMust( |
| 63 |
"smtp://user:[email protected]/?auth=Login&[email protected]&[email protected]", |
| 64 |
) |
| 65 |
|
| 66 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 67 |
gomega.Expect(service.Config.Auth).To(gomega.Equal(AuthTypes.Login)) |
| 68 |
}) |
| 69 |
|
| 70 |
ginkgo.It("should use the specified timeout", func() { |
| 71 |
serviceURL := testutils.URLMust(modifyURL(baseNoAuthURL, map[string]string{"timeout": "5s"})) |
| 72 |
|
| 73 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 74 |
gomega.Expect(service.Config.Timeout).To(gomega.Equal(5 * time.Second)) |
| 75 |
}) |
| 76 |
|
| 77 |
ginkgo.It("should fail when the configuration URL is invalid", func() { |
| 78 |
serviceURL := testutils.URLMust("smtp://example.com/") |
| 79 |
|
| 80 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.MatchError(ErrFromAddressMissing)) |
| 81 |
}) |
| 82 |
|
| 83 |
ginkgo.It("should keep the default port when the URL port is out of range", func() { |
| 84 |
serviceURL := testutils.URLMust( |
| 85 |
"smtp://example.com:99999/[email protected]&[email protected]", |
| 86 |
) |
| 87 |
|
| 88 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 89 |
gomega.Expect(service.Config.Port).To(gomega.Equal(uint16(DefaultSMTPPort))) |
| 90 |
}) |
| 91 |
}) |
| 92 |
|
| 93 |
ginkgo.Describe("Send", func() { |
| 94 |
ginkgo.It("should use the default timeout when Timeout is non-positive", func() { |
| 95 |
gomega.Expect(effectiveTimeout(0)).To(gomega.Equal(defaultTimeout)) |
| 96 |
gomega.Expect(effectiveTimeout(-time.Second)).To(gomega.Equal(defaultTimeout)) |
| 97 |
gomega.Expect(effectiveTimeout(5 * time.Second)).To(gomega.Equal(5 * time.Second)) |
| 98 |
}) |
| 99 |
|
| 100 |
ginkgo.It("should fail when the service is not configured with a reachable host", func() { |
| 101 |
svc := Service{Config: &Config{Host: "127.0.0.1", Port: 1, Timeout: time.Millisecond}} |
| 102 |
gomega.Expect(svc.Send("test message", nil)).To(matchFailure(FailGetSMTPClient)) |
| 103 |
|
| 104 |
svc.Config.Encryption = EncMethods.ImplicitTLS |
| 105 |
gomega.Expect(svc.Send("test message", nil)).To(matchFailure(FailGetSMTPClient)) |
| 106 |
}) |
| 107 |
|
| 108 |
ginkgo.It("should fail when an invalid param is passed", func() { |
| 109 |
svc := Service{Config: &Config{}} |
| 110 |
gomega.Expect(svc.Send("test message", &types.Params{"invalid": "value"})). |
| 111 |
To(matchFailure(FailApplySendParams)) |
| 112 |
}) |
| 113 |
|
| 114 |
ginkgo.It("should log a warning when SkipTLSVerify is enabled", func() { |
| 115 |
var buf bytes.Buffer |
| 116 |
|
| 117 |
testLogger := log.New(&buf, "", 0) |
| 118 |
svc := &Service{} |
| 119 |
gomega.Expect(svc.Initialize(testutils.URLMust(baseNoAuthURL), testLogger)).To(gomega.Succeed()) |
| 120 |
svc.Config.SkipTLSVerify = true |
| 121 |
svc.Config.Host = "127.0.0.1" |
| 122 |
svc.Config.Port = 1 |
| 123 |
svc.Config.Timeout = time.Millisecond |
| 124 |
|
| 125 |
gomega.Expect(svc.Send("test message", nil)).To(matchFailure(FailGetSMTPClient)) |
| 126 |
gomega.Expect(buf.String()). |
| 127 |
To(gomega.ContainSubstring("Warning: TLS verification is disabled, making connections insecure")) |
| 128 |
}) |
| 129 |
|
| 130 |
ginkgo.It("should apply send params before dialing", func() { |
| 131 |
address, firstByte, stop := startGreetingServer() |
| 132 |
defer stop() |
| 133 |
|
| 134 |
serviceURL := testutils.URLMust( |
| 135 |
"smtp://" + address + "/?encryption=ImplicitTLS&auth=none&usestarttls=no" + |
| 136 |
"&[email protected]&[email protected]&timeout=5s", |
| 137 |
) |
| 138 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 139 |
|
| 140 |
gomega.Expect(service.Send("test message", &types.Params{"encryption": "None"})). |
| 141 |
To(matchFailure(FailHandshake)) |
| 142 |
gomega.Eventually(firstByte).Should(gomega.Receive(gomega.Equal(byte('E')))) |
| 143 |
}) |
| 144 |
|
| 145 |
ginkgo.It("should time out the handshake using the configured deadline", func() { |
| 146 |
address, stop := startHangingGreetingServer() |
| 147 |
defer stop() |
| 148 |
|
| 149 |
sendTimeout := 200 * time.Millisecond |
| 150 |
serviceURL := testutils.URLMust( |
| 151 |
"smtp://" + address + "/?auth=none&usestarttls=no" + |
| 152 |
"&[email protected]&[email protected]&timeout=" + |
| 153 |
sendTimeout.String(), |
| 154 |
) |
| 155 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 156 |
|
| 157 |
start := time.Now() |
| 158 |
err := service.Send("test message", nil) |
| 159 |
|
| 160 |
gomega.Expect(time.Since(start)).To(gomega.BeNumerically("<", sendTimeout+2*time.Second)) |
| 161 |
gomega.Expect(err).To(matchFailure(FailHandshake)) |
| 162 |
}) |
| 163 |
}) |
| 164 |
|
| 165 |
ginkgo.Describe("SendContext", func() { |
| 166 |
ginkgo.It("should fail when the caller context is already canceled", func() { |
| 167 |
ctx, cancel := context.WithCancel(context.Background()) |
| 168 |
cancel() |
| 169 |
|
| 170 |
svc := Service{Config: &Config{Host: "example.com", Port: 25, Timeout: time.Second}} |
| 171 |
err := svc.SendContext(ctx, "test message", nil) |
| 172 |
gomega.Expect(err).To(matchFailure(FailGetSMTPClient)) |
| 173 |
gomega.Expect(err).To(gomega.MatchError(context.Canceled)) |
| 174 |
}) |
| 175 |
|
| 176 |
ginkgo.It("should time out the handshake using the caller context deadline", func() { |
| 177 |
address, stop := startHangingGreetingServer() |
| 178 |
defer stop() |
| 179 |
|
| 180 |
parentTimeout := 200 * time.Millisecond |
| 181 |
serviceURL := testutils.URLMust( |
| 182 |
"smtp://" + address + "/?auth=none&usestarttls=no" + |
| 183 |
"&[email protected]&[email protected]&timeout=10s", |
| 184 |
) |
| 185 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 186 |
|
| 187 |
ctx, cancel := context.WithTimeout(context.Background(), parentTimeout) |
| 188 |
defer cancel() |
| 189 |
|
| 190 |
start := time.Now() |
| 191 |
err := service.SendContext(ctx, "test message", nil) |
| 192 |
|
| 193 |
gomega.Expect(time.Since(start)).To(gomega.BeNumerically("<", parentTimeout+2*time.Second)) |
| 194 |
gomega.Expect(err).To(matchFailure(FailHandshake)) |
| 195 |
}) |
| 196 |
}) |
| 197 |
|
| 198 |
ginkgo.When("SkipTLSVerify is enabled on the parsed URL", func() { |
| 199 |
ginkgo.It("should log a security warning when Send is called", func() { |
| 200 |
localService := &Service{} |
| 201 |
testURL := modifyURL(baseNoAuthURL, map[string]string{"skiptlsverify": "yes"}) |
| 202 |
|
| 203 |
var buf bytes.Buffer |
| 204 |
|
| 205 |
testLogger := log.New(&buf, "", 0) |
| 206 |
gomega.Expect(localService.Initialize(testutils.URLMust(testURL), testLogger)).To(gomega.Succeed()) |
| 207 |
localService.Config.Host = "127.0.0.1" |
| 208 |
localService.Config.Port = 1 |
| 209 |
localService.Config.Timeout = time.Millisecond |
| 210 |
|
| 211 |
gomega.Expect(localService.Send("test message", nil)).To(matchFailure(FailGetSMTPClient)) |
| 212 |
gomega.Expect(buf.String()). |
| 213 |
To(gomega.ContainSubstring("Warning: TLS verification is disabled, making connections insecure")) |
| 214 |
}) |
| 215 |
}) |
| 216 |
|
| 217 |
ginkgo.When("send params change the message content", func() { |
| 218 |
ginkgo.It("should build the headers from the params", func() { |
| 219 |
gomega.Expect(service.Initialize(testutils.URLMust(baseNoAuthURL), logger)).To(gomega.Succeed()) |
| 220 |
|
| 221 |
config := service.Config.Clone() |
| 222 |
gomega.Expect(service.propKeyResolver.UpdateConfigFromParams(&config, &types.Params{ |
| 223 |
"fromaddress": "[email protected]", |
| 224 |
"fromname": "Override", |
| 225 |
"subject": "Overridden", |
| 226 |
"usehtml": "yes", |
| 227 |
})).To(gomega.Succeed()) |
| 228 |
|
| 229 |
textCon, tcfaker := testutils.CreateTextConFaker([]string{ |
| 230 |
"250-mx.google.com at your service", |
| 231 |
"250 8BITMIME", |
| 232 |
"250 Sender OK", |
| 233 |
"250 Receiver OK", |
| 234 |
"354 Go ahead", |
| 235 |
"250 Data OK", |
| 236 |
"221 OK", |
| 237 |
}, "\r\n") |
| 238 |
client := &smtp.Client{Text: textCon} |
| 239 |
fakeTLSEnabled(client, "example.com") |
| 240 |
|
| 241 |
gomega.Expect(runSMTPSession(service, client, &config)).To(gomega.Succeed()) |
| 242 |
|
| 243 |
received := tcfaker.GetClientSentences() |
| 244 |
gomega.Expect(received).To(gomega.ContainElement("MAIL FROM:<[email protected]> BODY=8BITMIME")) |
| 245 |
gomega.Expect(received).To(gomega.ContainElement(`From: "Override" <[email protected]>`)) |
| 246 |
gomega.Expect(received).To(gomega.ContainElement("Subject: Overridden")) |
| 247 |
gomega.Expect(received). |
| 248 |
To(gomega.ContainElement(gomega.HavePrefix("Content-Type: multipart/alternative; boundary="))) |
| 249 |
}) |
| 250 |
}) |
| 251 |
|
| 252 |
ginkgo.When("running E2E tests", func() { |
| 253 |
ginkgo.It("should work without errors", func() { |
| 254 |
if envSMTPURL == "" { |
| 255 |
ginkgo.Skip("environment not set up for E2E testing") |
| 256 |
} |
| 257 |
|
| 258 |
serviceURL, err := url.Parse(envSMTPURL) |
| 259 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 260 |
gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 261 |
gomega.Expect(service.Send("this is an integration test", nil)).To(gomega.Succeed()) |
| 262 |
}) |
| 263 |
}) |
| 264 |
|
| 265 |
ginkgo.When("configuring timeout via params", func() { |
| 266 |
ginkgo.It("should use the specified timeout", func() { |
| 267 |
config := &Config{} |
| 268 |
resolver := format.NewPropKeyResolver(config) |
| 269 |
params := types.Params{"timeout": "1h2m3s"} |
| 270 |
|
| 271 |
gomega.Expect(resolver.UpdateConfigFromParams(config, ¶ms)).To(gomega.Succeed()) |
| 272 |
gomega.Expect(config.Timeout).To(gomega.Equal(1*time.Hour + 2*time.Minute + 3*time.Second)) |
| 273 |
}) |
| 274 |
|
| 275 |
ginkgo.It("should reject a bare non-zero timeout value", func() { |
| 276 |
config := &Config{Timeout: 5 * time.Second} |
| 277 |
resolver := format.NewPropKeyResolver(config) |
| 278 |
params := types.Params{"timeout": "10"} |
| 279 |
|
| 280 |
gomega.Expect(resolver.UpdateConfigFromParams(config, ¶ms)). |
| 281 |
To(gomega.MatchError(format.ErrParseDurationFailed)) |
| 282 |
gomega.Expect(config.Timeout).To(gomega.Equal(5 * time.Second)) |
| 283 |
}) |
| 284 |
}) |
| 285 |
}) |