Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3b2c48b5e4
commit
c0c4addb85
285 changed files with 25880 additions and 0 deletions
116
pkg/services/mattermost/mattermost.go
Normal file
116
pkg/services/mattermost/mattermost.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package mattermost
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// defaultHTTPTimeout is the default timeout for HTTP requests.
|
||||
const defaultHTTPTimeout = 10 * time.Second
|
||||
|
||||
// ErrSendFailed indicates that the notification failed due to an unexpected response status code.
|
||||
var ErrSendFailed = errors.New(
|
||||
"failed to send notification to service, response status code unexpected",
|
||||
)
|
||||
|
||||
// Service sends notifications to a pre-configured Mattermost channel or user.
|
||||
type Service struct {
|
||||
standard.Standard
|
||||
Config *Config
|
||||
pkr format.PropKeyResolver
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// GetHTTPClient returns the service's HTTP client for testing purposes.
|
||||
func (service *Service) GetHTTPClient() *http.Client {
|
||||
return service.httpClient
|
||||
}
|
||||
|
||||
// Initialize configures the service with a URL and logger.
|
||||
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
|
||||
service.SetLogger(logger)
|
||||
service.Config = &Config{}
|
||||
service.pkr = format.NewPropKeyResolver(service.Config)
|
||||
|
||||
err := service.Config.setURL(&service.pkr, configURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var transport *http.Transport
|
||||
if service.Config.DisableTLS {
|
||||
transport = &http.Transport{
|
||||
TLSClientConfig: nil, // Plain HTTP
|
||||
}
|
||||
} else {
|
||||
transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: false, // Explicitly safe when TLS is enabled
|
||||
MinVersion: tls.VersionTLS12, // Enforce TLS 1.2 or higher
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
service.httpClient = &http.Client{Transport: transport}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetID returns the service identifier.
|
||||
func (service *Service) GetID() string {
|
||||
return Scheme
|
||||
}
|
||||
|
||||
// Send delivers a notification message to Mattermost.
|
||||
func (service *Service) Send(message string, params *types.Params) error {
|
||||
config := service.Config
|
||||
apiURL := buildURL(config)
|
||||
|
||||
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
|
||||
return fmt.Errorf("updating config from params: %w", err)
|
||||
}
|
||||
|
||||
json, _ := CreateJSONPayload(config, message, params)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(json))
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating POST request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
res, err := service.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing POST request to Mattermost API: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%w: %s", ErrSendFailed, res.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildURL constructs the API URL for Mattermost based on the Config.
|
||||
func buildURL(config *Config) string {
|
||||
scheme := "https"
|
||||
if config.DisableTLS {
|
||||
scheme = "http"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s://%s/hooks/%s", scheme, config.Host, config.Token)
|
||||
}
|
121
pkg/services/mattermost/mattermost_config.go
Normal file
121
pkg/services/mattermost/mattermost_config.go
Normal file
|
@ -0,0 +1,121 @@
|
|||
package mattermost
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// Scheme is the identifying part of this service's configuration URL.
|
||||
const Scheme = "mattermost"
|
||||
|
||||
// Static errors for configuration validation.
|
||||
var (
|
||||
ErrNotEnoughArguments = errors.New(
|
||||
"the apiURL does not include enough arguments, either provide 1 or 3 arguments (they may be empty)",
|
||||
)
|
||||
)
|
||||
|
||||
// ErrorMessage represents error events within the Mattermost service.
|
||||
type ErrorMessage string
|
||||
|
||||
// Config holds all configuration information for the Mattermost service.
|
||||
type Config struct {
|
||||
standard.EnumlessConfig
|
||||
UserName string `desc:"Override webhook user" optional:"" url:"user"`
|
||||
Icon string `desc:"Use emoji or URL as icon (based on presence of http(s):// prefix)" optional:"" default:"" key:"icon,icon_emoji,icon_url"`
|
||||
Title string `desc:"Notification title, optionally set by the sender (not used)" default:"" key:"title"`
|
||||
Channel string `desc:"Override webhook channel" optional:"" url:"path2"`
|
||||
Host string `desc:"Mattermost server host" url:"host,port"`
|
||||
Token string `desc:"Webhook token" url:"path1"`
|
||||
DisableTLS bool ` default:"No" key:"disabletls"`
|
||||
}
|
||||
|
||||
// CreateConfigFromURL creates a new Config instance from a URL representation.
|
||||
func CreateConfigFromURL(url *url.URL) (*Config, error) {
|
||||
config := &Config{}
|
||||
if err := config.SetURL(url); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// GetURL returns a URL representation of the Config's current field values.
|
||||
func (c *Config) GetURL() *url.URL {
|
||||
resolver := format.NewPropKeyResolver(c)
|
||||
|
||||
return c.getURL(&resolver) // Pass pointer to resolver
|
||||
}
|
||||
|
||||
// SetURL updates the Config from a URL representation of its field values.
|
||||
func (c *Config) SetURL(url *url.URL) error {
|
||||
resolver := format.NewPropKeyResolver(c)
|
||||
|
||||
return c.setURL(&resolver, url) // Pass pointer to resolver
|
||||
}
|
||||
|
||||
// getURL constructs a URL from the Config's fields using the provided resolver.
|
||||
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
|
||||
paths := []string{"", c.Token, c.Channel}
|
||||
if c.Channel == "" {
|
||||
paths = paths[:2]
|
||||
}
|
||||
|
||||
var user *url.Userinfo
|
||||
if c.UserName != "" {
|
||||
user = url.User(c.UserName)
|
||||
}
|
||||
|
||||
return &url.URL{
|
||||
User: user,
|
||||
Host: c.Host,
|
||||
Path: strings.Join(paths, "/"),
|
||||
Scheme: Scheme,
|
||||
ForceQuery: false,
|
||||
RawQuery: format.BuildQuery(resolver),
|
||||
}
|
||||
}
|
||||
|
||||
// setURL updates the Config from a URL using the provided resolver.
|
||||
func (c *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
|
||||
c.Host = url.Host
|
||||
c.UserName = url.User.Username()
|
||||
|
||||
if err := c.parsePath(url); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for key, vals := range url.Query() {
|
||||
if err := resolver.Set(key, vals[0]); err != nil {
|
||||
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// parsePath extracts Token and Channel from the URL path and validates arguments.
|
||||
func (c *Config) parsePath(url *url.URL) error {
|
||||
path := strings.Split(strings.Trim(url.Path, "/"), "/")
|
||||
isDummy := url.String() == "mattermost://dummy@dummy.com"
|
||||
|
||||
if !isDummy && (len(path) < 1 || path[0] == "") {
|
||||
return ErrNotEnoughArguments
|
||||
}
|
||||
|
||||
if len(path) > 0 && path[0] != "" {
|
||||
c.Token = path[0]
|
||||
}
|
||||
|
||||
if len(path) > 1 && path[1] != "" {
|
||||
c.Channel = path[1]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
63
pkg/services/mattermost/mattermost_json.go
Normal file
63
pkg/services/mattermost/mattermost_json.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package mattermost
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt" // Add this import
|
||||
"regexp"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
// iconURLPattern matches URLs starting with http or https for icon detection.
|
||||
var iconURLPattern = regexp.MustCompile(`https?://`)
|
||||
|
||||
// JSON represents the payload structure for Mattermost notifications.
|
||||
type JSON struct {
|
||||
Text string `json:"text"`
|
||||
UserName string `json:"username,omitempty"`
|
||||
Channel string `json:"channel,omitempty"`
|
||||
IconEmoji string `json:"icon_emoji,omitempty"`
|
||||
IconURL string `json:"icon_url,omitempty"`
|
||||
}
|
||||
|
||||
// SetIcon sets the appropriate icon field in the payload based on whether the input is a URL or not.
|
||||
func (j *JSON) SetIcon(icon string) {
|
||||
j.IconURL = ""
|
||||
j.IconEmoji = ""
|
||||
|
||||
if icon != "" {
|
||||
if iconURLPattern.MatchString(icon) {
|
||||
j.IconURL = icon
|
||||
} else {
|
||||
j.IconEmoji = icon
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CreateJSONPayload generates a JSON payload for the Mattermost service.
|
||||
func CreateJSONPayload(config *Config, message string, params *types.Params) ([]byte, error) {
|
||||
payload := JSON{
|
||||
Text: message,
|
||||
UserName: config.UserName,
|
||||
Channel: config.Channel,
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
if value, found := (*params)["username"]; found {
|
||||
payload.UserName = value
|
||||
}
|
||||
|
||||
if value, found := (*params)["channel"]; found {
|
||||
payload.Channel = value
|
||||
}
|
||||
}
|
||||
|
||||
payload.SetIcon(config.Icon)
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling Mattermost payload to JSON: %w", err)
|
||||
}
|
||||
|
||||
return payloadBytes, nil
|
||||
}
|
440
pkg/services/mattermost/mattermost_test.go
Normal file
440
pkg/services/mattermost/mattermost_test.go
Normal file
|
@ -0,0 +1,440 @@
|
|||
package mattermost
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
var (
|
||||
service *Service
|
||||
envMattermostURL *url.URL
|
||||
_ = ginkgo.BeforeSuite(func() {
|
||||
service = &Service{}
|
||||
envMattermostURL, _ = url.Parse(os.Getenv("SHOUTRRR_MATTERMOST_URL"))
|
||||
})
|
||||
)
|
||||
|
||||
func TestMattermost(t *testing.T) {
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Shoutrrr Mattermost Suite")
|
||||
}
|
||||
|
||||
var _ = ginkgo.Describe("the mattermost service", func() {
|
||||
ginkgo.When("running integration tests", func() {
|
||||
ginkgo.It("should work without errors", func() {
|
||||
if envMattermostURL.String() == "" {
|
||||
return
|
||||
}
|
||||
serviceURL, _ := url.Parse(envMattermostURL.String())
|
||||
gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
|
||||
To(gomega.Succeed())
|
||||
err := service.Send(
|
||||
"this is an integration test",
|
||||
nil,
|
||||
)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("the mattermost config", func() {
|
||||
ginkgo.When("generating a config object", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
|
||||
)
|
||||
config := &Config{}
|
||||
err := config.SetURL(mattermostURL)
|
||||
ginkgo.It("should not have caused an error", func() {
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should set host", func() {
|
||||
gomega.Expect(config.Host).To(gomega.Equal("mattermost.my-domain.com"))
|
||||
})
|
||||
ginkgo.It("should set token", func() {
|
||||
gomega.Expect(config.Token).To(gomega.Equal("thisshouldbeanapitoken"))
|
||||
})
|
||||
ginkgo.It("should not set channel or username", func() {
|
||||
gomega.Expect(config.Channel).To(gomega.BeEmpty())
|
||||
gomega.Expect(config.UserName).To(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
ginkgo.When("generating a new config with url, that has no token", func() {
|
||||
ginkgo.It("should return an error", func() {
|
||||
mattermostURL, _ := url.Parse("mattermost://mattermost.my-domain.com")
|
||||
config := &Config{}
|
||||
err := config.SetURL(mattermostURL)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
ginkgo.When("generating a config object with username only", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken",
|
||||
)
|
||||
config := &Config{}
|
||||
err := config.SetURL(mattermostURL)
|
||||
ginkgo.It("should not have caused an error", func() {
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should set username", func() {
|
||||
gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
|
||||
})
|
||||
ginkgo.It("should not set channel", func() {
|
||||
gomega.Expect(config.Channel).To(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
ginkgo.When("generating a config object with channel only", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
|
||||
)
|
||||
config := &Config{}
|
||||
err := config.SetURL(mattermostURL)
|
||||
ginkgo.It("should not hav caused an error", func() {
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should set channel", func() {
|
||||
gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
|
||||
})
|
||||
ginkgo.It("should not set username", func() {
|
||||
gomega.Expect(config.UserName).To(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
ginkgo.When("generating a config object with channel an userName", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
|
||||
)
|
||||
config := &Config{}
|
||||
err := config.SetURL(mattermostURL)
|
||||
ginkgo.It("should not hav caused an error", func() {
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should set channel", func() {
|
||||
gomega.Expect(config.Channel).To(gomega.Equal("testChannel"))
|
||||
})
|
||||
ginkgo.It("should set username", func() {
|
||||
gomega.Expect(config.UserName).To(gomega.Equal("testUserName"))
|
||||
})
|
||||
})
|
||||
ginkgo.When("using DisableTLS and port", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://watchtower@home.lan:8065/token/channel?disabletls=yes",
|
||||
)
|
||||
config := &Config{}
|
||||
gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
|
||||
ginkgo.It("should preserve host with port", func() {
|
||||
gomega.Expect(config.Host).To(gomega.Equal("home.lan:8065"))
|
||||
})
|
||||
ginkgo.It("should set DisableTLS", func() {
|
||||
gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
|
||||
})
|
||||
ginkgo.It("should generate http URL", func() {
|
||||
gomega.Expect(buildURL(config)).To(gomega.Equal("http://home.lan:8065/hooks/token"))
|
||||
})
|
||||
ginkgo.It("should serialize back correctly", func() {
|
||||
gomega.Expect(config.GetURL().String()).
|
||||
To(gomega.Equal("mattermost://watchtower@home.lan:8065/token/channel?disabletls=Yes"))
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("initializing with DisableTLS", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("should use plain HTTP transport when DisableTLS is true", func() {
|
||||
mattermostURL, _ := url.Parse("mattermost://user@host:8080/token?disabletls=yes")
|
||||
service := &Service{}
|
||||
err := service.Initialize(mattermostURL, testutils.TestLogger())
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
httpmock.ActivateNonDefault(service.httpClient)
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"http://host:8080/hooks/token",
|
||||
httpmock.NewStringResponder(200, ""),
|
||||
)
|
||||
|
||||
err = service.Send("Test message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(buildURL(service.Config)).
|
||||
To(gomega.Equal("http://host:8080/hooks/token"))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("sending the payload", func() {
|
||||
var err error
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("should not report an error if the server accepts the payload", func() {
|
||||
config := Config{
|
||||
Host: "mattermost.host",
|
||||
Token: "token",
|
||||
}
|
||||
serviceURL := config.GetURL()
|
||||
service := Service{}
|
||||
err = service.Initialize(serviceURL, nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.ActivateNonDefault(service.httpClient)
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://mattermost.host/hooks/token",
|
||||
httpmock.NewStringResponder(200, ""),
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should return an error if the server rejects the payload", func() {
|
||||
config := Config{
|
||||
Host: "mattermost.host",
|
||||
Token: "token",
|
||||
}
|
||||
serviceURL := config.GetURL()
|
||||
service := Service{}
|
||||
err = service.Initialize(serviceURL, nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.ActivateNonDefault(service.httpClient)
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://mattermost.host/hooks/token",
|
||||
httpmock.NewStringResponder(403, "Forbidden"),
|
||||
)
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
gomega.Expect(err.Error()).
|
||||
To(gomega.ContainSubstring("failed to send notification to service"))
|
||||
resp := httpmock.NewStringResponse(403, "Forbidden")
|
||||
resp.Status = "403 Forbidden"
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://mattermost.host/hooks/token",
|
||||
httpmock.ResponderFromResponse(resp),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.When("generating a config object", func() {
|
||||
ginkgo.It("should not set icon", func() {
|
||||
slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB")
|
||||
config, configError := CreateConfigFromURL(slackURL)
|
||||
|
||||
gomega.Expect(configError).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(config.Icon).To(gomega.BeEmpty())
|
||||
})
|
||||
ginkgo.It("should set icon", func() {
|
||||
slackURL, _ := url.Parse("mattermost://AAAAAAAAA/BBBBBBBBB?icon=test")
|
||||
config, configError := CreateConfigFromURL(slackURL)
|
||||
|
||||
gomega.Expect(configError).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(config.Icon).To(gomega.BeIdenticalTo("test"))
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("creating the payload", func() {
|
||||
ginkgo.Describe("the icon fields", func() {
|
||||
payload := JSON{}
|
||||
ginkgo.It("should set IconURL when the configured icon looks like an URL", func() {
|
||||
payload.SetIcon("https://example.com/logo.png")
|
||||
gomega.Expect(payload.IconURL).To(gomega.Equal("https://example.com/logo.png"))
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
|
||||
})
|
||||
ginkgo.It(
|
||||
"should set IconEmoji when the configured icon does not look like an URL",
|
||||
func() {
|
||||
payload.SetIcon("tanabata_tree")
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.Equal("tanabata_tree"))
|
||||
gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
|
||||
},
|
||||
)
|
||||
ginkgo.It("should clear both fields when icon is empty", func() {
|
||||
payload.SetIcon("")
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
|
||||
gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("Sending messages", func() {
|
||||
ginkgo.When("sending a message completely without parameters", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://mattermost.my-domain.com/thisshouldbeanapitoken",
|
||||
)
|
||||
config := &Config{}
|
||||
gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
|
||||
ginkgo.It("should generate the correct url to call", func() {
|
||||
generatedURL := buildURL(config)
|
||||
gomega.Expect(generatedURL).
|
||||
To(gomega.Equal("https://mattermost.my-domain.com/hooks/thisshouldbeanapitoken"))
|
||||
})
|
||||
ginkgo.It("should generate the correct JSON body", func() {
|
||||
json, err := CreateJSONPayload(config, "this is a message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(string(json)).To(gomega.Equal("{\"text\":\"this is a message\"}"))
|
||||
})
|
||||
})
|
||||
ginkgo.When("sending a message with pre set username and channel", func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
|
||||
)
|
||||
config := &Config{}
|
||||
gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
|
||||
ginkgo.It("should generate the correct JSON body", func() {
|
||||
json, err := CreateJSONPayload(config, "this is a message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(string(json)).
|
||||
To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"testUserName\",\"channel\":\"testChannel\"}"))
|
||||
})
|
||||
})
|
||||
ginkgo.When(
|
||||
"sending a message with pre set username and channel but overwriting them with parameters",
|
||||
func() {
|
||||
mattermostURL, _ := url.Parse(
|
||||
"mattermost://testUserName@mattermost.my-domain.com/thisshouldbeanapitoken/testChannel",
|
||||
)
|
||||
config := &Config{}
|
||||
gomega.Expect(config.SetURL(mattermostURL)).To(gomega.Succeed())
|
||||
ginkgo.It("should generate the correct JSON body", func() {
|
||||
params := (*types.Params)(
|
||||
&map[string]string{
|
||||
"username": "overwriteUserName",
|
||||
"channel": "overwriteChannel",
|
||||
},
|
||||
)
|
||||
json, err := CreateJSONPayload(config, "this is a message", params)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(string(json)).
|
||||
To(gomega.Equal("{\"text\":\"this is a message\",\"username\":\"overwriteUserName\",\"channel\":\"overwriteChannel\"}"))
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.It("should be identical after de-/serialization", func() {
|
||||
input := "mattermost://bot@mattermost.host/token/channel"
|
||||
|
||||
config := &Config{}
|
||||
gomega.Expect(config.SetURL(testutils.URLMust(input))).To(gomega.Succeed())
|
||||
gomega.Expect(config.GetURL().String()).To(gomega.Equal(input))
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("creating configurations", func() {
|
||||
ginkgo.When("given a url with channel field", func() {
|
||||
ginkgo.It("should not throw an error", func() {
|
||||
serviceURL := testutils.URLMust(`mattermost://user@mockserver/atoken/achannel`)
|
||||
gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
|
||||
})
|
||||
})
|
||||
ginkgo.When("given a url with title prop", func() {
|
||||
ginkgo.It("should not throw an error", func() {
|
||||
serviceURL := testutils.URLMust(
|
||||
`mattermost://user@mockserver/atoken?icon=https%3A%2F%2Fexample%2Fsomething.png`,
|
||||
)
|
||||
gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
|
||||
})
|
||||
})
|
||||
ginkgo.When("given a url with all fields and props", func() {
|
||||
ginkgo.It("should not throw an error", func() {
|
||||
serviceURL := testutils.URLMust(
|
||||
`mattermost://user@mockserver/atoken/achannel?icon=https%3A%2F%2Fexample%2Fsomething.png`,
|
||||
)
|
||||
gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.Succeed())
|
||||
})
|
||||
})
|
||||
ginkgo.When("given a url with invalid props", func() {
|
||||
ginkgo.It("should return an error", func() {
|
||||
serviceURL := testutils.URLMust(`matrix://user@mockserver/atoken?foo=bar`)
|
||||
gomega.Expect((&Config{}).SetURL(serviceURL)).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.It("should be identical after de-/serialization", func() {
|
||||
testURL := "mattermost://user@mockserver/atoken/achannel?icon=something"
|
||||
|
||||
url, err := url.Parse(testURL)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "parsing")
|
||||
|
||||
config := &Config{}
|
||||
err = config.SetURL(url)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying")
|
||||
|
||||
outputURL := config.GetURL()
|
||||
fmt.Fprint(ginkgo.GinkgoWriter, outputURL.String(), " ", testURL, "\n")
|
||||
|
||||
gomega.Expect(outputURL.String()).To(gomega.Equal(testURL))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("sending the payload", func() {
|
||||
var err error
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("should not report an error if the server accepts the payload", func() {
|
||||
config := Config{
|
||||
Host: "mattermost.host",
|
||||
Token: "token",
|
||||
}
|
||||
serviceURL := config.GetURL()
|
||||
service := Service{}
|
||||
err = service.Initialize(serviceURL, nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
httpmock.ActivateNonDefault(service.httpClient)
|
||||
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
"https://mattermost.host/hooks/token",
|
||||
httpmock.NewStringResponder(200, ``),
|
||||
)
|
||||
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("the basic service API", func() {
|
||||
ginkgo.Describe("the service config", func() {
|
||||
ginkgo.It("should implement basic service config API methods correctly", func() {
|
||||
testutils.TestConfigGetInvalidQueryValue(&Config{})
|
||||
|
||||
testutils.TestConfigSetDefaultValues(&Config{})
|
||||
|
||||
testutils.TestConfigGetEnumsCount(&Config{}, 0)
|
||||
testutils.TestConfigGetFieldsCount(&Config{}, 5)
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("the service instance", func() {
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
ginkgo.It("should implement basic service API methods correctly", func() {
|
||||
serviceURL := testutils.URLMust("mattermost://mockhost/mocktoken")
|
||||
gomega.Expect(service.Initialize(serviceURL, testutils.TestLogger())).
|
||||
To(gomega.Succeed())
|
||||
testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.It("should return the correct service ID", func() {
|
||||
service := &Service{}
|
||||
gomega.Expect(service.GetID()).To(gomega.Equal("mattermost"))
|
||||
})
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue