Skip to content

Commit

Permalink
test: split standard failures and fix smtp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Dec 20, 2020
1 parent 15960cf commit fd12d74
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 48 deletions.
13 changes: 0 additions & 13 deletions internal/failures/failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import "fmt"
// FailureID is a number to be used to identify a specific error
type FailureID int

const (
// FailTestSetup is FailureID used to represent an error that is part of the setup for tests
FailTestSetup FailureID = -1
)

type failure struct {
message string
id FailureID
Expand Down Expand Up @@ -48,12 +43,4 @@ func Wrap(message string, id FailureID, wrappedError error, v ...interface{}) Fa
}
}

// IsTestSetupFailure checks whether the given failure is due to the test setup being broken
func IsTestSetupFailure(f Failure) (string, bool) {
if f != nil && f.ID() == FailTestSetup {
return fmt.Sprintf("test setup failed: %s", f.Error()), true
}
return "", false
}

var _ error = &failure{}
61 changes: 26 additions & 35 deletions pkg/services/smtp/smtp_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package smtp

import (
"fmt"
"github.com/containrrr/shoutrrr/pkg/format"
"log"
"net/smtp"
"net/url"
Expand All @@ -13,6 +11,8 @@ import (

"github.com/containrrr/shoutrrr/internal/failures"
"github.com/containrrr/shoutrrr/internal/testutils"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/util"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -50,14 +50,11 @@ var _ = Describe("the SMTP service", func() {

config := &Config{}
pkr := format.NewPropKeyResolver(config)
err = config.SetURL(url)
err = config.setURL(&pkr, url)
Expect(err).NotTo(HaveOccurred(), "verifying")

fmt.Printf("%v", pkr.QueryFields())
outputURL := config.GetURL()

fmt.Println(outputURL.String())

Expect(outputURL.String()).To(Equal(testURL))

})
Expand Down Expand Up @@ -125,39 +122,34 @@ var _ = Describe("the SMTP service", func() {
It("should fail when writing multipart plain header", func() {
writer := testutils.CreateFailWriter(1)
err := service.writeMultipartMessage(writer, message)
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailPlainHeader))
})

It("should fail when writing multipart plain message", func() {
writer := testutils.CreateFailWriter(2)
err := service.writeMultipartMessage(writer, message)
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailMessageRaw))
})

It("should fail when writing multipart HTML header", func() {
writer := testutils.CreateFailWriter(4)
err := service.writeMultipartMessage(writer, message)
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailHTMLHeader))
})

It("should fail when writing multipart HTML message", func() {
writer := testutils.CreateFailWriter(5)
err := service.writeMultipartMessage(writer, message)
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailMessageRaw))
})

It("should fail when writing multipart end header", func() {
writer := testutils.CreateFailWriter(6)
err := service.writeMultipartMessage(writer, message)
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailMultiEndHeader))
})
Expand All @@ -168,7 +160,6 @@ var _ = Describe("the SMTP service", func() {
Expect(e).ToNot(HaveOccurred())

err := service.writeMessagePart(writer, message, "dummy")
fmt.Printf("%+v\n", err)
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailMessageTemplate))
})
Expand Down Expand Up @@ -216,7 +207,7 @@ var _ = Describe("the SMTP service", func() {
"250 Data OK",
"221 OK",
}, "<pre>{{ .message }}</pre>", "{{ .message }}")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -239,7 +230,7 @@ var _ = Describe("the SMTP service", func() {
"250 Data OK",
"221 OK",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -262,7 +253,7 @@ var _ = Describe("the SMTP service", func() {
"250 Data OK",
"221 OK",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -282,7 +273,7 @@ var _ = Describe("the SMTP service", func() {
"250 8BITMIME",
"502 That's too hard",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -293,12 +284,12 @@ var _ = Describe("the SMTP service", func() {
It("should fail when authentication type is invalid", func() {
testURL := "smtp://example.com:2225/?startTLS=no&auth=bad&[email protected]&[email protected]&useHTML=no"
err := testIntegration(testURL, []string{}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expect(err).To(HaveOccurred())
Expect(err.ID()).To(Equal(FailAuthType))
Expect(err.ID()).To(Equal(standard.FailServiceInit))
})

It("should fail when not being able to use authentication type", func() {
Expand All @@ -310,7 +301,7 @@ var _ = Describe("the SMTP service", func() {
"250 8BITMIME",
"504 Liar",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -327,7 +318,7 @@ var _ = Describe("the SMTP service", func() {
"250 8BITMIME",
"551 I don't know you",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -342,7 +333,7 @@ var _ = Describe("the SMTP service", func() {
"250 Sender OK",
"553 She doesn't want to be disturbed",
})
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -358,7 +349,7 @@ var _ = Describe("the SMTP service", func() {
"250 Receiver OK",
"554 Nah I'm fine thanks",
})
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -375,7 +366,7 @@ var _ = Describe("the SMTP service", func() {
"354 Go ahead",
"554 Such garbage!",
})
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -396,7 +387,7 @@ var _ = Describe("the SMTP service", func() {
"250 Data OK",
"502 You can't quit, you're fired!",
}, "", "")
if msg, test := failures.IsTestSetupFailure(err); test {
if msg, test := standard.IsTestSetupFailure(err); test {
Skip(msg)
return
}
Expand All @@ -408,19 +399,19 @@ var _ = Describe("the SMTP service", func() {
})
})

func testSendRecipient(testURL string, responses []string) failure {
func testSendRecipient(testURL string, responses []string) failures.Failure {
serviceURL, err := url.Parse(testURL)
if err != nil {
return failures.Wrap("error parsing URL", failures.FailTestSetup, err)
return standard.Failure(standard.FailParseURL, err)
}

err = service.Initialize(serviceURL, logger)
if err != nil {
return failures.Wrap("error parsing URL", failures.FailTestSetup, err)
return failures.Wrap("error parsing URL", standard.FailTestSetup, err)
}

if err := service.SetTemplateString("plain", "{{.message}}"); err != nil {
return failures.Wrap("error setting plain template", failures.FailTestSetup, err)
return failures.Wrap("error setting plain template", standard.FailTestSetup, err)
}

textCon, tcfaker := testutils.CreateTextConFaker(responses, "\r\n")
Expand All @@ -444,26 +435,26 @@ func testSendRecipient(testURL string, responses []string) failure {
return nil
}

func testIntegration(testURL string, responses []string, htmlTemplate string, plainTemplate string) failure {
func testIntegration(testURL string, responses []string, htmlTemplate string, plainTemplate string) failures.Failure {

serviceURL, err := url.Parse(testURL)
if err != nil {
return failures.Wrap("error parsing URL", failures.FailTestSetup, err)
return standard.Failure(standard.FailParseURL, err)
}

err = service.Initialize(serviceURL, logger)
if err != nil {
return failures.Wrap("error parsing URL", failures.FailTestSetup, err)
if err = service.Initialize(serviceURL, logger); err != nil {
return standard.Failure(standard.FailServiceInit, err)
}

if htmlTemplate != "" {
if err := service.SetTemplateString("HTML", htmlTemplate); err != nil {
return failures.Wrap("error setting HTML template", failures.FailTestSetup, err)
return failures.Wrap("error setting HTML template", standard.FailTestSetup, err)
}
}

if plainTemplate != "" {
if err := service.SetTemplateString("plain", plainTemplate); err != nil {
return failures.Wrap("error setting plain template", failures.FailTestSetup, err)
return failures.Wrap("error setting plain template", standard.FailTestSetup, err)
}
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/services/standard/standard_failures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package standard

import (
"fmt"
f "github.com/containrrr/shoutrrr/internal/failures"
)

const (
// FailTestSetup is the FailureID used to represent an error that is part of the setup for tests
FailTestSetup f.FailureID = -1
// FailParseURL is the FailureID used to represent failing to parse the service URL
FailParseURL f.FailureID = -2
// FailServiceInit is the FailureID used to represent failure of a service.Initialize method
FailServiceInit f.FailureID = -3
// FailUnknown is the default FailureID
FailUnknown f.FailureID = iota
)

func Failure(failureID f.FailureID, err error, v ...interface{}) f.Failure {
messages := map[int]string{
int(FailParseURL): "error parsing Service URL",
int(FailUnknown): "an unknown error occurred",
}

msg := messages[int(failureID)]
if msg == "" {
msg = messages[int(FailUnknown)]
}

return f.Wrap(msg, failureID, err, v...)
}

type failureLike interface {
f.Failure
}

// IsTestSetupFailure checks whether the given failure is due to the test setup being broken
func IsTestSetupFailure(failure failureLike) (string, bool) {
if failure != nil && failure.ID() == FailTestSetup {
return fmt.Sprintf("test setup failed: %s", failure.Error()), true
}
return "", false
}

0 comments on commit fd12d74

Please sign in to comment.