| 1 | package signalgrid |
| 2 | |
| 3 | import ( |
| 4 | "github.com/onsi/ginkgo/v2" |
| 5 | "github.com/onsi/gomega" |
| 6 | |
| 7 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 | ) |
| 9 | |
| 10 | var _ = ginkgo.Describe("Config", func() { |
| 11 | var ( |
| 12 | config *Config |
| 13 | pkr format.PropKeyResolver |
| 14 | ) |
| 15 | |
| 16 | ginkgo.BeforeEach(func() { |
| 17 | config = &Config{} |
| 18 | pkr = format.NewPropKeyResolver(config) |
| 19 | gomega.Expect(pkr.SetDefaultProps(config)).To(gomega.Succeed()) |
| 20 | }) |
| 21 | |
| 22 | ginkgo.Describe("SetURL", func() { |
| 23 | ginkgo.It("should parse client key from userinfo and channel from host", func() { |
| 24 | err := config.SetURL(mustParseURL("signalgrid://abc123@channel")) |
| 25 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 26 | gomega.Expect(config.ClientKey).To(gomega.Equal("abc123")) |
| 27 | gomega.Expect(config.Channel).To(gomega.Equal("channel")) |
| 28 | }) |
| 29 | |
| 30 | ginkgo.It("should parse title, type, and critical query parameters", func() { |
| 31 | err := config.SetURL(mustParseURL( |
| 32 | "signalgrid://key@channel?title=Server%20Down&type=CRIT&critical=true", |
| 33 | )) |
| 34 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 35 | gomega.Expect(config.Title).To(gomega.Equal("Server Down")) |
| 36 | gomega.Expect(config.Type).To(gomega.Equal(TypeCRIT)) |
| 37 | gomega.Expect(config.Critical).To(gomega.BeTrue()) |
| 38 | }) |
| 39 | |
| 40 | ginkgo.It("should accept lowercase type aliases", func() { |
| 41 | err := config.SetURL(mustParseURL("signalgrid://key@channel?type=info")) |
| 42 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 43 | gomega.Expect(config.Type).To(gomega.Equal(TypeINFO)) |
| 44 | }) |
| 45 | |
| 46 | ginkgo.It("should reject an invalid type", func() { |
| 47 | err := config.SetURL(mustParseURL("signalgrid://key@channel?type=nope")) |
| 48 | gomega.Expect(err).To(gomega.HaveOccurred()) |
| 49 | }) |
| 50 | |
| 51 | ginkgo.It("should reject a missing client key", func() { |
| 52 | err := config.SetURL(mustParseURL("signalgrid://channel")) |
| 53 | gomega.Expect(err).To(gomega.MatchError(ErrClientKeyMissing)) |
| 54 | }) |
| 55 | |
| 56 | ginkgo.It("should reject a missing channel", func() { |
| 57 | err := config.SetURL(mustParseURL("signalgrid://key@")) |
| 58 | gomega.Expect(err).To(gomega.MatchError(ErrChannelMissing)) |
| 59 | }) |
| 60 | |
| 61 | ginkgo.It("should accept the docs dummy URL", func() { |
| 62 | err := config.SetURL(mustParseURL(dummyServiceURL)) |
| 63 | gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 64 | }) |
| 65 | }) |
| 66 | |
| 67 | ginkgo.Describe("GetURL", func() { |
| 68 | ginkgo.It("should round-trip client key and channel", func() { |
| 69 | config.ClientKey = "abc123" |
| 70 | config.Channel = "channel" |
| 71 | config.Title = "Alert" |
| 72 | config.Type = TypeCRIT |
| 73 | config.Critical = true |
| 74 | |
| 75 | got := config.GetURL() |
| 76 | gomega.Expect(got.Scheme).To(gomega.Equal(Scheme)) |
| 77 | gomega.Expect(got.User.Username()).To(gomega.Equal("abc123")) |
| 78 | gomega.Expect(got.Host).To(gomega.Equal("channel")) |
| 79 | |
| 80 | roundTrip := &Config{} |
| 81 | gomega.Expect(roundTrip.SetURL(got)).To(gomega.Succeed()) |
| 82 | gomega.Expect(roundTrip.ClientKey).To(gomega.Equal("abc123")) |
| 83 | gomega.Expect(roundTrip.Channel).To(gomega.Equal("channel")) |
| 84 | gomega.Expect(roundTrip.Title).To(gomega.Equal("Alert")) |
| 85 | gomega.Expect(roundTrip.Type).To(gomega.Equal(TypeCRIT)) |
| 86 | gomega.Expect(roundTrip.Critical).To(gomega.BeTrue()) |
| 87 | }) |
| 88 | |
| 89 | ginkgo.It("should omit default type from the query", func() { |
| 90 | config.ClientKey = "key" |
| 91 | config.Channel = "channel" |
| 92 | |
| 93 | got := config.GetURL() |
| 94 | gomega.Expect(got.Query().Get("type")).To(gomega.BeEmpty()) |
| 95 | gomega.Expect(got.Query().Get("critical")).To(gomega.BeEmpty()) |
| 96 | }) |
| 97 | }) |
| 98 | |
| 99 | ginkgo.Describe("SetDefaultProps", func() { |
| 100 | ginkgo.It("should default type to INFO", func() { |
| 101 | gomega.Expect(config.Type).To(gomega.Equal(TypeINFO)) |
| 102 | gomega.Expect(config.Type.String()).To(gomega.Equal("INFO")) |
| 103 | }) |
| 104 | }) |
| 105 | }) |