| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/mock" |
| 7 |
|
| 8 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 9 |
gomega "github.com/onsi/gomega" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/internal/failures" |
| 12 |
failuresmocks "github.com/nicholas-fedor/shoutrrr/internal/failures/mocks" |
| 13 |
) |
| 14 |
|
| 15 |
var _ = ginkgo.Describe("fail", func() { |
| 16 |
ginkgo.It("should wrap the underlying error with the selected ID", func() { |
| 17 |
underlying := failuresmocks.NewMockFailure(ginkgo.GinkgoT()) |
| 18 |
underlying.On("Error").Return("boom") |
| 19 |
|
| 20 |
got := fail(FailHandshake, underlying) |
| 21 |
gomega.Expect(got.ID()).To(gomega.Equal(FailHandshake)) |
| 22 |
gomega.Expect(got.Unwrap()).To(gomega.Equal(underlying)) |
| 23 |
gomega.Expect(got.Error()).To(gomega.ContainSubstring("server did not accept the handshake")) |
| 24 |
gomega.Expect(got.Error()).To(gomega.ContainSubstring("boom")) |
| 25 |
}) |
| 26 |
|
| 27 |
ginkgo.It("should interpolate format verbs from extra arguments", func() { |
| 28 |
got := fail(FailAuthType, nil, "bogus") |
| 29 |
gomega.Expect(got.ID()).To(gomega.Equal(FailAuthType)) |
| 30 |
gomega.Expect(got.Error()).To(gomega.Equal("invalid authentication method 'bogus'")) |
| 31 |
}) |
| 32 |
|
| 33 |
ginkgo.It("should interpolate the recipient into FailSendRecipient", func() { |
| 34 |
got := fail(FailSendRecipient, errors.New("nope"), "[email protected]") |
| 35 |
gomega.Expect(got.ID()).To(gomega.Equal(FailSendRecipient)) |
| 36 |
gomega.Expect(got.Error()).To(gomega.ContainSubstring(`error sending message to recipient "[email protected]"`)) |
| 37 |
}) |
| 38 |
|
| 39 |
ginkgo.It("should use the unknown message for an unrecognized ID", func() { |
| 40 |
got := fail(failures.FailureID(999), nil) |
| 41 |
gomega.Expect(got.ID()).To(gomega.Equal(failures.FailureID(999))) |
| 42 |
gomega.Expect(got.Error()).To(gomega.Equal("an unknown error occurred")) |
| 43 |
}) |
| 44 |
|
| 45 |
ginkgo.DescribeTable("should select a descriptive message for each failure ID", |
| 46 |
func(id failures.FailureID, substring string) { |
| 47 |
got := fail(id, nil) |
| 48 |
gomega.Expect(got.ID()).To(gomega.Equal(id)) |
| 49 |
gomega.Expect(got.Error()).To(gomega.ContainSubstring(substring)) |
| 50 |
}, |
| 51 |
ginkgo.Entry("FailUnknown", FailUnknown, "an unknown error occurred"), |
| 52 |
ginkgo.Entry("FailGetSMTPClient", FailGetSMTPClient, "error getting SMTP client"), |
| 53 |
ginkgo.Entry("FailConnectToServer", FailConnectToServer, "error connecting to server"), |
| 54 |
ginkgo.Entry("FailCreateSMTPClient", FailCreateSMTPClient, "error creating smtp client"), |
| 55 |
ginkgo.Entry("FailEnableStartTLS", FailEnableStartTLS, "error enabling StartTLS"), |
| 56 |
ginkgo.Entry("FailAuthenticating", FailAuthenticating, "error authenticating"), |
| 57 |
ginkgo.Entry("FailClosingSession", FailClosingSession, "error closing session"), |
| 58 |
ginkgo.Entry("FailPlainHeader", FailPlainHeader, "error writing plain header"), |
| 59 |
ginkgo.Entry("FailHTMLHeader", FailHTMLHeader, "error writing HTML header"), |
| 60 |
ginkgo.Entry("FailMultiEndHeader", FailMultiEndHeader, "error writing multipart end header"), |
| 61 |
ginkgo.Entry("FailMessageTemplate", FailMessageTemplate, "error applying message template"), |
| 62 |
ginkgo.Entry("FailMessageRaw", FailMessageRaw, "error writing message"), |
| 63 |
ginkgo.Entry("FailSetSender", FailSetSender, "error setting MAIL FROM"), |
| 64 |
ginkgo.Entry("FailSetRecipient", FailSetRecipient, "error setting RCPT"), |
| 65 |
ginkgo.Entry("FailOpenDataStream", FailOpenDataStream, "error starting DATA"), |
| 66 |
ginkgo.Entry("FailWriteHeaders", FailWriteHeaders, "error writing message headers"), |
| 67 |
ginkgo.Entry("FailCloseDataStream", FailCloseDataStream, "error closing message stream"), |
| 68 |
ginkgo.Entry("FailApplySendParams", FailApplySendParams, "error applying params to send config"), |
| 69 |
ginkgo.Entry("FailHandshake", FailHandshake, "server did not accept the handshake"), |
| 70 |
ginkgo.Entry("FailResetSession", FailResetSession, "error resetting session between recipients"), |
| 71 |
) |
| 72 |
|
| 73 |
ginkgo.It("should compare equal by ID via errors.Is", func() { |
| 74 |
gomega.Expect(fail(FailHandshake, errors.New("one"))).To(gomega.MatchError(fail(FailHandshake, nil))) |
| 75 |
gomega.Expect(fail(FailHandshake, nil)).NotTo(gomega.MatchError(fail(FailAuthenticating, nil))) |
| 76 |
}) |
| 77 |
|
| 78 |
ginkgo.It("should not require the mock Error method when only unwrapping", func() { |
| 79 |
underlying := failuresmocks.NewMockFailure(ginkgo.GinkgoT()) |
| 80 |
underlying.On("Error").Return("unused").Maybe() |
| 81 |
underlying.On("ID").Return(FailUnknown).Maybe() |
| 82 |
underlying.On("Unwrap").Return(nil).Maybe() |
| 83 |
underlying.On("Is", mock.Anything).Return(false).Maybe() |
| 84 |
|
| 85 |
got := fail(FailConnectToServer, underlying) |
| 86 |
gomega.Expect(got.Unwrap()).To(gomega.Equal(underlying)) |
| 87 |
}) |
| 88 |
}) |