| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net" |
| 7 |
"net/url" |
| 8 |
"strconv" |
| 9 |
|
| 10 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 11 |
gomega "github.com/onsi/gomega" |
| 12 |
|
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 |
) |
| 15 |
|
| 16 |
var _ = ginkgo.Describe("dialClient", func() { |
| 17 |
ginkgo.It("should fail when the context is already canceled", func() { |
| 18 |
ctx, cancel := context.WithCancel(context.Background()) |
| 19 |
cancel() |
| 20 |
|
| 21 |
_, err := dialClient(ctx, &Config{ |
| 22 |
Host: "example.com", |
| 23 |
Port: 25, |
| 24 |
FromAddress: "[email protected]", |
| 25 |
ToAddresses: []string{"[email protected]"}, |
| 26 |
}, nil) |
| 27 |
gomega.Expect(err).To(matchFailure(FailConnectToServer)) |
| 28 |
gomega.Expect(err).To(gomega.MatchError(context.Canceled)) |
| 29 |
}) |
| 30 |
|
| 31 |
ginkgo.It("should fail when the host cannot be reached", func() { |
| 32 |
_, err := dialClient(context.Background(), &Config{ |
| 33 |
Host: "127.0.0.1", |
| 34 |
Port: 1, |
| 35 |
}, nil) |
| 36 |
gomega.Expect(err).To(matchFailure(FailConnectToServer)) |
| 37 |
}) |
| 38 |
|
| 39 |
ginkgo.It("should create a client against a greeting server", func() { |
| 40 |
address, _, stop := startGreetingServer() |
| 41 |
defer stop() |
| 42 |
|
| 43 |
host, portStr, err := net.SplitHostPort(address) |
| 44 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 45 |
|
| 46 |
port, err := strconv.ParseUint(portStr, 10, 16) |
| 47 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 48 |
|
| 49 |
client, err := dialClient(context.Background(), &Config{Host: host, Port: uint16(port)}, nil) |
| 50 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 51 |
gomega.Expect(client).NotTo(gomega.BeNil()) |
| 52 |
gomega.Expect(client.Close()).To(gomega.Succeed()) |
| 53 |
}) |
| 54 |
|
| 55 |
ginkgo.It("should use a custom dialer", func() { |
| 56 |
address, _, stop := startGreetingServer() |
| 57 |
defer stop() |
| 58 |
|
| 59 |
host, portStr, err := net.SplitHostPort(address) |
| 60 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 61 |
|
| 62 |
port, err := strconv.ParseUint(portStr, 10, 16) |
| 63 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 64 |
|
| 65 |
var gotNetwork, gotAddr string |
| 66 |
|
| 67 |
dial := func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 68 |
gotNetwork = network |
| 69 |
gotAddr = addr |
| 70 |
|
| 71 |
return (&net.Dialer{}).DialContext(ctx, network, addr) |
| 72 |
} |
| 73 |
|
| 74 |
client, err := dialClient( |
| 75 |
context.Background(), |
| 76 |
&Config{Host: host, Port: uint16(port)}, |
| 77 |
dial, |
| 78 |
) |
| 79 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 80 |
gomega.Expect(client).NotTo(gomega.BeNil()) |
| 81 |
gomega.Expect(gotNetwork).To(gomega.Equal("tcp")) |
| 82 |
gomega.Expect(gotAddr).To(gomega.Equal(net.JoinHostPort(host, portStr))) |
| 83 |
gomega.Expect(client.Close()).To(gomega.Succeed()) |
| 84 |
}) |
| 85 |
|
| 86 |
ginkgo.It("should surface a custom dialer error as FailConnectToServer", func() { |
| 87 |
blocked := errors.New("destination blocked") |
| 88 |
_, err := dialClient( |
| 89 |
context.Background(), |
| 90 |
&Config{Host: "mail.example.com", Port: 587}, |
| 91 |
func(_ context.Context, _, _ string) (net.Conn, error) { |
| 92 |
return nil, blocked |
| 93 |
}, |
| 94 |
) |
| 95 |
gomega.Expect(err).To(matchFailure(FailConnectToServer)) |
| 96 |
gomega.Expect(err).To(gomega.MatchError(blocked)) |
| 97 |
}) |
| 98 |
}) |
| 99 |
|
| 100 |
var _ = ginkgo.Describe("SetDialContext", func() { |
| 101 |
ginkgo.It("should fail Send when the custom dialer blocks the destination", func() { |
| 102 |
svc := &Service{} |
| 103 |
serviceURL, err := url.Parse( |
| 104 |
"smtp://mail.example.com:587/[email protected]&[email protected]", |
| 105 |
) |
| 106 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 107 |
gomega.Expect(svc.Initialize(serviceURL, logger)).To(gomega.Succeed()) |
| 108 |
|
| 109 |
blocked := errors.New("destination blocked") |
| 110 |
|
| 111 |
svc.SetDialContext(func(_ context.Context, _, _ string) (net.Conn, error) { |
| 112 |
return nil, blocked |
| 113 |
}) |
| 114 |
|
| 115 |
err = svc.Send("hello", nil) |
| 116 |
gomega.Expect(err).To(matchFailure(FailGetSMTPClient)) |
| 117 |
gomega.Expect(err).To(gomega.MatchError(blocked)) |
| 118 |
}) |
| 119 |
|
| 120 |
ginkgo.It("should satisfy DialContextSetter", func() { |
| 121 |
var setter types.DialContextSetter = &Service{} |
| 122 |
gomega.Expect(setter).NotTo(gomega.BeNil()) |
| 123 |
}) |
| 124 |
}) |