| 1 |
package mqtt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
"testing" |
| 6 |
"testing/synctest" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/mock" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/push/mqtt" |
| 12 |
) |
| 13 |
|
| 14 |
func TestConfigValidURLParsing(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
synctest.Test(t, func(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
url string |
| 20 |
expectedHost string |
| 21 |
expectedPort int |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "mqtt scheme with custom port", |
| 25 |
url: "mqtt://broker.example.com:1884/alerts", |
| 26 |
expectedHost: "broker.example.com", |
| 27 |
expectedPort: 1884, |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "mqtts scheme with custom port", |
| 31 |
url: "mqtts://broker.example.com:8884/alerts", |
| 32 |
expectedHost: "broker.example.com", |
| 33 |
expectedPort: 8884, |
| 34 |
}, |
| 35 |
} |
| 36 |
|
| 37 |
for _, tt := range tests { |
| 38 |
mockManager := &MockConnectionManager{} |
| 39 |
mockManager.On("AwaitConnection", mock.Anything).Return(nil) |
| 40 |
mockManager.On("Publish", mock.Anything, mock.Anything). |
| 41 |
Return(createMockPublishResponse(0), nil). |
| 42 |
Once() |
| 43 |
|
| 44 |
service := createTestService(t, tt.url, mockManager) |
| 45 |
|
| 46 |
// Verify config was parsed correctly |
| 47 |
require.Equal(t, tt.expectedHost, service.Config.Host, "host mismatch for %s", tt.name) |
| 48 |
require.Equal(t, tt.expectedPort, service.Config.Port, "port mismatch for %s", tt.name) |
| 49 |
|
| 50 |
// Send a message to trigger connection |
| 51 |
err := service.Send("test message", nil) |
| 52 |
require.NoError(t, err) |
| 53 |
|
| 54 |
mockManager.AssertExpectations(t) |
| 55 |
} |
| 56 |
}) |
| 57 |
} |
| 58 |
|
| 59 |
func TestConfigQueryParameters(t *testing.T) { |
| 60 |
t.Parallel() |
| 61 |
synctest.Test(t, func(t *testing.T) { |
| 62 |
tests := []struct { |
| 63 |
name string |
| 64 |
url string |
| 65 |
expectedQoS byte |
| 66 |
expectedRetained bool |
| 67 |
expectedClientID string |
| 68 |
expectedCleanSession bool |
| 69 |
}{ |
| 70 |
{ |
| 71 |
name: "qos 0", |
| 72 |
url: "mqtt://broker.example.com/alerts?qos=0", |
| 73 |
expectedQoS: 0, |
| 74 |
expectedRetained: false, |
| 75 |
expectedClientID: "shoutrrr", |
| 76 |
expectedCleanSession: true, |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: "qos 1", |
| 80 |
url: "mqtt://broker.example.com/alerts?qos=1", |
| 81 |
expectedQoS: 1, |
| 82 |
expectedRetained: false, |
| 83 |
expectedClientID: "shoutrrr", |
| 84 |
expectedCleanSession: true, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "qos 2", |
| 88 |
url: "mqtt://broker.example.com/alerts?qos=2", |
| 89 |
expectedQoS: 2, |
| 90 |
expectedRetained: false, |
| 91 |
expectedClientID: "shoutrrr", |
| 92 |
expectedCleanSession: true, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "retained yes", |
| 96 |
url: "mqtt://broker.example.com/alerts?retained=yes", |
| 97 |
expectedQoS: 0, |
| 98 |
expectedRetained: true, |
| 99 |
expectedClientID: "shoutrrr", |
| 100 |
expectedCleanSession: true, |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: "retained true", |
| 104 |
url: "mqtt://broker.example.com/alerts?retained=true", |
| 105 |
expectedQoS: 0, |
| 106 |
expectedRetained: true, |
| 107 |
expectedClientID: "shoutrrr", |
| 108 |
expectedCleanSession: true, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "custom client id", |
| 112 |
url: "mqtt://broker.example.com/alerts?clientid=custom-client", |
| 113 |
expectedQoS: 0, |
| 114 |
expectedRetained: false, |
| 115 |
expectedClientID: "custom-client", |
| 116 |
expectedCleanSession: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "clean session no", |
| 120 |
url: "mqtt://broker.example.com/alerts?cleansession=no", |
| 121 |
expectedQoS: 0, |
| 122 |
expectedRetained: false, |
| 123 |
expectedClientID: "shoutrrr", |
| 124 |
expectedCleanSession: false, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "all options combined", |
| 128 |
url: "mqtt://broker.example.com/alerts?qos=1&retained=yes&clientid=myclient&cleansession=no", |
| 129 |
expectedQoS: 1, |
| 130 |
expectedRetained: true, |
| 131 |
expectedClientID: "myclient", |
| 132 |
expectedCleanSession: false, |
| 133 |
}, |
| 134 |
} |
| 135 |
|
| 136 |
for _, tt := range tests { |
| 137 |
mockManager := &MockConnectionManager{} |
| 138 |
mockManager.On("AwaitConnection", mock.Anything).Return(nil) |
| 139 |
mockManager.On("Publish", mock.Anything, mock.Anything). |
| 140 |
Return(createMockPublishResponse(0), nil). |
| 141 |
Once() |
| 142 |
|
| 143 |
service := createTestService(t, tt.url, mockManager) |
| 144 |
|
| 145 |
// Verify config was parsed correctly |
| 146 |
require.Equal(t, tt.expectedQoS, byte(service.Config.QoS)) |
| 147 |
require.Equal(t, tt.expectedRetained, service.Config.Retained) |
| 148 |
require.Equal(t, tt.expectedClientID, service.Config.ClientID) |
| 149 |
require.Equal(t, tt.expectedCleanSession, service.Config.CleanSession) |
| 150 |
|
| 151 |
// Send a message to verify the config is used |
| 152 |
err := service.Send("test message", nil) |
| 153 |
require.NoError(t, err) |
| 154 |
|
| 155 |
// Verify the publish was called with correct parameters |
| 156 |
publish := getPublishCall(mockManager) |
| 157 |
require.NotNil(t, publish) |
| 158 |
require.Equal(t, tt.expectedQoS, publish.QoS) |
| 159 |
require.Equal(t, tt.expectedRetained, publish.Retain) |
| 160 |
|
| 161 |
mockManager.AssertExpectations(t) |
| 162 |
} |
| 163 |
}) |
| 164 |
} |
| 165 |
|
| 166 |
func TestConfigUsernamePassword(t *testing.T) { |
| 167 |
t.Parallel() |
| 168 |
synctest.Test(t, func(t *testing.T) { |
| 169 |
tests := []struct { |
| 170 |
name string |
| 171 |
url string |
| 172 |
expectedUsername string |
| 173 |
expectedPassword string |
| 174 |
}{ |
| 175 |
{ |
| 176 |
name: "username only", |
| 177 |
url: "mqtt://[email protected]/alerts", |
| 178 |
expectedUsername: "user", |
| 179 |
expectedPassword: "", |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "username and password", |
| 183 |
url: "mqtt://user:[email protected]/alerts", |
| 184 |
expectedUsername: "user", |
| 185 |
expectedPassword: "pass", |
| 186 |
}, |
| 187 |
{ |
| 188 |
name: "username with special characters", |
| 189 |
url: "mqtt://user%40domain:pass%[email protected]/alerts", |
| 190 |
expectedUsername: "user@domain", |
| 191 |
expectedPassword: "pass@word", |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "no credentials", |
| 195 |
url: "mqtt://broker.example.com/alerts", |
| 196 |
expectedUsername: "", |
| 197 |
expectedPassword: "", |
| 198 |
}, |
| 199 |
} |
| 200 |
|
| 201 |
for _, tt := range tests { |
| 202 |
service := createTestService(t, tt.url) |
| 203 |
|
| 204 |
// Verify credentials were parsed correctly |
| 205 |
require.Equal(t, tt.expectedUsername, service.Config.Username) |
| 206 |
require.Equal(t, tt.expectedPassword, service.Config.Password) |
| 207 |
} |
| 208 |
}) |
| 209 |
} |
| 210 |
|
| 211 |
func TestConfigTLSOptions(t *testing.T) { |
| 212 |
t.Parallel() |
| 213 |
synctest.Test(t, func(t *testing.T) { |
| 214 |
tests := []struct { |
| 215 |
name string |
| 216 |
url string |
| 217 |
expectedDisableTLS bool |
| 218 |
expectedDisableTLSVerify bool |
| 219 |
}{ |
| 220 |
{ |
| 221 |
name: "tls enabled by default with mqtts", |
| 222 |
url: "mqtts://broker.example.com/alerts", |
| 223 |
expectedDisableTLS: false, |
| 224 |
expectedDisableTLSVerify: false, |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "tls disabled explicitly", |
| 228 |
url: "mqtts://broker.example.com/alerts?disabletls=yes", |
| 229 |
expectedDisableTLS: true, |
| 230 |
expectedDisableTLSVerify: false, |
| 231 |
}, |
| 232 |
{ |
| 233 |
name: "tls verification disabled", |
| 234 |
url: "mqtts://broker.example.com/alerts?disabletlsverification=yes", |
| 235 |
expectedDisableTLS: false, |
| 236 |
expectedDisableTLSVerify: true, |
| 237 |
}, |
| 238 |
{ |
| 239 |
name: "both tls options", |
| 240 |
url: "mqtts://broker.example.com/alerts?disabletls=yes&disabletlsverification=yes", |
| 241 |
expectedDisableTLS: true, |
| 242 |
expectedDisableTLSVerify: true, |
| 243 |
}, |
| 244 |
} |
| 245 |
|
| 246 |
for _, tt := range tests { |
| 247 |
service := createTestService(t, tt.url) |
| 248 |
|
| 249 |
// Verify TLS options were parsed correctly |
| 250 |
require.Equal(t, tt.expectedDisableTLS, service.Config.DisableTLS) |
| 251 |
require.Equal(t, tt.expectedDisableTLSVerify, service.Config.DisableTLSVerification) |
| 252 |
} |
| 253 |
}) |
| 254 |
} |
| 255 |
|
| 256 |
func TestConfigInvalidURLs(t *testing.T) { |
| 257 |
t.Parallel() |
| 258 |
synctest.Test(t, func(t *testing.T) { |
| 259 |
tests := []struct { |
| 260 |
name string |
| 261 |
url string |
| 262 |
expectError bool |
| 263 |
}{ |
| 264 |
{ |
| 265 |
name: "missing topic", |
| 266 |
url: "mqtt://broker.example.com", |
| 267 |
expectError: true, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "missing topic with trailing slash", |
| 271 |
url: "mqtt://broker.example.com/", |
| 272 |
expectError: true, |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "empty topic", |
| 276 |
url: "mqtt://broker.example.com/", |
| 277 |
expectError: true, |
| 278 |
}, |
| 279 |
} |
| 280 |
|
| 281 |
for _, tt := range tests { |
| 282 |
service := &mqtt.Service{} |
| 283 |
|
| 284 |
parsedURL, err := parseURLHelper(tt.url) |
| 285 |
if err != nil { |
| 286 |
continue // URL parsing failed, which is acceptable |
| 287 |
} |
| 288 |
|
| 289 |
err = service.Initialize(parsedURL, &mockLogger{}) |
| 290 |
if tt.expectError { |
| 291 |
require.Error(t, err) |
| 292 |
} |
| 293 |
} |
| 294 |
}) |
| 295 |
} |
| 296 |
|
| 297 |
func TestConfigInvalidQoS(t *testing.T) { |
| 298 |
t.Parallel() |
| 299 |
synctest.Test(t, func(t *testing.T) { |
| 300 |
tests := []struct { |
| 301 |
name string |
| 302 |
url string |
| 303 |
}{ |
| 304 |
{ |
| 305 |
name: "qos too high", |
| 306 |
url: "mqtt://broker.example.com/alerts?qos=3", |
| 307 |
}, |
| 308 |
{ |
| 309 |
name: "qos invalid string", |
| 310 |
url: "mqtt://broker.example.com/alerts?qos=invalid", |
| 311 |
}, |
| 312 |
} |
| 313 |
|
| 314 |
for _, tt := range tests { |
| 315 |
// Create service directly without mock - initialization should fail |
| 316 |
service := &mqtt.Service{} |
| 317 |
parsedURL, err := url.Parse(tt.url) |
| 318 |
require.NoError(t, err) |
| 319 |
|
| 320 |
// Initialize should fail due to invalid QoS |
| 321 |
err = service.Initialize(parsedURL, &mockLogger{}) |
| 322 |
require.Error(t, err, "expected initialization error for %s", tt.name) |
| 323 |
require.Contains(t, err.Error(), "qos") |
| 324 |
} |
| 325 |
}) |
| 326 |
} |
| 327 |
|
| 328 |
// parseURLHelper is a helper to parse URL strings, returning any parsing errors. |
| 329 |
func parseURLHelper(urlStr string) (*url.URL, error) { |
| 330 |
return url.Parse(urlStr) |
| 331 |
} |