| 1 |
package homeassistant |
| 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 token from userinfo and host from hostname", func() { |
| 24 |
err := config.SetURL(mustParseURL("homeassistant://[email protected]")) |
| 25 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 26 |
gomega.Expect(config.Token).To(gomega.Equal("s3cret")) |
| 27 |
gomega.Expect(config.Host).To(gomega.Equal("ha.example.com")) |
| 28 |
gomega.Expect(config.Port).To(gomega.Equal(0)) |
| 29 |
gomega.Expect(config.Path).To(gomega.BeEmpty()) |
| 30 |
gomega.Expect(config.DisableTLS).To(gomega.BeFalse()) |
| 31 |
}) |
| 32 |
|
| 33 |
ginkgo.It("should parse an explicit port and path prefix", func() { |
| 34 |
err := config.SetURL(mustParseURL( |
| 35 |
"homeassistant://[email protected]:8123/hass/?disabletls=yes", |
| 36 |
)) |
| 37 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 38 |
gomega.Expect(config.Port).To(gomega.Equal(8123)) |
| 39 |
gomega.Expect(config.Path).To(gomega.Equal("/hass")) |
| 40 |
gomega.Expect(config.DisableTLS).To(gomega.BeTrue()) |
| 41 |
}) |
| 42 |
|
| 43 |
ginkgo.It("should parse title, service, targets, nid, and skiptlsverify", func() { |
| 44 |
err := config.SetURL(mustParseURL( |
| 45 |
"homeassistant://[email protected]" + |
| 46 |
"?title=Update&service=notify.mobile_app_phone&targets=device1,device2" + |
| 47 |
"&nid=watchtower&skiptlsverify=yes", |
| 48 |
)) |
| 49 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 50 |
gomega.Expect(config.Title).To(gomega.Equal("Update")) |
| 51 |
gomega.Expect(config.Service).To(gomega.Equal("notify.mobile_app_phone")) |
| 52 |
gomega.Expect(config.Targets).To(gomega.Equal([]string{"device1", "device2"})) |
| 53 |
gomega.Expect(config.Nid).To(gomega.Equal("watchtower")) |
| 54 |
gomega.Expect(config.SkipTLSVerify).To(gomega.BeTrue()) |
| 55 |
}) |
| 56 |
|
| 57 |
ginkgo.It("should reject a missing token", func() { |
| 58 |
err := config.SetURL(mustParseURL("homeassistant://ha.example.com")) |
| 59 |
gomega.Expect(err).To(gomega.MatchError(ErrTokenMissing)) |
| 60 |
}) |
| 61 |
|
| 62 |
ginkgo.It("should reject a missing host", func() { |
| 63 |
err := config.SetURL(mustParseURL("homeassistant://s3cret@")) |
| 64 |
gomega.Expect(err).To(gomega.MatchError(ErrHostMissing)) |
| 65 |
}) |
| 66 |
|
| 67 |
ginkgo.It("should reject an invalid port", func() { |
| 68 |
err := config.SetURL(mustParseURL("homeassistant://[email protected]:99999")) |
| 69 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidPort)) |
| 70 |
}) |
| 71 |
|
| 72 |
ginkgo.It("should reject an invalid service mapping", func() { |
| 73 |
err := config.SetURL(mustParseURL("homeassistant://[email protected]?service=notify.")) |
| 74 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidService)) |
| 75 |
}) |
| 76 |
|
| 77 |
ginkgo.It("should reject a service that would escape the API path", func() { |
| 78 |
err := config.SetURL(mustParseURL( |
| 79 |
"homeassistant://[email protected]?service=/api/events/foo", |
| 80 |
)) |
| 81 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidService)) |
| 82 |
|
| 83 |
err = config.SetURL(mustParseURL( |
| 84 |
"homeassistant://[email protected]?service=../config", |
| 85 |
)) |
| 86 |
gomega.Expect(err).To(gomega.MatchError(ErrInvalidService)) |
| 87 |
}) |
| 88 |
|
| 89 |
ginkgo.It("should accept the docs dummy URL", func() { |
| 90 |
err := config.SetURL(mustParseURL(dummyServiceURL)) |
| 91 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 92 |
}) |
| 93 |
}) |
| 94 |
|
| 95 |
ginkgo.Describe("GetURL", func() { |
| 96 |
ginkgo.It("should round-trip token, host, path, and query fields", func() { |
| 97 |
config.Token = "s3cret" |
| 98 |
config.Host = "ha.example.com" |
| 99 |
config.Port = 8443 |
| 100 |
config.Path = "/hass" |
| 101 |
config.Title = "Update" |
| 102 |
config.Service = "notify.mobile_app_phone" |
| 103 |
config.Targets = []string{"device1"} |
| 104 |
config.Nid = "watchtower" |
| 105 |
config.DisableTLS = true |
| 106 |
|
| 107 |
got := config.GetURL() |
| 108 |
gomega.Expect(got.Scheme).To(gomega.Equal(Scheme)) |
| 109 |
gomega.Expect(got.User.Username()).To(gomega.Equal("s3cret")) |
| 110 |
gomega.Expect(got.Hostname()).To(gomega.Equal("ha.example.com")) |
| 111 |
gomega.Expect(got.Path).To(gomega.Equal("/hass")) |
| 112 |
|
| 113 |
roundTrip := &Config{} |
| 114 |
gomega.Expect(roundTrip.SetURL(got)).To(gomega.Succeed()) |
| 115 |
gomega.Expect(roundTrip.Token).To(gomega.Equal("s3cret")) |
| 116 |
gomega.Expect(roundTrip.Host).To(gomega.Equal("ha.example.com")) |
| 117 |
gomega.Expect(roundTrip.Port).To(gomega.Equal(8443)) |
| 118 |
gomega.Expect(roundTrip.Path).To(gomega.Equal("/hass")) |
| 119 |
gomega.Expect(roundTrip.Title).To(gomega.Equal("Update")) |
| 120 |
gomega.Expect(roundTrip.Service).To(gomega.Equal("notify.mobile_app_phone")) |
| 121 |
gomega.Expect(roundTrip.Targets).To(gomega.Equal([]string{"device1"})) |
| 122 |
gomega.Expect(roundTrip.Nid).To(gomega.Equal("watchtower")) |
| 123 |
gomega.Expect(roundTrip.DisableTLS).To(gomega.BeTrue()) |
| 124 |
}) |
| 125 |
|
| 126 |
ginkgo.It("should omit the implied HTTPS port", func() { |
| 127 |
config.Token = "s3cret" |
| 128 |
config.Host = "ha.example.com" |
| 129 |
config.Port = 443 |
| 130 |
|
| 131 |
got := config.GetURL() |
| 132 |
gomega.Expect(got.Host).To(gomega.Equal("ha.example.com")) |
| 133 |
gomega.Expect(got.Port()).To(gomega.BeEmpty()) |
| 134 |
}) |
| 135 |
|
| 136 |
ginkgo.It("should omit the implied HTTP port when TLS is disabled", func() { |
| 137 |
config.Token = "s3cret" |
| 138 |
config.Host = "homeassistant.local" |
| 139 |
config.Port = 8123 |
| 140 |
config.DisableTLS = true |
| 141 |
|
| 142 |
got := config.GetURL() |
| 143 |
gomega.Expect(got.Host).To(gomega.Equal("homeassistant.local")) |
| 144 |
gomega.Expect(got.Port()).To(gomega.BeEmpty()) |
| 145 |
}) |
| 146 |
|
| 147 |
ginkgo.It("should keep a non-default HTTPS port", func() { |
| 148 |
config.Token = "s3cret" |
| 149 |
config.Host = "homeassistant.local" |
| 150 |
config.Port = 8123 |
| 151 |
|
| 152 |
got := config.GetURL() |
| 153 |
gomega.Expect(got.Host).To(gomega.Equal("homeassistant.local:8123")) |
| 154 |
}) |
| 155 |
}) |
| 156 |
|
| 157 |
ginkgo.Describe("domainService", func() { |
| 158 |
ginkgo.It("should default to persistent_notification.create", func() { |
| 159 |
domain, service, err := config.domainService() |
| 160 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 161 |
gomega.Expect(domain).To(gomega.Equal(persistentDomain)) |
| 162 |
gomega.Expect(service).To(gomega.Equal(persistentService)) |
| 163 |
gomega.Expect(config.isPersistent()).To(gomega.BeTrue()) |
| 164 |
}) |
| 165 |
|
| 166 |
ginkgo.It("should map a bare name onto the notify domain", func() { |
| 167 |
config.Service = "mobile_app_phone" |
| 168 |
domain, service, err := config.domainService() |
| 169 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 170 |
gomega.Expect(domain).To(gomega.Equal(notifyDomain)) |
| 171 |
gomega.Expect(service).To(gomega.Equal("mobile_app_phone")) |
| 172 |
gomega.Expect(config.isPersistent()).To(gomega.BeFalse()) |
| 173 |
}) |
| 174 |
|
| 175 |
ginkgo.It("should split a domain.action value", func() { |
| 176 |
config.Service = "notify.mobile_app_phone" |
| 177 |
domain, service, err := config.domainService() |
| 178 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 179 |
gomega.Expect(domain).To(gomega.Equal("notify")) |
| 180 |
gomega.Expect(service).To(gomega.Equal("mobile_app_phone")) |
| 181 |
}) |
| 182 |
}) |
| 183 |
|
| 184 |
ginkgo.Describe("apiURL", func() { |
| 185 |
ginkgo.It("should use HTTPS port 443 when the port is omitted", func() { |
| 186 |
config.Host = "ha.example.com" |
| 187 |
gomega.Expect(config.apiURL(persistentDomain, persistentService)).To( |
| 188 |
gomega.Equal("https://ha.example.com:443/api/services/persistent_notification/create"), |
| 189 |
) |
| 190 |
}) |
| 191 |
|
| 192 |
ginkgo.It("should use HTTP port 8123 when TLS is disabled and the port is omitted", func() { |
| 193 |
config.Host = "homeassistant.local" |
| 194 |
config.DisableTLS = true |
| 195 |
gomega.Expect(config.apiURL(persistentDomain, persistentService)).To( |
| 196 |
gomega.Equal( |
| 197 |
"http://homeassistant.local:8123/api/services/persistent_notification/create", |
| 198 |
), |
| 199 |
) |
| 200 |
}) |
| 201 |
|
| 202 |
ginkgo.It("should include a reverse-proxy prefix", func() { |
| 203 |
config.Host = "ha.example.com" |
| 204 |
config.Path = "/hass" |
| 205 |
gomega.Expect(config.apiURL("notify", "mobile_app_phone")).To( |
| 206 |
gomega.Equal("https://ha.example.com:443/hass/api/services/notify/mobile_app_phone"), |
| 207 |
) |
| 208 |
}) |
| 209 |
}) |
| 210 |
}) |