Skip to content

Commit

Permalink
tests: add service compliance test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Apr 2, 2021
1 parent 31ce8d4 commit b8c4d0c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pkg/services/gotify/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Service struct {
standard.Standard
config *Config
pkr format.PropKeyResolver
client *http.Client
Client *http.Client
}

// Initialize loads ServiceConfig from configURL and sets logger for this Service
Expand All @@ -33,7 +33,7 @@ func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error
service.pkr = format.NewPropKeyResolver(service.config)
err := service.config.SetURL(configURL)

service.client = &http.Client{
service.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
// If DisableTLS is specified, we might still need to disable TLS verification
Expand Down Expand Up @@ -106,7 +106,7 @@ func (service *Service) Send(message string, params *types.Params) error {
return err
}
jsonBuffer := bytes.NewBuffer(jsonBody)
resp, err := service.client.Post(postURL, "application/json", jsonBuffer)
resp, err := service.Client.Post(postURL, "application/json", jsonBuffer)
if err != nil {
return fmt.Errorf("failed to send notification to Gotify: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/gotify/gotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var _ = Describe("the Gotify plugin URL building and token validation functions"
It("should not report an error if the server accepts the payload", func() {
serviceURL, _ := url.Parse("gotify://my.gotify.tld/Aaa.bbb.ccc.ddd")
err = service.Initialize(serviceURL, logger)
httpmock.ActivateNonDefault(service.client)
httpmock.ActivateNonDefault(service.Client)
Expect(err).NotTo(HaveOccurred())

targetURL := "https://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
Expand All @@ -131,7 +131,7 @@ var _ = Describe("the Gotify plugin URL building and token validation functions"
It("should not panic if an error occurs when sending the payload", func() {
serviceURL, _ := url.Parse("gotify://my.gotify.tld/Aaa.bbb.ccc.ddd")
err = service.Initialize(serviceURL, logger)
httpmock.ActivateNonDefault(service.client)
httpmock.ActivateNonDefault(service.Client)
Expect(err).NotTo(HaveOccurred())

targetURL := "https://my.gotify.tld/message?token=Aaa.bbb.ccc.ddd"
Expand Down
100 changes: 100 additions & 0 deletions pkg/services/services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package services_test

import (
"github.com/containrrr/shoutrrr/pkg/router"
"github.com/containrrr/shoutrrr/pkg/services/gotify"
"github.com/containrrr/shoutrrr/pkg/types"
"github.com/jarcoal/httpmock"
"log"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestServices(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Service Compliance Suite")
}

var serviceURLs = map[string]string{
"discord": "discord://token@id",
"gotify": "gotify://example.com/Aaa.bbb.ccc.ddd",
"hangouts": "hangouts://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz",
"ifttt": "ifttt://key?events=event",
"join": "join://:apikey@join/?devices=device",
"logger": "logger://",
"mattermost": "mattermost://[email protected]/token",
"opsgenie": "opsgenie://example.com/token?responders=user:dummy",
"pushbullet": "pushbullet://tokentokentokentokentokentokentoke",
"pushover": "pushover://:token@user/?devices=device",
"rocketchat": "rocketchat://example.com/token/channel",
"slack": "slack://AAAAAAAAA/BBBBBBBBB/123456789123456789123456",
"smtp": "smtp://host.tld:25/[email protected]&[email protected]",
"teams": "teams://11111111-4444-4444-8444-cccccccccccc@22222222-4444-4444-8444-cccccccccccc/33333333012222222222333333333344/44444444-4444-4444-8444-cccccccccccc",
"telegram": "telegram://000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@telegram?channels=channel",
"xmpp": "xmpp://",
"zulip": "zulip://mail:[email protected]/?stream=foo&topic=bar",
}

var logger = log.New(GinkgoWriter, "Test", log.LstdFlags)

var _ = Describe("services", func() {

BeforeEach(func() {

})
AfterEach(func() {

})

When("passed the a title param", func() {

var serviceRouter *router.ServiceRouter

AfterEach(func() {
httpmock.DeactivateAndReset()
})

for key, configURL := range serviceURLs {

key := key //necessary to ensure the correct value is passed to the closure
configURL := configURL
serviceRouter, _ = router.New(logger)

It("should not throw an error for "+key, func() {

if key == "smtp" {
Skip("smtp does not use HTTP and needs a specific test")
}
if key == "xmpp" {
Skip("not supported")
}

httpmock.Activate()
if key == "discord" {
// Always return a "No content" result, as the http request isn't what is under test
httpmock.RegisterNoResponder(httpmock.NewStringResponder(204, ""))
} else {
// Always return an "OK" result, as the http request isn't what is under test
httpmock.RegisterNoResponder(httpmock.NewStringResponder(200, ""))
}

service, err := serviceRouter.Locate(configURL)
Expect(err).NotTo(HaveOccurred())

if key == "gotify" {
gotifyService := service.(*gotify.Service)
httpmock.ActivateNonDefault(gotifyService.Client)
}

err = service.Send("test", (*types.Params)(&map[string]string{
"title": "test title",
}))
Expect(err).NotTo(HaveOccurred())
})

}
})

})

0 comments on commit b8c4d0c

Please sign in to comment.