| 1 |
package signal |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json/v2" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"net" |
| 9 |
"net/http" |
| 10 |
"net/url" |
| 11 |
|
| 12 |
"github.com/jarcoal/httpmock" |
| 13 |
"github.com/onsi/ginkgo/v2" |
| 14 |
"github.com/onsi/gomega" |
| 15 |
|
| 16 |
"github.com/nicholas-fedor/shoutrrr/internal/meta" |
| 17 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 18 |
) |
| 19 |
|
| 20 |
var _ = ginkgo.Describe("service", func() { |
| 21 |
var signal *Service |
| 22 |
|
| 23 |
ginkgo.BeforeEach(func() { |
| 24 |
signal = &Service{} |
| 25 |
}) |
| 26 |
|
| 27 |
ginkgo.It("should return the correct service ID", func() { |
| 28 |
gomega.Expect(signal.GetID()).To(gomega.Equal("signal")) |
| 29 |
}) |
| 30 |
|
| 31 |
ginkgo.Describe("custom HTTP client", func() { |
| 32 |
ginkgo.It("should keep an injected client through Initialize", func() { |
| 33 |
stub := &stubHTTPClient{} |
| 34 |
signal.SetHTTPClient(stub) |
| 35 |
|
| 36 |
serviceURL, err := url.Parse("signal://localhost:8080/+1234567890/+0987654321") |
| 37 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 38 |
gomega.Expect(signal.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred()) |
| 39 |
gomega.Expect(signal.httpClient).To(gomega.BeIdenticalTo(stub)) |
| 40 |
}) |
| 41 |
|
| 42 |
ginkgo.It("should use a client injected after Initialize", func() { |
| 43 |
serviceURL, err := url.Parse("signal://localhost:8080/+1234567890/+0987654321") |
| 44 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 45 |
gomega.Expect(signal.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred()) |
| 46 |
|
| 47 |
stub := &stubHTTPClient{} |
| 48 |
signal.SetHTTPClient(stub) |
| 49 |
|
| 50 |
gomega.Expect(signal.Send("hi", nil)).NotTo(gomega.HaveOccurred()) |
| 51 |
gomega.Expect(stub.req).NotTo(gomega.BeNil()) |
| 52 |
gomega.Expect(stub.req.URL.Path).To(gomega.Equal("/v2/send")) |
| 53 |
}) |
| 54 |
|
| 55 |
ginkgo.It("should keep an injected client when skiptlsverify is set on send", func() { |
| 56 |
serviceURL, err := url.Parse("signal://localhost:8080/+1234567890/+0987654321") |
| 57 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 58 |
gomega.Expect(signal.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred()) |
| 59 |
|
| 60 |
stub := &stubHTTPClient{} |
| 61 |
signal.SetHTTPClient(stub) |
| 62 |
|
| 63 |
params := types.Params{"skiptlsverify": "yes"} |
| 64 |
gomega.Expect(signal.Send("hi", ¶ms)).NotTo(gomega.HaveOccurred()) |
| 65 |
gomega.Expect(signal.httpClient).To(gomega.BeIdenticalTo(stub)) |
| 66 |
gomega.Expect(stub.req).NotTo(gomega.BeNil()) |
| 67 |
}) |
| 68 |
|
| 69 |
ginkgo.It("should use DialContext from an injected HTTP client", func() { |
| 70 |
serviceURL, err := url.Parse("signal://localhost:8080/+1234567890/+0987654321") |
| 71 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 72 |
gomega.Expect(signal.Initialize(serviceURL, logger)).NotTo(gomega.HaveOccurred()) |
| 73 |
|
| 74 |
blocked := errors.New("destination blocked") |
| 75 |
|
| 76 |
signal.SetHTTPClient(&http.Client{ |
| 77 |
Transport: &http.Transport{ |
| 78 |
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { |
| 79 |
return nil, blocked |
| 80 |
}, |
| 81 |
}, |
| 82 |
}) |
| 83 |
|
| 84 |
err = signal.Send("hi", nil) |
| 85 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 86 |
gomega.Expect(err).To(gomega.MatchError(blocked)) |
| 87 |
}) |
| 88 |
}) |
| 89 |
|
| 90 |
ginkgo.Describe("sending the payload", func() { |
| 91 |
var captured capturedRequest |
| 92 |
|
| 93 |
ginkgo.BeforeEach(func() { |
| 94 |
httpmock.Activate() |
| 95 |
|
| 96 |
captured = capturedRequest{} |
| 97 |
}) |
| 98 |
ginkgo.AfterEach(func() { |
| 99 |
httpmock.DeactivateAndReset() |
| 100 |
}) |
| 101 |
|
| 102 |
ginkgo.It("should not report an error if the server accepts the payload", func() { |
| 103 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 104 |
setupCapture(http.StatusOK, `{"timestamp": 1234567890}`, &captured) |
| 105 |
|
| 106 |
err := signal.Send("Test message", nil) |
| 107 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 108 |
gomega.Expect(captured.userAgent).To(gomega.Equal(meta.UserAgent())) |
| 109 |
gomega.Expect(captured.payload.Message).To(gomega.Equal("Test message")) |
| 110 |
gomega.Expect(captured.payload.TextMode).To(gomega.BeEmpty()) |
| 111 |
gomega.Expect(captured.payload.NotifySelf).To(gomega.BeNil()) |
| 112 |
}) |
| 113 |
|
| 114 |
ginkgo.It("should send text_mode styled from the URL", func() { |
| 115 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321?textmode=styled") |
| 116 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 117 |
|
| 118 |
err := signal.Send("*hello*", nil) |
| 119 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 120 |
gomega.Expect(captured.payload.TextMode).To(gomega.Equal("styled")) |
| 121 |
gomega.Expect(captured.payload.Message).To(gomega.Equal("*hello*")) |
| 122 |
}) |
| 123 |
|
| 124 |
ginkgo.It("should send text_mode normal from the URL", func() { |
| 125 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321?textmode=normal") |
| 126 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 127 |
|
| 128 |
err := signal.Send("plain", nil) |
| 129 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 130 |
gomega.Expect(captured.payload.TextMode).To(gomega.Equal("normal")) |
| 131 |
}) |
| 132 |
|
| 133 |
ginkgo.It("should apply textmode from send params", func() { |
| 134 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 135 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 136 |
|
| 137 |
params := types.Params{"textmode": "styled"} |
| 138 |
err := signal.Send("hi", ¶ms) |
| 139 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 140 |
gomega.Expect(captured.payload.TextMode).To(gomega.Equal("styled")) |
| 141 |
}) |
| 142 |
|
| 143 |
ginkgo.It("should prepend a plain title", func() { |
| 144 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321?title=Alert") |
| 145 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 146 |
|
| 147 |
err := signal.Send("body", nil) |
| 148 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 149 |
gomega.Expect(captured.payload.Message).To(gomega.Equal("Alert\nbody")) |
| 150 |
}) |
| 151 |
|
| 152 |
ginkgo.It("should bold the title when styled", func() { |
| 153 |
initMocked( |
| 154 |
signal, |
| 155 |
"signal://localhost:8080/+1234567890/+0987654321?title=Alert&textmode=styled", |
| 156 |
) |
| 157 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 158 |
|
| 159 |
err := signal.Send("body", nil) |
| 160 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 161 |
gomega.Expect(captured.payload.Message).To(gomega.Equal("**Alert**\nbody")) |
| 162 |
}) |
| 163 |
|
| 164 |
ginkgo.It("should send a title-only message when the body is empty", func() { |
| 165 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321?title=Alert") |
| 166 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 167 |
|
| 168 |
err := signal.Send("", nil) |
| 169 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 170 |
gomega.Expect(captured.payload.Message).To(gomega.Equal("Alert")) |
| 171 |
}) |
| 172 |
|
| 173 |
ginkgo.It("should send notify_self false when notifyself=no", func() { |
| 174 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321?notifyself=no") |
| 175 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 176 |
|
| 177 |
err := signal.Send("hi", nil) |
| 178 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 179 |
gomega.Expect(captured.payload.NotifySelf).NotTo(gomega.BeNil()) |
| 180 |
gomega.Expect(*captured.payload.NotifySelf).To(gomega.BeFalse()) |
| 181 |
}) |
| 182 |
|
| 183 |
ginkgo.It("should report an error if the server returns an error", func() { |
| 184 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 185 |
setupCapture(http.StatusBadRequest, `{"error": "Bad Request"}`, &captured) |
| 186 |
|
| 187 |
err := signal.Send("Test message", nil) |
| 188 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 189 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("server returned status 400")) |
| 190 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Bad Request")) |
| 191 |
}) |
| 192 |
|
| 193 |
ginkgo.It("should include challenge tokens and account on 429", func() { |
| 194 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 195 |
setupCapture( |
| 196 |
http.StatusTooManyRequests, |
| 197 |
`{"error":"rate limited","challenge_tokens":["abc","def"],"account":"+1234567890"}`, |
| 198 |
&captured, |
| 199 |
) |
| 200 |
|
| 201 |
err := signal.Send("Test message", nil) |
| 202 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 203 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("account +1234567890")) |
| 204 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("challenge tokens: abc,def")) |
| 205 |
}) |
| 206 |
|
| 207 |
ginkgo.It("should handle attachments in parameters", func() { |
| 208 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 209 |
setupCapture(http.StatusOK, `{"timestamp": 1234567890}`, &captured) |
| 210 |
|
| 211 |
params := types.Params{ |
| 212 |
"attachments": "base64data1,base64data2", |
| 213 |
} |
| 214 |
|
| 215 |
err := signal.Send("Test message", ¶ms) |
| 216 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 217 |
gomega.Expect(captured.payload.Base64Attachments). |
| 218 |
To(gomega.Equal([]string{"base64data1", "base64data2"})) |
| 219 |
}) |
| 220 |
|
| 221 |
ginkgo.It("should send a data URI as a single attachment", func() { |
| 222 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 223 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 224 |
|
| 225 |
params := types.Params{ |
| 226 |
"attachments": "data:image/png;base64,iVBOR", |
| 227 |
} |
| 228 |
|
| 229 |
err := signal.Send("Test message", ¶ms) |
| 230 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 231 |
gomega.Expect(captured.payload.Base64Attachments). |
| 232 |
To(gomega.Equal([]string{"data:image/png;base64,iVBOR"})) |
| 233 |
}) |
| 234 |
|
| 235 |
ginkgo.It("should strip the u: prefix in the send payload", func() { |
| 236 |
initMocked(signal, "signal://localhost:8080/+1234567890/u:someuser.123") |
| 237 |
setupCapture(http.StatusOK, `{"timestamp": 1}`, &captured) |
| 238 |
|
| 239 |
err := signal.Send("hi", nil) |
| 240 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 241 |
gomega.Expect(captured.payload.Recipients).To(gomega.Equal([]string{"someuser.123"})) |
| 242 |
}) |
| 243 |
|
| 244 |
ginkgo.It("should send mixed recipient types as separate requests", func() { |
| 245 |
initMocked( |
| 246 |
signal, |
| 247 |
"signal://localhost:8080/+1234567890/+0987654321/u:someuser.123/group.testgroup", |
| 248 |
) |
| 249 |
|
| 250 |
var payloads []sendMessagePayload |
| 251 |
|
| 252 |
httpmock.RegisterResponder( |
| 253 |
"POST", |
| 254 |
"https://localhost:8080/v2/send", |
| 255 |
func(req *http.Request) (*http.Response, error) { |
| 256 |
var payload sendMessagePayload |
| 257 |
|
| 258 |
reqBody, err := io.ReadAll(req.Body) |
| 259 |
if err == nil { |
| 260 |
_ = json.Unmarshal(reqBody, &payload) |
| 261 |
payloads = append(payloads, payload) |
| 262 |
} |
| 263 |
|
| 264 |
return httpmock.NewStringResponse(http.StatusOK, `{"timestamp":1}`), nil |
| 265 |
}, |
| 266 |
) |
| 267 |
|
| 268 |
err := signal.Send("hi", nil) |
| 269 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 270 |
gomega.Expect(payloads).To(gomega.HaveLen(3)) |
| 271 |
gomega.Expect(payloads[0].Recipients).To(gomega.Equal([]string{"+0987654321"})) |
| 272 |
gomega.Expect(payloads[1].Recipients).To(gomega.Equal([]string{"someuser.123"})) |
| 273 |
gomega.Expect(payloads[2].Recipients).To(gomega.Equal([]string{"group.testgroup"})) |
| 274 |
}) |
| 275 |
|
| 276 |
ginkgo.It("should handle different response formats", func() { |
| 277 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 278 |
setupCapture(http.StatusCreated, `{"timestamp": "1234567890"}`, &captured) |
| 279 |
|
| 280 |
err := signal.Send("Test message", nil) |
| 281 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 282 |
}) |
| 283 |
|
| 284 |
ginkgo.It("should handle server errors gracefully", func() { |
| 285 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 286 |
setupCapture( |
| 287 |
http.StatusInternalServerError, |
| 288 |
`{"error": "Internal Server Error"}`, |
| 289 |
&captured, |
| 290 |
) |
| 291 |
|
| 292 |
err := signal.Send("Test message", nil) |
| 293 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 294 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("server returned status 500")) |
| 295 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Internal Server Error")) |
| 296 |
}) |
| 297 |
|
| 298 |
ginkgo.It("should reject unknown send params", func() { |
| 299 |
initMocked(signal, "signal://localhost:8080/+1234567890/+0987654321") |
| 300 |
|
| 301 |
params := types.Params{"unknown": "value"} |
| 302 |
err := signal.Send("Test message", ¶ms) |
| 303 |
gomega.Expect(err).To(gomega.HaveOccurred()) |
| 304 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("not a valid config key")) |
| 305 |
}) |
| 306 |
|
| 307 |
ginkgo.It("should return error when no recipients configured", func() { |
| 308 |
signal.Config = &Config{ |
| 309 |
Host: "localhost", |
| 310 |
Port: 8080, |
| 311 |
Source: "+1234567890", |
| 312 |
Recipients: []string{}, |
| 313 |
} |
| 314 |
|
| 315 |
err := signal.Send("Test message", nil) |
| 316 |
gomega.Expect(err).To(gomega.MatchError(ErrNoRecipients)) |
| 317 |
}) |
| 318 |
}) |
| 319 |
}) |