| 1 | package ntfy |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/onsi/ginkgo/v2" |
| 7 | "github.com/onsi/gomega" |
| 8 | |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 | ) |
| 11 | |
| 12 | var _ = ginkgo.Describe("Config", func() { |
| 13 | var config *Config |
| 14 | |
| 15 | ginkgo.BeforeEach(func() { |
| 16 | config = &Config{} |
| 17 | }) |
| 18 | |
| 19 | ginkgo.Describe("Enums", func() { |
| 20 | ginkgo.It("should return a map with Priority enum formatter", func() { |
| 21 | enums := config.Enums() |
| 22 | gomega.Expect(enums).To(gomega.HaveKey("Priority")) |
| 23 | gomega.Expect(enums["Priority"]).NotTo(gomega.BeNil()) |
| 24 | }) |
| 25 | }) |
| 26 | |
| 27 | ginkgo.Describe("GetAPIURL", func() { |
| 28 | ginkgo.It("should construct URL with https scheme by default", func() { |
| 29 | config.Scheme = "https" |
| 30 | config.Host = "ntfy.sh" |
| 31 | config.Topic = "mytopic" |
| 32 | |
| 33 | result := config.GetAPIURL() |
| 34 | gomega.Expect(result).To(gomega.Equal("https://ntfy.sh/mytopic")) |
| 35 | }) |
| 36 | |
| 37 | ginkgo.It("should not double-slash when topic already has leading slash", func() { |
| 38 | config.Scheme = "https" |
| 39 | config.Host = "ntfy.sh" |
| 40 | config.Topic = "/mytopic" |
| 41 | |
| 42 | result := config.GetAPIURL() |
| 43 | gomega.Expect(result).To(gomega.Equal("https://ntfy.sh/mytopic")) |
| 44 | }) |
| 45 | |
| 46 | ginkgo.It("should handle nested topic paths", func() { |
| 47 | config.Scheme = "https" |
| 48 | config.Host = "ntfy.sh" |
| 49 | config.Topic = "notifications/alerts" |
| 50 | |
| 51 | result := config.GetAPIURL() |
| 52 | gomega.Expect(result).To(gomega.Equal("https://ntfy.sh/notifications/alerts")) |
| 53 | }) |
| 54 | |
| 55 | ginkgo.It("should use http scheme when configured", func() { |
| 56 | config.Scheme = "http" |
| 57 | config.Host = "ntfy.local" |
| 58 | config.Topic = "mytopic" |
| 59 | |
| 60 | result := config.GetAPIURL() |
| 61 | gomega.Expect(result).To(gomega.Equal("http://ntfy.local/mytopic")) |
| 62 | }) |
| 63 | }) |
| 64 | |
| 65 | ginkgo.Describe("GetURL", func() { |
| 66 | ginkgo.It("should build URL with all fields populated", func() { |
| 67 | config.Scheme = "https" |
| 68 | config.Host = "ntfy.sh" |
| 69 | config.Topic = "mytopic" |
| 70 | config.Username = "user" |
| 71 | config.Password = "pass" |
| 72 | config.Title = "Alert" |
| 73 | config.Priority = PriorityHigh |
| 74 | |
| 75 | result := config.GetURL() |
| 76 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 77 | gomega.Expect(result.Scheme).To(gomega.Equal(Scheme)) |
| 78 | gomega.Expect(result.Host).To(gomega.Equal("ntfy.sh")) |
| 79 | gomega.Expect(result.Path).To(gomega.Equal("/mytopic")) |
| 80 | gomega.Expect(result.User.Username()).To(gomega.Equal("user")) |
| 81 | pass, hasPass := result.User.Password() |
| 82 | gomega.Expect(hasPass).To(gomega.BeTrue()) |
| 83 | gomega.Expect(pass).To(gomega.Equal("pass")) |
| 84 | gomega.Expect(result.Query().Get("title")).To(gomega.Equal("Alert")) |
| 85 | gomega.Expect(result.Query().Get("priority")).To(gomega.Equal("High")) |
| 86 | }) |
| 87 | |
| 88 | ginkgo.It("should build URL without credentials when empty", func() { |
| 89 | config.Scheme = "https" |
| 90 | config.Host = "ntfy.sh" |
| 91 | config.Topic = "mytopic" |
| 92 | |
| 93 | result := config.GetURL() |
| 94 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 95 | gomega.Expect(result.User).To(gomega.BeNil()) |
| 96 | }) |
| 97 | |
| 98 | ginkgo.It("should build URL with username only", func() { |
| 99 | config.Scheme = "https" |
| 100 | config.Host = "ntfy.sh" |
| 101 | config.Topic = "mytopic" |
| 102 | config.Username = "user" |
| 103 | |
| 104 | result := config.GetURL() |
| 105 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 106 | gomega.Expect(result.User.Username()).To(gomega.Equal("user")) |
| 107 | _, hasPass := result.User.Password() |
| 108 | gomega.Expect(hasPass).To(gomega.BeFalse()) |
| 109 | }) |
| 110 | |
| 111 | ginkgo.It("should force query string even with no params", func() { |
| 112 | config.Scheme = "https" |
| 113 | config.Host = "ntfy.sh" |
| 114 | config.Topic = "mytopic" |
| 115 | |
| 116 | result := config.GetURL() |
| 117 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 118 | gomega.Expect(result.ForceQuery).To(gomega.BeTrue()) |
| 119 | }) |
| 120 | }) |
| 121 | |
| 122 | ginkgo.Describe("QueryFields", func() { |
| 123 | ginkgo.It("should return non-empty list of query fields", func() { |
| 124 | fields := config.QueryFields() |
| 125 | gomega.Expect(fields).NotTo(gomega.BeEmpty()) |
| 126 | }) |
| 127 | |
| 128 | ginkgo.It("should contain priority field", func() { |
| 129 | fields := config.QueryFields() |
| 130 | gomega.Expect(fields).To(gomega.ContainElement("priority")) |
| 131 | }) |
| 132 | |
| 133 | ginkgo.It("should contain title field", func() { |
| 134 | fields := config.QueryFields() |
| 135 | gomega.Expect(fields).To(gomega.ContainElement("title")) |
| 136 | }) |
| 137 | }) |
| 138 | |
| 139 | ginkgo.Describe("SetURL", func() { |
| 140 | ginkgo.It("should parse URL components correctly", func() { |
| 141 | testURL := mustParseURL("ntfy://user:[email protected]/mytopic?priority=5") |
| 142 | |
| 143 | err := config.SetURL(testURL) |
| 144 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 145 | gomega.Expect(config.Host).To(gomega.Equal("ntfy.example.com")) |
| 146 | gomega.Expect(config.Topic).To(gomega.Equal("mytopic")) |
| 147 | gomega.Expect(config.Username).To(gomega.Equal("user")) |
| 148 | gomega.Expect(config.Password).To(gomega.Equal("pass")) |
| 149 | gomega.Expect(config.Priority).To(gomega.Equal(PriorityMax)) |
| 150 | }) |
| 151 | |
| 152 | ginkgo.It("should parse URL without credentials", func() { |
| 153 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic") |
| 154 | |
| 155 | err := config.SetURL(testURL) |
| 156 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 157 | gomega.Expect(config.Username).To(gomega.Equal("")) |
| 158 | gomega.Expect(config.Password).To(gomega.Equal("")) |
| 159 | gomega.Expect(config.Host).To(gomega.Equal("ntfy.example.com")) |
| 160 | gomega.Expect(config.Topic).To(gomega.Equal("mytopic")) |
| 161 | }) |
| 162 | |
| 163 | ginkgo.It("should parse URL with username only", func() { |
| 164 | testURL := mustParseURL("ntfy://[email protected]/mytopic") |
| 165 | |
| 166 | err := config.SetURL(testURL) |
| 167 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 168 | gomega.Expect(config.Username).To(gomega.Equal("user")) |
| 169 | gomega.Expect(config.Password).To(gomega.Equal("")) |
| 170 | }) |
| 171 | |
| 172 | ginkgo.It("should return ErrTopicRequired for empty topic", func() { |
| 173 | testURL := mustParseURL("ntfy://ntfy.example.com/") |
| 174 | |
| 175 | err := config.SetURL(testURL) |
| 176 | gomega.Expect(err).To(gomega.Equal(ErrTopicRequired)) |
| 177 | }) |
| 178 | |
| 179 | ginkgo.It("should skip topic validation for dummy URL", func() { |
| 180 | testURL := mustParseURL("ntfy://[email protected]") |
| 181 | |
| 182 | err := config.SetURL(testURL) |
| 183 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 184 | }) |
| 185 | |
| 186 | ginkgo.It("should parse nested topic paths", func() { |
| 187 | testURL := mustParseURL("ntfy://ntfy.example.com/level1/level2/level3") |
| 188 | |
| 189 | err := config.SetURL(testURL) |
| 190 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 191 | gomega.Expect(config.Topic).To(gomega.Equal("level1/level2/level3")) |
| 192 | }) |
| 193 | |
| 194 | ginkgo.It("should parse query parameters", func() { |
| 195 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic?title=Alert&priority=High&tags=warning,skull") |
| 196 | |
| 197 | err := config.SetURL(testURL) |
| 198 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 199 | gomega.Expect(config.Title).To(gomega.Equal("Alert")) |
| 200 | gomega.Expect(config.Priority).To(gomega.Equal(PriorityHigh)) |
| 201 | gomega.Expect(config.Tags).To(gomega.Equal([]string{"warning", "skull"})) |
| 202 | }) |
| 203 | |
| 204 | ginkgo.It("should handle semicolons in query by splitting actions", func() { |
| 205 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic?actions=view;open") |
| 206 | |
| 207 | err := config.SetURL(testURL) |
| 208 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 209 | gomega.Expect(config.Actions).To(gomega.Equal([]string{"view", "open"})) |
| 210 | }) |
| 211 | |
| 212 | ginkgo.It("should parse markdown query parameter", func() { |
| 213 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic?markdown=yes") |
| 214 | |
| 215 | err := config.SetURL(testURL) |
| 216 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 217 | gomega.Expect(config.Markdown).To(gomega.BeTrue()) |
| 218 | }) |
| 219 | |
| 220 | ginkgo.It("should parse cache query parameter as No", func() { |
| 221 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic?cache=no") |
| 222 | |
| 223 | err := config.SetURL(testURL) |
| 224 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 225 | gomega.Expect(config.Cache).To(gomega.BeFalse()) |
| 226 | }) |
| 227 | }) |
| 228 | |
| 229 | ginkgo.Describe("getURL", func() { |
| 230 | ginkgo.It("should construct URL with ntfy scheme", func() { |
| 231 | config.Scheme = "https" |
| 232 | config.Host = "ntfy.sh" |
| 233 | config.Topic = "mytopic" |
| 234 | config.Username = "user" |
| 235 | config.Password = "pass" |
| 236 | |
| 237 | resolver := format.NewPropKeyResolver(config) |
| 238 | result := config.getURL(&resolver) |
| 239 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 240 | gomega.Expect(result.Scheme).To(gomega.Equal(Scheme)) |
| 241 | gomega.Expect(result.Host).To(gomega.Equal("ntfy.sh")) |
| 242 | gomega.Expect(result.Path).To(gomega.Equal("/mytopic")) |
| 243 | gomega.Expect(result.User.Username()).To(gomega.Equal("user")) |
| 244 | pass, hasPass := result.User.Password() |
| 245 | gomega.Expect(hasPass).To(gomega.BeTrue()) |
| 246 | gomega.Expect(pass).To(gomega.Equal("pass")) |
| 247 | }) |
| 248 | |
| 249 | ginkgo.It("should include username without password", func() { |
| 250 | config.Scheme = "https" |
| 251 | config.Host = "ntfy.sh" |
| 252 | config.Topic = "mytopic" |
| 253 | config.Username = "user" |
| 254 | |
| 255 | resolver := format.NewPropKeyResolver(config) |
| 256 | result := config.getURL(&resolver) |
| 257 | gomega.Expect(result).NotTo(gomega.BeNil()) |
| 258 | gomega.Expect(result.User.Username()).To(gomega.Equal("user")) |
| 259 | _, hasPass := result.User.Password() |
| 260 | gomega.Expect(hasPass).To(gomega.BeFalse()) |
| 261 | }) |
| 262 | }) |
| 263 | |
| 264 | ginkgo.Describe("setURL", func() { |
| 265 | ginkgo.It("should set topic from URL path without leading slash", func() { |
| 266 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic") |
| 267 | |
| 268 | resolver := format.NewPropKeyResolver(config) |
| 269 | err := config.setURL(&resolver, testURL) |
| 270 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 271 | gomega.Expect(config.Topic).To(gomega.Equal("mytopic")) |
| 272 | }) |
| 273 | |
| 274 | ginkgo.It("should set topic from URL path with leading slash", func() { |
| 275 | testURL := mustParseURL("ntfy://ntfy.example.com//mytopic") |
| 276 | |
| 277 | resolver := format.NewPropKeyResolver(config) |
| 278 | err := config.setURL(&resolver, testURL) |
| 279 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 280 | gomega.Expect(config.Topic).To(gomega.Equal("/mytopic")) |
| 281 | }) |
| 282 | |
| 283 | ginkgo.It("should clear credentials when URL has no user info", func() { |
| 284 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic") |
| 285 | |
| 286 | config.Username = "olduser" |
| 287 | config.Password = "oldpass" |
| 288 | |
| 289 | resolver := format.NewPropKeyResolver(config) |
| 290 | err := config.setURL(&resolver, testURL) |
| 291 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 292 | gomega.Expect(config.Username).To(gomega.Equal("")) |
| 293 | gomega.Expect(config.Password).To(gomega.Equal("")) |
| 294 | }) |
| 295 | |
| 296 | ginkgo.It("should set host from URL", func() { |
| 297 | testURL := mustParseURL("ntfy://ntfy.example.com:8080/mytopic") |
| 298 | |
| 299 | resolver := format.NewPropKeyResolver(config) |
| 300 | err := config.setURL(&resolver, testURL) |
| 301 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 302 | gomega.Expect(config.Host).To(gomega.Equal("ntfy.example.com:8080")) |
| 303 | }) |
| 304 | |
| 305 | ginkgo.It("should split semicolons in actions query", func() { |
| 306 | testURL := mustParseURL("ntfy://ntfy.example.com/mytopic?actions=a;b;c") |
| 307 | gomega.Expect(strings.Contains(testURL.RawQuery, ";")).To(gomega.BeTrue()) |
| 308 | |
| 309 | resolver := format.NewPropKeyResolver(config) |
| 310 | err := config.setURL(&resolver, testURL) |
| 311 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 312 | gomega.Expect(config.Actions).To(gomega.Equal([]string{"a", "b", "c"})) |
| 313 | }) |
| 314 | |
| 315 | ginkgo.It("should return ErrTopicRequired for empty topic except dummy URL", func() { |
| 316 | testURL := mustParseURL("ntfy://ntfy.example.com/") |
| 317 | |
| 318 | resolver := format.NewPropKeyResolver(config) |
| 319 | err := config.setURL(&resolver, testURL) |
| 320 | gomega.Expect(err).To(gomega.Equal(ErrTopicRequired)) |
| 321 | }) |
| 322 | |
| 323 | ginkgo.It("should skip topic validation for dummy URL", func() { |
| 324 | testURL := mustParseURL("ntfy://[email protected]") |
| 325 | |
| 326 | resolver := format.NewPropKeyResolver(config) |
| 327 | err := config.setURL(&resolver, testURL) |
| 328 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 329 | }) |
| 330 | }) |
| 331 | |
| 332 | ginkgo.Describe("default values", func() { |
| 333 | ginkgo.It("should apply defaults from struct tags when only required fields are set", func() { |
| 334 | svc := &Service{} |
| 335 | serviceURL := mustParseURL("ntfy://hostname/topic") |
| 336 | gomega.Expect(svc.Initialize(serviceURL, &noOpLogger{})).To(gomega.Succeed()) |
| 337 | gomega.Expect(svc.Config.Host).To(gomega.Equal("hostname")) |
| 338 | gomega.Expect(svc.Config.Topic).To(gomega.Equal("topic")) |
| 339 | gomega.Expect(svc.Config.Scheme).To(gomega.Equal("https")) |
| 340 | gomega.Expect(svc.Config.Tags).To(gomega.Equal([]string{""})) |
| 341 | gomega.Expect(svc.Config.Actions).To(gomega.Equal([]string{""})) |
| 342 | gomega.Expect(svc.Config.Priority).To(gomega.Equal(PriorityDefault)) |
| 343 | gomega.Expect(svc.Config.Markdown).To(gomega.BeFalse()) |
| 344 | gomega.Expect(svc.Config.Firebase).To(gomega.BeTrue()) |
| 345 | gomega.Expect(svc.Config.Cache).To(gomega.BeTrue()) |
| 346 | gomega.Expect(svc.Config.DisableTLSVerification).To(gomega.BeFalse()) |
| 347 | gomega.Expect(svc.Config.DisableTLS).To(gomega.BeFalse()) |
| 348 | }) |
| 349 | }) |
| 350 | |
| 351 | ginkgo.Describe("URL round-trip", func() { |
| 352 | ginkgo.It("should be identical after de-/serialization", func() { |
| 353 | testURL := "ntfy://user:[email protected]:2225/topic?cache=No&click=CLICK&disabletls=No&disabletlsverification=No&firebase=No&icon=ICON&markdown=No&priority=Max&scheme=http&title=TITLE" |
| 354 | cfg := &Config{} |
| 355 | pkr := format.NewPropKeyResolver(cfg) |
| 356 | gomega.Expect(cfg.setURL(&pkr, mustParseURL(testURL))).To(gomega.Succeed()) |
| 357 | gomega.Expect(cfg.GetURL().String()).To(gomega.Equal(testURL)) |
| 358 | }) |
| 359 | }) |
| 360 | |
| 361 | ginkgo.Describe("Service API compliance", func() { |
| 362 | ginkgo.It("should pass standard config tests", func() { |
| 363 | cfg := &Config{} |
| 364 | enums := cfg.Enums() |
| 365 | gomega.Expect(enums).To(gomega.HaveKey("Priority")) |
| 366 | |
| 367 | resolver := format.NewPropKeyResolver(cfg) |
| 368 | fields := resolver.QueryFields() |
| 369 | gomega.Expect(fields).To(gomega.HaveLen(18)) |
| 370 | }) |
| 371 | }) |
| 372 | }) |