| 1 |
package discord_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/url" |
| 6 |
"testing" |
| 7 |
"testing/synctest" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/mock" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/chat/discord" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 14 |
) |
| 15 |
|
| 16 |
const contentTypeJSON = "application/json" |
| 17 |
|
| 18 |
func TestWebhookURLFormatCompliance(t *testing.T) { |
| 19 |
t.Parallel() |
| 20 |
synctest.Test(t, func(t *testing.T) { |
| 21 |
mockClient := &MockHTTPClient{} |
| 22 |
|
| 23 |
tests := []struct { |
| 24 |
name string |
| 25 |
url string |
| 26 |
shouldError bool |
| 27 |
}{ |
| 28 |
{"valid webhook URL", "discord://token@123", false}, |
| 29 |
{"webhook with numbers", "discord://123@456", false}, |
| 30 |
{"webhook with special chars", "discord://token_123@webhook-456", false}, |
| 31 |
{"empty token", "discord://@webhook", true}, |
| 32 |
{"empty webhook", "discord://token@", true}, |
| 33 |
{"missing scheme", "token@webhook", true}, |
| 34 |
{"wrong scheme", "http://token@webhook", false}, |
| 35 |
{"invalid host", "discord://[email protected]/webhook", true}, |
| 36 |
{"invalid path", "discord://token@123456789/invalid", true}, |
| 37 |
} |
| 38 |
|
| 39 |
for _, tt := range tests { |
| 40 |
parsedURL, parseErr := url.Parse(tt.url) |
| 41 |
if parseErr != nil { |
| 42 |
// URL parsing failed |
| 43 |
if tt.shouldError { |
| 44 |
// Expected to fail |
| 45 |
continue |
| 46 |
} |
| 47 |
// Unexpected parse failure |
| 48 |
t.Fatalf("Unexpected URL parse error for %s: %v", tt.name, parseErr) |
| 49 |
} |
| 50 |
|
| 51 |
// URL parsed successfully, try to create service |
| 52 |
service := &discord.Service{} |
| 53 |
|
| 54 |
initErr := service.Initialize(parsedURL, &mockLogger{}) |
| 55 |
if initErr != nil { |
| 56 |
// Initialize failed |
| 57 |
if tt.shouldError { |
| 58 |
// Expected to fail |
| 59 |
continue |
| 60 |
} |
| 61 |
// Unexpected init failure |
| 62 |
t.Fatalf("Unexpected Initialize error for %s: %v", tt.name, initErr) |
| 63 |
} |
| 64 |
|
| 65 |
// Initialize succeeded, set up mock and test Send |
| 66 |
mockClient.On("Do", mock.Anything). |
| 67 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 68 |
Once() |
| 69 |
|
| 70 |
// Override HTTPClient after Initialize |
| 71 |
service.HTTPClient = mockClient |
| 72 |
|
| 73 |
err := service.Send("test", nil) |
| 74 |
require.NoError(t, err, "Expected no error for valid test case: %s", tt.name) |
| 75 |
} |
| 76 |
|
| 77 |
mockClient.AssertExpectations(t) |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
func TestPayloadStructureCompliance(t *testing.T) { |
| 82 |
t.Parallel() |
| 83 |
synctest.Test(t, func(t *testing.T) { |
| 84 |
mockClient := &MockHTTPClient{} |
| 85 |
service := createTestService( |
| 86 |
t, |
| 87 |
"discord://test-token@test-webhook", |
| 88 |
mockClient, |
| 89 |
) |
| 90 |
|
| 91 |
mockClient.On("Do", mock.Anything). |
| 92 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 93 |
Once() |
| 94 |
|
| 95 |
err := service.Send("Test message", nil) |
| 96 |
|
| 97 |
require.NoError(t, err) |
| 98 |
assertRequestContains(t, mockClient, `"content":"Test message"`) |
| 99 |
|
| 100 |
mockClient.AssertExpectations(t) |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func TestEmbedStructureCompliance(t *testing.T) { |
| 105 |
t.Parallel() |
| 106 |
synctest.Test(t, func(t *testing.T) { |
| 107 |
mockClient := &MockHTTPClient{} |
| 108 |
mockClient.On("Do", mock.Anything). |
| 109 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 110 |
Once() |
| 111 |
|
| 112 |
service := createTestService( |
| 113 |
t, |
| 114 |
"discord://test-token@test-webhook", |
| 115 |
mockClient, |
| 116 |
) |
| 117 |
|
| 118 |
items := []types.MessageItem{ |
| 119 |
{ |
| 120 |
Text: "Test embed", |
| 121 |
Fields: []types.Field{ |
| 122 |
{Key: "embed_author_name", Value: "Test Author"}, |
| 123 |
{Key: "embed_image_url", Value: "https://example.com/image.png"}, |
| 124 |
{Key: "embed_thumbnail_url", Value: "https://example.com/thumb.png"}, |
| 125 |
{Key: "Field1", Value: "Value1"}, |
| 126 |
}, |
| 127 |
Level: types.Info, |
| 128 |
}, |
| 129 |
} |
| 130 |
|
| 131 |
err := service.SendItems(items, nil) |
| 132 |
|
| 133 |
require.NoError(t, err) |
| 134 |
// Verify embed structure |
| 135 |
assertRequestContains(t, mockClient, `"embeds":[{`) |
| 136 |
assertRequestContains(t, mockClient, `"description":"Test embed"`) |
| 137 |
assertRequestContains(t, mockClient, `"author":{"name":"Test Author"}`) |
| 138 |
assertRequestContains( |
| 139 |
t, |
| 140 |
mockClient, |
| 141 |
`"fields":[{"name":"Field1","value":"Value1"}]`, |
| 142 |
) |
| 143 |
assertRequestContains( |
| 144 |
t, |
| 145 |
mockClient, |
| 146 |
`"image":{"url":"https://example.com/image.png"}`, |
| 147 |
) |
| 148 |
assertRequestContains( |
| 149 |
t, |
| 150 |
mockClient, |
| 151 |
`"thumbnail":{"url":"https://example.com/thumb.png"}`, |
| 152 |
) |
| 153 |
assertRequestContains(t, mockClient, `"footer":{"text":"Info"}`) |
| 154 |
|
| 155 |
mockClient.AssertExpectations(t) |
| 156 |
}) |
| 157 |
} |
| 158 |
|
| 159 |
func TestFileUploadCompliance(t *testing.T) { |
| 160 |
t.Parallel() |
| 161 |
synctest.Test(t, func(t *testing.T) { |
| 162 |
mockClient := &MockHTTPClient{} |
| 163 |
service := createTestService( |
| 164 |
t, |
| 165 |
"discord://test-token@test-webhook", |
| 166 |
mockClient, |
| 167 |
) |
| 168 |
|
| 169 |
mockClient.On("Do", mock.Anything). |
| 170 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 171 |
Once() |
| 172 |
|
| 173 |
items := []types.MessageItem{ |
| 174 |
createTestMessageItemWithFile("File message", "test.txt", []byte("file content")), |
| 175 |
} |
| 176 |
|
| 177 |
err := service.SendItems(items, nil) |
| 178 |
|
| 179 |
require.NoError(t, err) |
| 180 |
// Verify multipart form data structure |
| 181 |
assertRequestContains(t, mockClient, "Content-Disposition: form-data") |
| 182 |
assertRequestContains(t, mockClient, `name="payload_json"`) |
| 183 |
assertRequestContains(t, mockClient, `name="files[0]"`) |
| 184 |
assertRequestContains(t, mockClient, `filename="test.txt"`) |
| 185 |
|
| 186 |
mockClient.AssertExpectations(t) |
| 187 |
}) |
| 188 |
} |
| 189 |
|
| 190 |
func TestThreadParameterCompliance(t *testing.T) { |
| 191 |
t.Parallel() |
| 192 |
synctest.Test(t, func(t *testing.T) { |
| 193 |
mockClient := &MockHTTPClient{} |
| 194 |
mockClient.On("Do", mock.Anything). |
| 195 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 196 |
Once() |
| 197 |
|
| 198 |
service := createTestService( |
| 199 |
t, |
| 200 |
"discord://test-token@test-webhook?thread_id=123456", |
| 201 |
mockClient, |
| 202 |
) |
| 203 |
|
| 204 |
err := service.Send("Thread message", nil) |
| 205 |
|
| 206 |
require.NoError(t, err) |
| 207 |
assertRequestMade( |
| 208 |
t, |
| 209 |
mockClient, |
| 210 |
"https://discord.com/api/webhooks/test-webhook/test-token?thread_id=123456", |
| 211 |
) |
| 212 |
|
| 213 |
mockClient.AssertExpectations(t) |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
func TestUsernameAvatarCompliance(t *testing.T) { |
| 218 |
t.Parallel() |
| 219 |
synctest.Test(t, func(t *testing.T) { |
| 220 |
mockClient := &MockHTTPClient{} |
| 221 |
service := createTestService( |
| 222 |
t, |
| 223 |
"discord://test-token@test-webhook", |
| 224 |
mockClient, |
| 225 |
) |
| 226 |
|
| 227 |
mockClient.On("Do", mock.Anything). |
| 228 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 229 |
Once() |
| 230 |
|
| 231 |
params := createTestParams( |
| 232 |
"username", |
| 233 |
"TestBot", |
| 234 |
"avatar", |
| 235 |
"https://example.com/avatar.png", |
| 236 |
) |
| 237 |
err := service.Send("Message with username/avatar", params) |
| 238 |
|
| 239 |
require.NoError(t, err) |
| 240 |
assertRequestContains(t, mockClient, `"username":"TestBot"`) |
| 241 |
assertRequestContains( |
| 242 |
t, |
| 243 |
mockClient, |
| 244 |
`"avatar_url":"https://example.com/avatar.png"`, |
| 245 |
) |
| 246 |
|
| 247 |
mockClient.AssertExpectations(t) |
| 248 |
}) |
| 249 |
} |
| 250 |
|
| 251 |
func TestJSONModeCompliance(t *testing.T) { |
| 252 |
t.Parallel() |
| 253 |
synctest.Test(t, func(t *testing.T) { |
| 254 |
mockClient := &MockHTTPClient{} |
| 255 |
mockClient.On("Do", mock.Anything). |
| 256 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 257 |
Once() |
| 258 |
|
| 259 |
service := createTestService( |
| 260 |
t, |
| 261 |
"discord://test-token@test-webhook?json=true", |
| 262 |
mockClient, |
| 263 |
) |
| 264 |
|
| 265 |
jsonPayload := `{"content": "Raw JSON message", "tts": true}` |
| 266 |
|
| 267 |
err := service.Send(jsonPayload, nil) |
| 268 |
|
| 269 |
require.NoError(t, err) |
| 270 |
assertRequestBody(t, mockClient, jsonPayload) |
| 271 |
|
| 272 |
mockClient.AssertExpectations(t) |
| 273 |
}) |
| 274 |
} |
| 275 |
|
| 276 |
func TestRateLimitHeaderCompliance(t *testing.T) { |
| 277 |
t.Parallel() |
| 278 |
synctest.Test(t, func(t *testing.T) { |
| 279 |
mockClient := &MockHTTPClient{} |
| 280 |
service := createTestService( |
| 281 |
t, |
| 282 |
"discord://test-token@test-webhook", |
| 283 |
mockClient, |
| 284 |
) |
| 285 |
|
| 286 |
// Test that service respects rate limit responses |
| 287 |
mockClient.On("Do", mock.Anything). |
| 288 |
Return(createMockResponse(http.StatusTooManyRequests, `{"retry_after": 1}`), nil). |
| 289 |
Once() |
| 290 |
mockClient.On("Do", mock.Anything). |
| 291 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 292 |
Once() |
| 293 |
|
| 294 |
err := service.Send("Rate limited message", nil) |
| 295 |
|
| 296 |
require.NoError(t, err) |
| 297 |
mockClient.AssertNumberOfCalls(t, "Do", 2) |
| 298 |
|
| 299 |
mockClient.AssertExpectations(t) |
| 300 |
}) |
| 301 |
} |
| 302 |
|
| 303 |
func TestContentTypeHeaderCompliance(t *testing.T) { |
| 304 |
t.Parallel() |
| 305 |
synctest.Test(t, func(t *testing.T) { |
| 306 |
mockClient := &MockHTTPClient{} |
| 307 |
service := createTestService( |
| 308 |
t, |
| 309 |
"discord://test-token@test-webhook", |
| 310 |
mockClient, |
| 311 |
) |
| 312 |
|
| 313 |
mockClient.On("Do", mock.Anything). |
| 314 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 315 |
Once() |
| 316 |
|
| 317 |
err := service.Send("Test message", nil) |
| 318 |
|
| 319 |
require.NoError(t, err) |
| 320 |
// Verify Content-Type header is set correctly |
| 321 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 322 |
return req.Header.Get("Content-Type") == contentTypeJSON |
| 323 |
}, "Content-Type header") |
| 324 |
|
| 325 |
mockClient.AssertExpectations(t) |
| 326 |
}) |
| 327 |
} |
| 328 |
|
| 329 |
func TestUserAgentHeaderCompliance(t *testing.T) { |
| 330 |
t.Parallel() |
| 331 |
synctest.Test(t, func(t *testing.T) { |
| 332 |
mockClient := &MockHTTPClient{} |
| 333 |
service := createTestService( |
| 334 |
t, |
| 335 |
"discord://test-token@test-webhook", |
| 336 |
mockClient, |
| 337 |
) |
| 338 |
|
| 339 |
mockClient.On("Do", mock.Anything). |
| 340 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 341 |
Once() |
| 342 |
|
| 343 |
err := service.Send("Test message", nil) |
| 344 |
|
| 345 |
require.NoError(t, err) |
| 346 |
// Verify User-Agent header is set |
| 347 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 348 |
return req.Header.Get("User-Agent") != "" |
| 349 |
}, "User-Agent header") |
| 350 |
|
| 351 |
mockClient.AssertExpectations(t) |
| 352 |
}) |
| 353 |
} |
| 354 |
|
| 355 |
func TestHTTPSRequirementCompliance(t *testing.T) { |
| 356 |
t.Parallel() |
| 357 |
synctest.Test(t, func(t *testing.T) { |
| 358 |
mockClient := &MockHTTPClient{} |
| 359 |
service := createTestService( |
| 360 |
t, |
| 361 |
"discord://test-token@test-webhook", |
| 362 |
mockClient, |
| 363 |
) |
| 364 |
|
| 365 |
// This should be tested by the service itself - all requests must use HTTPS |
| 366 |
mockClient.On("Do", mock.Anything). |
| 367 |
Return(createMockResponse(http.StatusNoContent, ""), nil). |
| 368 |
Once() |
| 369 |
|
| 370 |
err := service.Send("Test message", nil) |
| 371 |
|
| 372 |
require.NoError(t, err) |
| 373 |
assertRequestMatches(t, mockClient, func(req *http.Request) bool { |
| 374 |
return req.URL.Scheme == "https" |
| 375 |
}, "HTTPS scheme") |
| 376 |
|
| 377 |
mockClient.AssertExpectations(t) |
| 378 |
}) |
| 379 |
} |