| 1 |
package services_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"log" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/jarcoal/httpmock" |
| 12 |
"github.com/onsi/ginkgo/v2" |
| 13 |
"github.com/onsi/gomega" |
| 14 |
|
| 15 |
"github.com/nicholas-fedor/shoutrrr/pkg/router" |
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 |
) |
| 18 |
|
| 19 |
var serviceURLs = map[string]string{ |
| 20 |
"discord": "discord://token@id", |
| 21 |
"gotify": "gotify://example.com/Aaa.bbb.ccc.ddd", |
| 22 |
"homeassistant": "homeassistant://[email protected]", |
| 23 |
"googlechat": "googlechat://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz", |
| 24 |
"hangouts": "hangouts://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz", |
| 25 |
"ifttt": "ifttt://key?events=event", |
| 26 |
"join": "join://:apikey@join/?devices=device", |
| 27 |
"logger": "logger://", |
| 28 |
"mattermost": "mattermost://[email protected]/token", |
| 29 |
"opsgenie": "opsgenie://example.com/token?responders=user:dummy", |
| 30 |
"pushbullet": "pushbullet://tokentokentokentokentokentokentoke", |
| 31 |
"pushover": "pushover://:token@user/?devices=device", |
| 32 |
"rocketchat": "rocketchat://example.com/token/channel", |
| 33 |
"signal": "signal://localhost:8080/+15551234567/+15559876543", |
| 34 |
"slack": "slack://AAAAAAAAA/BBBBBBBBB/123456789123456789123456", |
| 35 |
"smtp": "smtp://host.tld:25/[email protected]&[email protected]", |
| 36 |
"teams": "teams://?host=https%3A%2F%2Fprod-00.westus.logic.azure.com%3A443%2Fworkflows%2F00000000-0000-0000-0000-000000000000%2Ftriggers%2Fmanual%2Fpaths%2Finvoke%3Fapi-version%3D2016-06-00%26sp%3D%2Ftriggers%2Fmanual%2Frun%26sv%3D1.0%26sig%3DXXXXXXXX", |
| 37 |
"telegram": "telegram://000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@telegram?channels=channel", |
| 38 |
"twilio": "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+15551234567/+15559876543", |
| 39 |
"xmpp": "xmpp://alice:[email protected]/[email protected]", |
| 40 |
"zulip": "zulip://mail:[email protected]/?stream=foo&topic=bar", |
| 41 |
} |
| 42 |
|
| 43 |
var serviceResponses = map[string]string{ |
| 44 |
"discord": "", |
| 45 |
"gotify": `{"id": 0}`, |
| 46 |
"homeassistant": `[]`, |
| 47 |
"googlechat": "", |
| 48 |
"hangouts": "", |
| 49 |
"ifttt": "", |
| 50 |
"join": "", |
| 51 |
"logger": "", |
| 52 |
"mattermost": "", |
| 53 |
"opsgenie": "", |
| 54 |
"pushbullet": `{"type": "note", "body": "test", "title": "test title", "active": true, "created": 0}`, |
| 55 |
"pushover": "", |
| 56 |
"rocketchat": "", |
| 57 |
"signal": `{"timestamp":1}`, |
| 58 |
"slack": "", |
| 59 |
"smtp": "", |
| 60 |
"teams": "", |
| 61 |
"telegram": "", |
| 62 |
"twilio": `{"sid": "SM123"}`, |
| 63 |
"xmpp": "", |
| 64 |
"zulip": "", |
| 65 |
} |
| 66 |
|
| 67 |
var logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags) |
| 68 |
|
| 69 |
var _ = ginkgo.Describe("services", func() { |
| 70 |
ginkgo.BeforeEach(func() { |
| 71 |
}) |
| 72 |
ginkgo.AfterEach(func() { |
| 73 |
}) |
| 74 |
|
| 75 |
ginkgo.When("passed the a title param", func() { |
| 76 |
var serviceRouter *router.ServiceRouter |
| 77 |
|
| 78 |
ginkgo.AfterEach(func() { |
| 79 |
httpmock.DeactivateAndReset() |
| 80 |
}) |
| 81 |
|
| 82 |
for key, serviceURL := range serviceURLs { |
| 83 |
serviceRouter, _ = router.NewWithOptions(logger, types.SenderOptions{}) |
| 84 |
|
| 85 |
ginkgo.It("should not throw an error for "+key, func() { |
| 86 |
if key == "smtp" { |
| 87 |
ginkgo.Skip("smtp does not use HTTP and needs a specific test") |
| 88 |
} |
| 89 |
|
| 90 |
if key == "xmpp" { |
| 91 |
ginkgo.Skip("xmpp does not use HTTP and needs a specific test") |
| 92 |
} |
| 93 |
|
| 94 |
service, err := serviceRouter.Locate(serviceURL) |
| 95 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 96 |
|
| 97 |
httpmock.Activate() |
| 98 |
|
| 99 |
customClient := &http.Client{} |
| 100 |
if setter, ok := service.(types.HTTPClientSetter); ok { |
| 101 |
httpmock.ActivateNonDefault(customClient) |
| 102 |
setter.SetHTTPClient(customClient) |
| 103 |
} |
| 104 |
|
| 105 |
respStatus := http.StatusOK |
| 106 |
if key == "discord" || key == "ifttt" { |
| 107 |
respStatus = http.StatusNoContent |
| 108 |
} |
| 109 |
|
| 110 |
if key == "twilio" { |
| 111 |
respStatus = http.StatusCreated |
| 112 |
} |
| 113 |
|
| 114 |
if key == "mattermost" { |
| 115 |
httpmock.RegisterResponder( |
| 116 |
"POST", |
| 117 |
"https://example.com/hooks/token", |
| 118 |
httpmock.NewStringResponder(http.StatusOK, ""), |
| 119 |
) |
| 120 |
} else { |
| 121 |
httpmock.RegisterNoResponder( |
| 122 |
httpmock.NewStringResponder(respStatus, serviceResponses[key]), |
| 123 |
) |
| 124 |
} |
| 125 |
|
| 126 |
err = service.Send("test", (*types.Params)(&map[string]string{ |
| 127 |
"title": "test title", |
| 128 |
})) |
| 129 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 130 |
}) |
| 131 |
|
| 132 |
if key == "mattermost" { |
| 133 |
ginkgo.It("should not throw an error for "+key+" with DisableTLS", func() { |
| 134 |
modifiedURL := serviceURL + "?disabletls=yes" |
| 135 |
service, err := serviceRouter.Locate(modifiedURL) |
| 136 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 137 |
|
| 138 |
httpmock.Activate() |
| 139 |
|
| 140 |
customClient := &http.Client{} |
| 141 |
if setter, ok := service.(types.HTTPClientSetter); ok { |
| 142 |
httpmock.ActivateNonDefault(customClient) |
| 143 |
setter.SetHTTPClient(customClient) |
| 144 |
} |
| 145 |
|
| 146 |
httpmock.RegisterResponder( |
| 147 |
"POST", |
| 148 |
"http://example.com/hooks/token", |
| 149 |
httpmock.NewStringResponder(http.StatusOK, ""), |
| 150 |
) |
| 151 |
|
| 152 |
err = service.Send("test", (*types.Params)(&map[string]string{ |
| 153 |
"title": "test title", |
| 154 |
})) |
| 155 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 156 |
}) |
| 157 |
} |
| 158 |
} |
| 159 |
}) |
| 160 |
|
| 161 |
ginkgo.Describe("custom HTTP client injection", func() { |
| 162 |
ginkgo.It("should use the injected client when sending a notification", func() { |
| 163 |
customClient := &http.Client{} |
| 164 |
httpmock.ActivateNonDefault(customClient) |
| 165 |
ginkgo.DeferCleanup(httpmock.DeactivateAndReset) |
| 166 |
|
| 167 |
httpmock.RegisterResponder( |
| 168 |
"POST", |
| 169 |
"https://example.com/webhook", |
| 170 |
httpmock.NewStringResponder(http.StatusOK, ""), |
| 171 |
) |
| 172 |
|
| 173 |
sr, err := router.NewWithOptions( |
| 174 |
logger, |
| 175 |
types.SenderOptions{HTTPClient: customClient}, |
| 176 |
"generic+https://example.com/webhook", |
| 177 |
) |
| 178 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 179 |
|
| 180 |
service, err := sr.Locate("generic+https://example.com/webhook") |
| 181 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 182 |
|
| 183 |
err = service.Send("test", nil) |
| 184 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 185 |
}) |
| 186 |
}) |
| 187 |
|
| 188 |
ginkgo.Describe("custom DialContext injection", func() { |
| 189 |
ginkgo.It("should use the injected dialer for SMTP", func() { |
| 190 |
blocked := errors.New("destination blocked") |
| 191 |
|
| 192 |
var gotNetwork, gotAddr string |
| 193 |
|
| 194 |
dial := func(_ context.Context, network, addr string) (net.Conn, error) { |
| 195 |
gotNetwork = network |
| 196 |
gotAddr = addr |
| 197 |
|
| 198 |
return nil, blocked |
| 199 |
} |
| 200 |
|
| 201 |
smtpURL := "smtp://mail.example.com:587/[email protected]&[email protected]" |
| 202 |
sr, err := router.NewWithOptions( |
| 203 |
logger, |
| 204 |
types.SenderOptions{DialContext: dial}, |
| 205 |
smtpURL, |
| 206 |
) |
| 207 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 208 |
|
| 209 |
errs := sr.Send("test", nil) |
| 210 |
gomega.Expect(errs).To(gomega.HaveLen(1)) |
| 211 |
gomega.Expect(errs[0]).To(gomega.MatchError(blocked)) |
| 212 |
gomega.Expect(gotNetwork).To(gomega.Equal("tcp")) |
| 213 |
gomega.Expect(gotAddr).To(gomega.Equal("mail.example.com:587")) |
| 214 |
}) |
| 215 |
|
| 216 |
ginkgo.It("should leave HTTP services unaffected", func() { |
| 217 |
customClient := &http.Client{} |
| 218 |
httpmock.ActivateNonDefault(customClient) |
| 219 |
ginkgo.DeferCleanup(httpmock.DeactivateAndReset) |
| 220 |
|
| 221 |
httpmock.RegisterResponder( |
| 222 |
"POST", |
| 223 |
"https://example.com/webhook", |
| 224 |
httpmock.NewStringResponder(http.StatusOK, ""), |
| 225 |
) |
| 226 |
|
| 227 |
sr, err := router.NewWithOptions( |
| 228 |
logger, |
| 229 |
types.SenderOptions{ |
| 230 |
HTTPClient: customClient, |
| 231 |
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { |
| 232 |
return nil, errors.New("should not dial") |
| 233 |
}, |
| 234 |
}, |
| 235 |
"generic+https://example.com/webhook", |
| 236 |
) |
| 237 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 238 |
|
| 239 |
service, err := sr.Locate("generic+https://example.com/webhook") |
| 240 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 241 |
|
| 242 |
err = service.Send("test", nil) |
| 243 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 244 |
}) |
| 245 |
}) |
| 246 |
}) |
| 247 |
|
| 248 |
func TestServices(t *testing.T) { |
| 249 |
t.Parallel() |
| 250 |
gomega.RegisterFailHandler(ginkgo.Fail) |
| 251 |
ginkgo.RunSpecs(t, "Service Compliance Suite") |
| 252 |
} |