| 1 | package generate |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/spf13/cobra" |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | |
| 11 | "github.com/nicholas-fedor/shoutrrr/pkg/services/push/pushover" |
| 12 | ) |
| 13 | |
| 14 | // Test_loadArgsFromAltSources tests the loadArgsFromAltSources function with various scenarios. |
| 15 | func Test_loadArgsFromAltSources(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | |
| 18 | tests := []struct { |
| 19 | name string |
| 20 | args []string |
| 21 | wantService string |
| 22 | wantGen string |
| 23 | wantNoChange bool |
| 24 | }{ |
| 25 | { |
| 26 | name: "positional service only", |
| 27 | args: []string{"discord"}, |
| 28 | wantService: "discord", |
| 29 | wantGen: "basic", |
| 30 | }, |
| 31 | { |
| 32 | name: "positional both args", |
| 33 | args: []string{"discord", "basic"}, |
| 34 | wantService: "discord", |
| 35 | wantGen: "basic", |
| 36 | }, |
| 37 | { |
| 38 | name: "no positional args", |
| 39 | args: []string{}, |
| 40 | wantNoChange: true, |
| 41 | }, |
| 42 | } |
| 43 | |
| 44 | for _, tt := range tests { |
| 45 | t.Run(tt.name, func(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | |
| 48 | // Create a cobra command with the expected flags |
| 49 | cmd := &cobra.Command{ |
| 50 | Use: "generate", |
| 51 | } |
| 52 | cmd.Flags().StringP("service", "s", "", "Notification service") |
| 53 | cmd.Flags().StringP("generator", "g", "basic", "Generator to use") |
| 54 | |
| 55 | loadArgsFromAltSources(cmd, tt.args) |
| 56 | |
| 57 | if tt.wantNoChange { |
| 58 | // Verify flags remain at their defaults/empty values |
| 59 | service, _ := cmd.Flags().GetString("service") |
| 60 | assert.Empty(t, service, "service flag should remain empty") |
| 61 | } else { |
| 62 | // Verify the flags were set correctly |
| 63 | service, _ := cmd.Flags().GetString("service") |
| 64 | gen, _ := cmd.Flags().GetString("generator") |
| 65 | |
| 66 | assert.Equal(t, tt.wantService, service, "service flag mismatch") |
| 67 | assert.Equal(t, tt.wantGen, gen, "generator flag mismatch") |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Test_maskSensitiveURL tests the maskSensitiveURL function with various service schemas. |
| 74 | func Test_maskSensitiveURL(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | |
| 77 | tests := []struct { |
| 78 | name string |
| 79 | serviceSchema string |
| 80 | urlStr string |
| 81 | want string |
| 82 | }{ |
| 83 | { |
| 84 | name: "discord service masks user", |
| 85 | serviceSchema: "discord", |
| 86 | urlStr: "https://[email protected]/api/webhooks/123", |
| 87 | want: "https://[email protected]/api/webhooks/123", |
| 88 | }, |
| 89 | { |
| 90 | name: "slack service masks user", |
| 91 | serviceSchema: "slack", |
| 92 | urlStr: "https://[email protected]/services/123", |
| 93 | want: "https://[email protected]/services/123", |
| 94 | }, |
| 95 | { |
| 96 | name: "teams service masks user", |
| 97 | serviceSchema: "teams", |
| 98 | urlStr: "https://[email protected]/webhook", |
| 99 | want: "https://[email protected]/webhook", |
| 100 | }, |
| 101 | { |
| 102 | name: "smtp service masks password", |
| 103 | serviceSchema: "smtp", |
| 104 | urlStr: "smtp://user:pass@host:587", |
| 105 | want: "smtp://user:REDACTED@host:587", |
| 106 | }, |
| 107 | { |
| 108 | name: "pushover service masks token and user", |
| 109 | serviceSchema: "pushover", |
| 110 | urlStr: "pushover://pushover.net?token=abc&user=def", |
| 111 | want: "pushover://pushover.net?token=REDACTED&user=REDACTED", |
| 112 | }, |
| 113 | { |
| 114 | name: "pushover service masks userinfo token and encryption key", |
| 115 | serviceSchema: "pushover", |
| 116 | urlStr: "pushover://Token:secret-api-token@userkey/?encryptionkey=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", |
| 117 | want: "pushover://Token:REDACTED@userkey/?encryptionkey=REDACTED", |
| 118 | }, |
| 119 | { |
| 120 | name: "gotify service masks token", |
| 121 | serviceSchema: "gotify", |
| 122 | urlStr: "gotify://gotify.net?token=secret", |
| 123 | want: "gotify://gotify.net?token=REDACTED", |
| 124 | }, |
| 125 | { |
| 126 | name: "signalgrid service masks user and host", |
| 127 | serviceSchema: "signalgrid", |
| 128 | urlStr: "signalgrid://ClientKey@ChannelToken?title=Alert", |
| 129 | want: "signalgrid://REDACTED@REDACTED?title=Alert", |
| 130 | }, |
| 131 | { |
| 132 | name: "generic service masks user and query params", |
| 133 | serviceSchema: "unknown", |
| 134 | urlStr: "https://user:pass@host?a=1&b=2", |
| 135 | want: "https://REDACTED@host?a=REDACTED&b=REDACTED", |
| 136 | }, |
| 137 | { |
| 138 | name: "invalid URL returns original", |
| 139 | serviceSchema: "discord", |
| 140 | urlStr: "://invalid", |
| 141 | want: "://invalid", |
| 142 | }, |
| 143 | } |
| 144 | |
| 145 | for _, tt := range tests { |
| 146 | t.Run(tt.name, func(t *testing.T) { |
| 147 | t.Parallel() |
| 148 | |
| 149 | got := maskSensitiveURL(tt.serviceSchema, tt.urlStr) |
| 150 | assert.Equal(t, tt.want, got, "maskSensitiveURL() returned unexpected value") |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Test_maskSensitiveURL_pushoverGeneratePath verifies that the default generate |
| 156 | // path redacts both Config.Token and Config.EncryptionKey. |
| 157 | func Test_maskSensitiveURL_pushoverGeneratePath(t *testing.T) { |
| 158 | t.Parallel() |
| 159 | |
| 160 | cfg := &pushover.Config{ |
| 161 | Token: "secret-api-token", |
| 162 | User: "userkey", |
| 163 | EncryptionKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", |
| 164 | } |
| 165 | |
| 166 | masked := maskSensitiveURL(pushover.Scheme, cfg.GetURL().String()) |
| 167 | |
| 168 | assert.NotContains(t, masked, cfg.Token, "API token must not appear in the masked URL") |
| 169 | assert.NotContains(t, masked, cfg.EncryptionKey, "encryption key must not appear in the masked URL") |
| 170 | assert.Contains(t, masked, redactedStr, "masked URL should contain the redaction placeholder") |
| 171 | } |
| 172 | |
| 173 | // Test_maskUser tests the maskUser function. |
| 174 | func Test_maskUser(t *testing.T) { |
| 175 | t.Parallel() |
| 176 | |
| 177 | tests := []struct { |
| 178 | name string |
| 179 | urlStr string |
| 180 | placeholder string |
| 181 | wantUser string |
| 182 | }{ |
| 183 | { |
| 184 | name: "masks user with placeholder", |
| 185 | urlStr: "https://token@host/path", |
| 186 | placeholder: redactedStr, |
| 187 | wantUser: redactedStr, |
| 188 | }, |
| 189 | { |
| 190 | name: "empty user remains nil", |
| 191 | urlStr: "https://host/path", |
| 192 | placeholder: redactedStr, |
| 193 | wantUser: "", |
| 194 | }, |
| 195 | } |
| 196 | |
| 197 | for _, tt := range tests { |
| 198 | t.Run(tt.name, func(t *testing.T) { |
| 199 | t.Parallel() |
| 200 | |
| 201 | parsedURL, err := url.Parse(tt.urlStr) |
| 202 | require.NoError(t, err, "failed to parse URL") |
| 203 | |
| 204 | maskUser(parsedURL, tt.placeholder) |
| 205 | |
| 206 | if tt.wantUser == "" { |
| 207 | assert.Nil(t, parsedURL.User, "user should be nil") |
| 208 | } else { |
| 209 | assert.Equal(t, tt.wantUser, parsedURL.User.Username(), "user mismatch") |
| 210 | } |
| 211 | }) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Test_maskSMTPUser tests the maskSMTPUser function. |
| 216 | func Test_maskSMTPUser(t *testing.T) { |
| 217 | t.Parallel() |
| 218 | |
| 219 | tests := []struct { |
| 220 | name string |
| 221 | urlStr string |
| 222 | wantUser string |
| 223 | wantPass string |
| 224 | }{ |
| 225 | { |
| 226 | name: "masks password preserving username", |
| 227 | urlStr: "smtp://user:pass@host:587", |
| 228 | wantUser: "user", |
| 229 | wantPass: redactedStr, |
| 230 | }, |
| 231 | { |
| 232 | name: "user without password gets redacted password", |
| 233 | urlStr: "smtp://user@host:587", |
| 234 | wantUser: "user", |
| 235 | wantPass: redactedStr, |
| 236 | }, |
| 237 | { |
| 238 | name: "nil user remains nil", |
| 239 | urlStr: "smtp://host:587", |
| 240 | wantUser: "", |
| 241 | wantPass: "", |
| 242 | }, |
| 243 | } |
| 244 | |
| 245 | for _, tt := range tests { |
| 246 | t.Run(tt.name, func(t *testing.T) { |
| 247 | t.Parallel() |
| 248 | |
| 249 | parsedURL, err := url.Parse(tt.urlStr) |
| 250 | require.NoError(t, err, "failed to parse URL") |
| 251 | |
| 252 | maskSMTPUser(parsedURL) |
| 253 | |
| 254 | if tt.wantUser == "" { |
| 255 | assert.Nil(t, parsedURL.User, "user should be nil") |
| 256 | } else { |
| 257 | assert.Equal(t, tt.wantUser, parsedURL.User.Username(), "username mismatch") |
| 258 | |
| 259 | pass, hasPass := parsedURL.User.Password() |
| 260 | if tt.wantPass != "" { |
| 261 | assert.True(t, hasPass, "password should be set") |
| 262 | assert.Equal(t, tt.wantPass, pass, "password mismatch") |
| 263 | } |
| 264 | } |
| 265 | }) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Test_maskPushoverQuery tests the maskPushoverQuery function. |
| 270 | func Test_maskPushoverQuery(t *testing.T) { |
| 271 | t.Parallel() |
| 272 | |
| 273 | tests := []struct { |
| 274 | name string |
| 275 | urlStr string |
| 276 | wantTok string |
| 277 | wantUser string |
| 278 | wantKey string |
| 279 | wantKeyParam string |
| 280 | wantPass string |
| 281 | }{ |
| 282 | { |
| 283 | name: "masks token and user", |
| 284 | urlStr: "pushover://?token=abc&user=def", |
| 285 | wantTok: redactedStr, |
| 286 | wantUser: redactedStr, |
| 287 | }, |
| 288 | { |
| 289 | name: "masks only token", |
| 290 | urlStr: "pushover://?token=abc", |
| 291 | wantTok: redactedStr, |
| 292 | wantUser: "", |
| 293 | }, |
| 294 | { |
| 295 | name: "leaves other params unchanged", |
| 296 | urlStr: "pushover://?other=value", |
| 297 | wantTok: "", |
| 298 | wantUser: "", |
| 299 | }, |
| 300 | { |
| 301 | name: "masks encryptionkey query parameter", |
| 302 | urlStr: "pushover://?encryptionkey=secret-hex-key", |
| 303 | wantKey: redactedStr, |
| 304 | wantKeyParam: "encryptionkey", |
| 305 | }, |
| 306 | { |
| 307 | name: "masks key alias query parameter", |
| 308 | urlStr: "pushover://?key=secret-hex-key", |
| 309 | wantKey: redactedStr, |
| 310 | wantKeyParam: "key", |
| 311 | }, |
| 312 | { |
| 313 | name: "masks API token in URL userinfo", |
| 314 | urlStr: "pushover://Token:secret-api-token@userkey/", |
| 315 | wantPass: redactedStr, |
| 316 | }, |
| 317 | } |
| 318 | |
| 319 | for _, tt := range tests { |
| 320 | t.Run(tt.name, func(t *testing.T) { |
| 321 | t.Parallel() |
| 322 | |
| 323 | parsedURL, err := url.Parse(tt.urlStr) |
| 324 | require.NoError(t, err, "failed to parse URL") |
| 325 | |
| 326 | maskPushoverQuery(parsedURL) |
| 327 | |
| 328 | query := parsedURL.Query() |
| 329 | if tt.wantTok != "" { |
| 330 | assert.Equal(t, tt.wantTok, query.Get("token"), "token mismatch") |
| 331 | } |
| 332 | |
| 333 | if tt.wantUser != "" { |
| 334 | assert.Equal(t, tt.wantUser, query.Get("user"), "user mismatch") |
| 335 | } |
| 336 | |
| 337 | if tt.wantKeyParam != "" { |
| 338 | require.True(t, query.Has(tt.wantKeyParam), "expected %s to be present", tt.wantKeyParam) |
| 339 | assert.Equal(t, tt.wantKey, query.Get(tt.wantKeyParam), tt.wantKeyParam+" mismatch") |
| 340 | } |
| 341 | |
| 342 | if tt.wantPass != "" { |
| 343 | pass, hasPass := parsedURL.User.Password() |
| 344 | assert.True(t, hasPass, "password should be set") |
| 345 | assert.Equal(t, tt.wantPass, pass, "userinfo token mismatch") |
| 346 | } |
| 347 | |
| 348 | if tt.wantTok == "" && tt.wantUser == "" && tt.wantKey == "" && tt.wantPass == "" { |
| 349 | // Verify other params are unchanged |
| 350 | assert.Equal(t, "value", query.Get("other"), "other param should be unchanged") |
| 351 | } |
| 352 | }) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Test_maskGotifyQuery tests the maskGotifyQuery function. |
| 357 | func Test_maskGotifyQuery(t *testing.T) { |
| 358 | t.Parallel() |
| 359 | |
| 360 | tests := []struct { |
| 361 | name string |
| 362 | urlStr string |
| 363 | wantTok string |
| 364 | }{ |
| 365 | { |
| 366 | name: "masks token", |
| 367 | urlStr: "gotify://?token=secret", |
| 368 | wantTok: redactedStr, |
| 369 | }, |
| 370 | { |
| 371 | name: "leaves other params unchanged", |
| 372 | urlStr: "gotify://?other=value", |
| 373 | wantTok: "", |
| 374 | }, |
| 375 | } |
| 376 | |
| 377 | for _, tt := range tests { |
| 378 | t.Run(tt.name, func(t *testing.T) { |
| 379 | t.Parallel() |
| 380 | |
| 381 | parsedURL, err := url.Parse(tt.urlStr) |
| 382 | require.NoError(t, err, "failed to parse URL") |
| 383 | |
| 384 | maskGotifyQuery(parsedURL) |
| 385 | |
| 386 | query := parsedURL.Query() |
| 387 | if tt.wantTok != "" { |
| 388 | assert.Equal(t, tt.wantTok, query.Get("token"), "token mismatch") |
| 389 | } else { |
| 390 | // Verify other params are unchanged |
| 391 | assert.Equal(t, "value", query.Get("other"), "other param should be unchanged") |
| 392 | } |
| 393 | }) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // Test_maskGeneric tests the maskGeneric function. |
| 398 | func Test_maskGeneric(t *testing.T) { |
| 399 | t.Parallel() |
| 400 | |
| 401 | tests := []struct { |
| 402 | name string |
| 403 | urlStr string |
| 404 | wantUser string |
| 405 | wantParams map[string]string |
| 406 | wantQueryLen int |
| 407 | }{ |
| 408 | { |
| 409 | name: "masks user and all query params", |
| 410 | urlStr: "https://user:pass@host?a=1&b=2", |
| 411 | wantUser: redactedStr, |
| 412 | wantParams: map[string]string{ |
| 413 | "a": redactedStr, |
| 414 | "b": redactedStr, |
| 415 | }, |
| 416 | wantQueryLen: 2, |
| 417 | }, |
| 418 | { |
| 419 | name: "masks only user when no query params", |
| 420 | urlStr: "https://user:pass@host", |
| 421 | wantUser: redactedStr, |
| 422 | }, |
| 423 | { |
| 424 | name: "nil user with query params", |
| 425 | urlStr: "https://host?a=1", |
| 426 | wantUser: "", |
| 427 | wantParams: map[string]string{"a": redactedStr}, |
| 428 | wantQueryLen: 1, |
| 429 | }, |
| 430 | } |
| 431 | |
| 432 | for _, tt := range tests { |
| 433 | t.Run(tt.name, func(t *testing.T) { |
| 434 | t.Parallel() |
| 435 | |
| 436 | parsedURL, err := url.Parse(tt.urlStr) |
| 437 | require.NoError(t, err, "failed to parse URL") |
| 438 | |
| 439 | maskGeneric(parsedURL) |
| 440 | |
| 441 | if tt.wantUser != "" { |
| 442 | assert.Equal(t, tt.wantUser, parsedURL.User.Username(), "user mismatch") |
| 443 | } else if parsedURL.User != nil { |
| 444 | t.Errorf("expected nil user, got %v", parsedURL.User) |
| 445 | } |
| 446 | |
| 447 | query := parsedURL.Query() |
| 448 | if tt.wantParams != nil { |
| 449 | assert.Len(t, query, tt.wantQueryLen, "query param count mismatch") |
| 450 | |
| 451 | for key, wantVal := range tt.wantParams { |
| 452 | assert.Equal(t, wantVal, query.Get(key), "param %s mismatch", key) |
| 453 | } |
| 454 | } |
| 455 | }) |
| 456 | } |
| 457 | } |