Debian Codegraph @

pkg/services/push/bark/bark_test.go

raw
1 package bark_test
2
3 import (
4 "log"
5 "net/http"
6 "net/url"
7 "os"
8 "testing"
9
10 "github.com/jarcoal/httpmock"
11 "github.com/onsi/ginkgo/v2"
12 "github.com/onsi/gomega"
13 "github.com/onsi/gomega/format"
14
15 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
16 "github.com/nicholas-fedor/shoutrrr/pkg/services/push/bark"
17 )
18
19 var (
20 service *bark.Service = &bark.Service{} // Bark service instance for testing
21 envBarkURL *url.URL // Environment-provided URL for integration tests
22 logger *log.Logger = testutils.TestLogger() // Shared logger for tests
23 _ = ginkgo.BeforeSuite(func() {
24 // Load the integration test URL from environment, if available
25 var err error
26
27 envBarkURL, err = url.Parse(os.Getenv("SHOUTRRR_BARK_URL"))
28 if err != nil {
29 envBarkURL = &url.URL{} // Default to empty URL if parsing fails
30 }
31 })
32 )
33
34 var _ = ginkgo.Describe("the bark service", func() {
35 ginkgo.When("running integration tests", func() {
36 ginkgo.It("sends a message successfully with a valid ENV URL", func() {
37 if envBarkURL.String() == "" {
38 ginkgo.Skip("No integration test ENV URL was set")
39
40 return
41 }
42
43 serviceURL := testutils.URLMust(envBarkURL.String())
44 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
45 gomega.Expect(service.Send("This is an integration test message", nil)).
46 To(gomega.Succeed())
47 })
48 })
49
50 ginkgo.Describe("the config", func() {
51 ginkgo.When("getting an API URL", func() {
52 ginkgo.It("constructs the expected URL for various path formats", func() {
53 gomega.Expect(getAPIForPath("path")).To(gomega.Equal("https://host/path/endpoint"))
54 gomega.Expect(getAPIForPath("/path")).To(gomega.Equal("https://host/path/endpoint"))
55 gomega.Expect(getAPIForPath("/path/")).
56 To(gomega.Equal("https://host/path/endpoint"))
57 gomega.Expect(getAPIForPath("path/")).To(gomega.Equal("https://host/path/endpoint"))
58 gomega.Expect(getAPIForPath("/")).To(gomega.Equal("https://host/endpoint"))
59 gomega.Expect(getAPIForPath("")).To(gomega.Equal("https://host/endpoint"))
60 })
61 })
62
63 ginkgo.When("only required fields are set", func() {
64 ginkgo.It("applies default values to optional fields", func() {
65 serviceURL := testutils.URLMust("bark://:devicekey@hostname")
66 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
67 gomega.Expect(*service.Config).To(gomega.Equal(bark.Config{
68 Host: "hostname",
69 DeviceKey: "devicekey",
70 Scheme: "https",
71 }))
72 })
73 })
74
75 ginkgo.When("parsing the configuration URL", func() {
76 ginkgo.It("preserves all fields after de-/serialization", func() {
77 testURL := "bark://:[email protected]:2225/?badge=5&category=CAT&group=GROUP&scheme=http&title=TITLE&url=URL"
78 config := &bark.Config{}
79 gomega.Expect(config.SetURL(testutils.URLMust(testURL))).
80 To(gomega.Succeed(), "verifying")
81 gomega.Expect(config.GetURL().String()).To(gomega.Equal(testURL))
82 })
83 })
84 })
85
86 ginkgo.When("sending the push payload", func() {
87 ginkgo.BeforeEach(func() {
88 httpmock.Activate()
89 })
90 ginkgo.AfterEach(func() {
91 httpmock.DeactivateAndReset()
92 })
93
94 ginkgo.It("sends successfully when the server accepts the payload", func() {
95 serviceURL := testutils.URLMust("bark://:devicekey@hostname")
96 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
97 httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
98 testutils.JSONRespondMust(200, bark.APIResponse{
99 Code: http.StatusOK,
100 Message: "OK",
101 }))
102 gomega.Expect(service.Send("Message", nil)).To(gomega.Succeed())
103 })
104
105 ginkgo.It("reports an error for a server error response", func() {
106 serviceURL := testutils.URLMust("bark://:devicekey@hostname")
107 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
108 httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
109 testutils.JSONRespondMust(500, bark.APIResponse{
110 Code: 500,
111 Message: "someone turned off the internet",
112 }))
113 gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
114 })
115
116 ginkgo.It("handles an unexpected server response gracefully", func() {
117 serviceURL := testutils.URLMust("bark://:devicekey@hostname")
118 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
119 httpmock.RegisterResponder("POST", service.Config.GetAPIURL("push"),
120 testutils.JSONRespondMust(200, bark.APIResponse{
121 Code: 500,
122 Message: "For some reason, the response code and HTTP code is different?",
123 }))
124 gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
125 })
126
127 ginkgo.It("handles communication errors without panicking", func() {
128 httpmock.DeactivateAndReset() // Ensure no mocks interfere
129
130 serviceURL := testutils.URLMust("bark://:devicekey@nonresolvablehostname")
131 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
132 gomega.Expect(service.Send("Message", nil)).To(gomega.HaveOccurred())
133 })
134 })
135
136 ginkgo.Describe("the basic service API", func() {
137 ginkgo.Describe("the service config", func() {
138 ginkgo.It("implements basic service config API methods correctly", func() {
139 testutils.TestConfigGetInvalidQueryValue(&bark.Config{})
140 testutils.TestConfigSetInvalidQueryValue(
141 &bark.Config{},
142 "bark://:mock-device@host/?foo=bar",
143 )
144 testutils.TestConfigSetDefaultValues(&bark.Config{})
145 testutils.TestConfigGetEnumsCount(&bark.Config{}, 0)
146 testutils.TestConfigGetFieldsCount(&bark.Config{}, 9)
147 })
148 })
149
150 ginkgo.Describe("the service instance", func() {
151 ginkgo.BeforeEach(func() {
152 httpmock.Activate()
153 })
154 ginkgo.AfterEach(func() {
155 httpmock.DeactivateAndReset()
156 })
157 ginkgo.It("implements basic service API methods correctly", func() {
158 serviceURL := testutils.URLMust("bark://:devicekey@hostname")
159 gomega.Expect(service.Initialize(serviceURL, logger)).To(gomega.Succeed())
160 testutils.TestServiceSetInvalidParamValue(service, "foo", "bar")
161 })
162 ginkgo.It("returns the correct service identifier", func() {
163 // No initialization needed since GetID is static
164 gomega.Expect(service.GetID()).To(gomega.Equal("bark"))
165 })
166 })
167 })
168 })
169
170 // TestBark runs the Ginkgo test suite for the bark package.
171 func TestBark(t *testing.T) {
172 t.Parallel()
173
174 format.CharactersAroundMismatchToInclude = 20 // Show more context in failure output
175
176 gomega.RegisterFailHandler(ginkgo.Fail)
177 ginkgo.RunSpecs(t, "Shoutrrr Bark Suite")
178 }
179
180 // getAPIForPath is a helper to construct an API URL for testing.
181 func getAPIForPath(path string) string {
182 c := bark.Config{Host: "host", Path: path, Scheme: "https"}
183
184 return c.GetAPIURL("endpoint")
185 }