|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestConfig_Struct(t *testing.T) { |
| 9 | + // Test that Config struct can be created and fields can be set |
| 10 | + cfg := &Config{ |
| 11 | + Provider: "slack", |
| 12 | + WebHookURL: "https://hooks.slack.com/test", |
| 13 | + Token: "test-token", |
| 14 | + ChannelId: "C1234567890", |
| 15 | + ChatId: "123456789", |
| 16 | + ConnectionURL: "redis://localhost:6379", |
| 17 | + SMTPHost: "smtp.gmail.com", |
| 18 | + SMTPPort: "587", |
| 19 | + UseTLS: false, |
| 20 | + UseSTARTTLS: true, |
| 21 | + |
| 22 | + SMTPPassword: "test-password", |
| 23 | + SMTPSubject: "Test Subject", |
| 24 | + |
| 25 | + |
| 26 | + GotifyURL: "https://gotify.example.com", |
| 27 | + GotifyToken: "gotify-token", |
| 28 | + GotifyTitle: "Test Title", |
| 29 | + GotifyPriority: 5, |
| 30 | + } |
| 31 | + |
| 32 | + // Verify all fields are set correctly |
| 33 | + if cfg.Provider != "slack" { |
| 34 | + t.Errorf("expected Provider 'slack', got '%s'", cfg.Provider) |
| 35 | + } |
| 36 | + if cfg.WebHookURL != "https://hooks.slack.com/test" { |
| 37 | + t.Errorf("expected WebHookURL 'https://hooks.slack.com/test', got '%s'", cfg.WebHookURL) |
| 38 | + } |
| 39 | + if cfg.Token != "test-token" { |
| 40 | + t.Errorf("expected Token 'test-token', got '%s'", cfg.Token) |
| 41 | + } |
| 42 | + if cfg.ChannelId != "C1234567890" { |
| 43 | + t.Errorf("expected ChannelId 'C1234567890', got '%s'", cfg.ChannelId) |
| 44 | + } |
| 45 | + if cfg.ChatId != "123456789" { |
| 46 | + t.Errorf("expected ChatId '123456789', got '%s'", cfg.ChatId) |
| 47 | + } |
| 48 | + if cfg.ConnectionURL != "redis://localhost:6379" { |
| 49 | + t.Errorf("expected ConnectionURL 'redis://localhost:6379', got '%s'", cfg.ConnectionURL) |
| 50 | + } |
| 51 | + if cfg.SMTPHost != "smtp.gmail.com" { |
| 52 | + t.Errorf("expected SMTPHost 'smtp.gmail.com', got '%s'", cfg.SMTPHost) |
| 53 | + } |
| 54 | + if cfg.SMTPPort != "587" { |
| 55 | + t.Errorf("expected SMTPPort '587', got '%s'", cfg.SMTPPort) |
| 56 | + } |
| 57 | + if cfg.UseTLS != false { |
| 58 | + t.Errorf("expected UseTLS false, got %v", cfg.UseTLS) |
| 59 | + } |
| 60 | + if cfg.UseSTARTTLS != true { |
| 61 | + t.Errorf("expected UseSTARTTLS true, got %v", cfg.UseSTARTTLS) |
| 62 | + } |
| 63 | + if cfg. SMTPUser != "[email protected]" { |
| 64 | + t. Errorf( "expected SMTPUser '[email protected]', got '%s'", cfg. SMTPUser) |
| 65 | + } |
| 66 | + if cfg.SMTPPassword != "test-password" { |
| 67 | + t.Errorf("expected SMTPPassword 'test-password', got '%s'", cfg.SMTPPassword) |
| 68 | + } |
| 69 | + if cfg.SMTPSubject != "Test Subject" { |
| 70 | + t.Errorf("expected SMTPSubject 'Test Subject', got '%s'", cfg.SMTPSubject) |
| 71 | + } |
| 72 | + if cfg. SMTPFrom != "[email protected]" { |
| 73 | + t. Errorf( "expected SMTPFrom '[email protected]', got '%s'", cfg. SMTPFrom) |
| 74 | + } |
| 75 | + if cfg. SMTPTo != "[email protected]" { |
| 76 | + t. Errorf( "expected SMTPTo '[email protected]', got '%s'", cfg. SMTPTo) |
| 77 | + } |
| 78 | + if cfg.GotifyURL != "https://gotify.example.com" { |
| 79 | + t.Errorf("expected GotifyURL 'https://gotify.example.com', got '%s'", cfg.GotifyURL) |
| 80 | + } |
| 81 | + if cfg.GotifyToken != "gotify-token" { |
| 82 | + t.Errorf("expected GotifyToken 'gotify-token', got '%s'", cfg.GotifyToken) |
| 83 | + } |
| 84 | + if cfg.GotifyTitle != "Test Title" { |
| 85 | + t.Errorf("expected GotifyTitle 'Test Title', got '%s'", cfg.GotifyTitle) |
| 86 | + } |
| 87 | + if cfg.GotifyPriority != 5 { |
| 88 | + t.Errorf("expected GotifyPriority 5, got %d", cfg.GotifyPriority) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestConfig_ProviderConfigurations(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + config Config |
| 96 | + provider string |
| 97 | + }{ |
| 98 | + { |
| 99 | + name: "slack configuration", |
| 100 | + config: Config{ |
| 101 | + Provider: "slack", |
| 102 | + Token: "slack-token", |
| 103 | + ChannelId: "C1234567890", |
| 104 | + }, |
| 105 | + provider: "slack", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "discord configuration", |
| 109 | + config: Config{ |
| 110 | + Provider: "discord", |
| 111 | + WebHookURL: "https://discord.com/api/webhooks/test", |
| 112 | + }, |
| 113 | + provider: "discord", |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "telegram configuration", |
| 117 | + config: Config{ |
| 118 | + Provider: "telegram", |
| 119 | + Token: "telegram-token", |
| 120 | + ChatId: "123456789", |
| 121 | + }, |
| 122 | + provider: "telegram", |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "smtp configuration", |
| 126 | + config: Config{ |
| 127 | + Provider: "smtp", |
| 128 | + SMTPHost: "smtp.gmail.com", |
| 129 | + SMTPPort: "587", |
| 130 | + UseSTARTTLS: true, |
| 131 | + |
| 132 | + SMTPPassword: "password", |
| 133 | + |
| 134 | + |
| 135 | + }, |
| 136 | + provider: "smtp", |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "redis configuration", |
| 140 | + config: Config{ |
| 141 | + Provider: "redis", |
| 142 | + ConnectionURL: "redis://localhost:6379", |
| 143 | + ChannelId: "test-channel", |
| 144 | + }, |
| 145 | + provider: "redis", |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "gotify configuration", |
| 149 | + config: Config{ |
| 150 | + Provider: "gotify", |
| 151 | + GotifyURL: "https://gotify.example.com", |
| 152 | + GotifyToken: "gotify-token", |
| 153 | + GotifyPriority: 5, |
| 154 | + }, |
| 155 | + provider: "gotify", |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.name, func(t *testing.T) { |
| 161 | + if tt.config.Provider != tt.provider { |
| 162 | + t.Errorf("expected Provider '%s', got '%s'", tt.provider, tt.config.Provider) |
| 163 | + } |
| 164 | + |
| 165 | + // Verify the config struct is properly initialized |
| 166 | + configType := reflect.TypeOf(tt.config) |
| 167 | + configValue := reflect.ValueOf(tt.config) |
| 168 | + |
| 169 | + for i := 0; i < configType.NumField(); i++ { |
| 170 | + field := configType.Field(i) |
| 171 | + value := configValue.Field(i) |
| 172 | + |
| 173 | + // Check that fields are accessible (not panicking) |
| 174 | + _ = field.Name |
| 175 | + _ = value.Interface() |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func TestConfig_ZeroValues(t *testing.T) { |
| 182 | + // Test zero values of Config struct |
| 183 | + var cfg Config |
| 184 | + |
| 185 | + // Verify zero values |
| 186 | + if cfg.Provider != "" { |
| 187 | + t.Errorf("expected zero value for Provider, got '%s'", cfg.Provider) |
| 188 | + } |
| 189 | + if cfg.WebHookURL != "" { |
| 190 | + t.Errorf("expected zero value for WebHookURL, got '%s'", cfg.WebHookURL) |
| 191 | + } |
| 192 | + if cfg.Token != "" { |
| 193 | + t.Errorf("expected zero value for Token, got '%s'", cfg.Token) |
| 194 | + } |
| 195 | + if cfg.ChannelId != "" { |
| 196 | + t.Errorf("expected zero value for ChannelId, got '%s'", cfg.ChannelId) |
| 197 | + } |
| 198 | + if cfg.UseTLS != false { |
| 199 | + t.Errorf("expected zero value for UseTLS, got %v", cfg.UseTLS) |
| 200 | + } |
| 201 | + if cfg.UseSTARTTLS != false { |
| 202 | + t.Errorf("expected zero value for UseSTARTTLS, got %v", cfg.UseSTARTTLS) |
| 203 | + } |
| 204 | + if cfg.GotifyPriority != 0 { |
| 205 | + t.Errorf("expected zero value for GotifyPriority, got %d", cfg.GotifyPriority) |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func TestConfig_InvalidDataTypes(t *testing.T) { |
| 210 | + tests := []struct { |
| 211 | + name string |
| 212 | + config Config |
| 213 | + expectError bool |
| 214 | + description string |
| 215 | + }{ |
| 216 | + { |
| 217 | + name: "invalid port number", |
| 218 | + config: Config{ |
| 219 | + SMTPPort: "not-a-number", |
| 220 | + }, |
| 221 | + expectError: false, // Config struct doesn't validate data types |
| 222 | + description: "Config struct accepts any string values", |
| 223 | + }, |
| 224 | + { |
| 225 | + name: "negative priority", |
| 226 | + config: Config{ |
| 227 | + GotifyPriority: -1, |
| 228 | + }, |
| 229 | + expectError: false, // Config struct doesn't validate ranges |
| 230 | + description: "Config struct accepts any int values", |
| 231 | + }, |
| 232 | + { |
| 233 | + name: "very large priority", |
| 234 | + config: Config{ |
| 235 | + GotifyPriority: 999999, |
| 236 | + }, |
| 237 | + expectError: false, |
| 238 | + description: "Config struct accepts any int values", |
| 239 | + }, |
| 240 | + { |
| 241 | + name: "empty strings", |
| 242 | + config: Config{ |
| 243 | + Provider: "", |
| 244 | + WebHookURL: "", |
| 245 | + Token: "", |
| 246 | + ChannelId: "", |
| 247 | + ChatId: "", |
| 248 | + ConnectionURL: "", |
| 249 | + SMTPHost: "", |
| 250 | + SMTPPort: "", |
| 251 | + SMTPUser: "", |
| 252 | + SMTPPassword: "", |
| 253 | + SMTPSubject: "", |
| 254 | + SMTPFrom: "", |
| 255 | + SMTPTo: "", |
| 256 | + GotifyURL: "", |
| 257 | + GotifyToken: "", |
| 258 | + GotifyTitle: "", |
| 259 | + }, |
| 260 | + expectError: false, |
| 261 | + description: "All string fields can be empty", |
| 262 | + }, |
| 263 | + { |
| 264 | + name: "very long strings", |
| 265 | + config: Config{ |
| 266 | + Provider: string(make([]byte, 10000)), |
| 267 | + WebHookURL: string(make([]byte, 10000)), |
| 268 | + Token: string(make([]byte, 10000)), |
| 269 | + ChannelId: string(make([]byte, 10000)), |
| 270 | + ChatId: string(make([]byte, 10000)), |
| 271 | + ConnectionURL: string(make([]byte, 10000)), |
| 272 | + SMTPHost: string(make([]byte, 10000)), |
| 273 | + SMTPPort: string(make([]byte, 10000)), |
| 274 | + SMTPUser: string(make([]byte, 10000)), |
| 275 | + SMTPPassword: string(make([]byte, 10000)), |
| 276 | + SMTPSubject: string(make([]byte, 10000)), |
| 277 | + SMTPFrom: string(make([]byte, 10000)), |
| 278 | + SMTPTo: string(make([]byte, 10000)), |
| 279 | + GotifyURL: string(make([]byte, 10000)), |
| 280 | + GotifyToken: string(make([]byte, 10000)), |
| 281 | + GotifyTitle: string(make([]byte, 10000)), |
| 282 | + }, |
| 283 | + expectError: false, |
| 284 | + description: "Config struct accepts very long strings", |
| 285 | + }, |
| 286 | + { |
| 287 | + name: "strings with special characters", |
| 288 | + config: Config{ |
| 289 | + Provider: "provider_with_émojis_🎉_and_spëcial_chärs", |
| 290 | + WebHookURL: "https://example.com/path with spaces?query=value&other=🎉", |
| 291 | + Token: "token-with-special-chars!@#$%^&*()", |
| 292 | + ChannelId: "channel_123_!@#", |
| 293 | + ChatId: "chat-456_!@#", |
| 294 | + ConnectionURL: "redis://user:pass@host:6379/db?param=value&other=🎉", |
| 295 | + SMTPHost: "smtp.gmail.com", |
| 296 | + SMTPPort: "587", |
| 297 | + |
| 298 | + SMTPPassword: "pass!@#$%^&*()", |
| 299 | + SMTPSubject: "Subject with émojis 🎉", |
| 300 | + |
| 301 | + |
| 302 | + GotifyURL: "https://gotify.example.com", |
| 303 | + GotifyToken: "token_!@#$%^&*()", |
| 304 | + GotifyTitle: "Title with spëcial chärs 🎉", |
| 305 | + }, |
| 306 | + expectError: false, |
| 307 | + description: "Config struct accepts strings with special characters", |
| 308 | + }, |
| 309 | + { |
| 310 | + name: "malformed URLs", |
| 311 | + config: Config{ |
| 312 | + WebHookURL: "not-a-url", |
| 313 | + ConnectionURL: "invalid-url-format", |
| 314 | + GotifyURL: "also-not-a-url", |
| 315 | + }, |
| 316 | + expectError: false, |
| 317 | + description: "Config struct doesn't validate URL formats", |
| 318 | + }, |
| 319 | + { |
| 320 | + name: "invalid email formats", |
| 321 | + config: Config{ |
| 322 | + SMTPUser: "not-an-email", |
| 323 | + SMTPFrom: "also-not-an-email", |
| 324 | + SMTPTo: "invalid-email-1,invalid-email-2", |
| 325 | + }, |
| 326 | + expectError: false, |
| 327 | + description: "Config struct doesn't validate email formats", |
| 328 | + }, |
| 329 | + } |
| 330 | + |
| 331 | + for _, tt := range tests { |
| 332 | + t.Run(tt.name, func(t *testing.T) { |
| 333 | + // Config struct doesn't perform validation, so we just test that it can hold the values |
| 334 | + if tt.expectError { |
| 335 | + t.Errorf("unexpected: Config struct should not validate data, but test expects error: %s", tt.description) |
| 336 | + } |
| 337 | + |
| 338 | + // Verify the config struct can hold these values without panicking |
| 339 | + _ = tt.config.Provider |
| 340 | + _ = tt.config.WebHookURL |
| 341 | + _ = tt.config.Token |
| 342 | + _ = tt.config.ChannelId |
| 343 | + _ = tt.config.ChatId |
| 344 | + _ = tt.config.ConnectionURL |
| 345 | + _ = tt.config.SMTPHost |
| 346 | + _ = tt.config.SMTPPort |
| 347 | + _ = tt.config.UseTLS |
| 348 | + _ = tt.config.UseSTARTTLS |
| 349 | + _ = tt.config.SMTPUser |
| 350 | + _ = tt.config.SMTPPassword |
| 351 | + _ = tt.config.SMTPSubject |
| 352 | + _ = tt.config.SMTPFrom |
| 353 | + _ = tt.config.SMTPTo |
| 354 | + _ = tt.config.GotifyURL |
| 355 | + _ = tt.config.GotifyToken |
| 356 | + _ = tt.config.GotifyTitle |
| 357 | + _ = tt.config.GotifyPriority |
| 358 | + }) |
| 359 | + } |
| 360 | +} |
0 commit comments