Debian Codegraph @

pkg/services/push/join/join_test.go

raw
1 package join_test
2
3 import (
4 "net/url"
5 "os"
6 "testing"
7
8 "github.com/jarcoal/httpmock"
9 "github.com/onsi/ginkgo/v2"
10 "github.com/onsi/gomega"
11
12 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
13 "github.com/nicholas-fedor/shoutrrr/pkg/format"
14 "github.com/nicholas-fedor/shoutrrr/pkg/services/push/join"
15 )
16
17 var (
18 service *join.Service
19 config *join.Config
20 pkr format.PropKeyResolver
21 envJoinURL *url.URL
22 _ = ginkgo.BeforeSuite(func() {
23 service = &join.Service{}
24 envJoinURL, _ = url.Parse(os.Getenv("SHOUTRRR_JOIN_URL"))
25 })
26 )
27
28 var _ = ginkgo.Describe("the join service", func() {
29 ginkgo.When("running integration tests", func() {
30 ginkgo.It("should work", func() {
31 if envJoinURL.String() == "" {
32 return
33 }
34
35 serviceURL, _ := url.Parse(envJoinURL.String())
36 err := service.Initialize(serviceURL, testutils.TestLogger())
37 gomega.Expect(err).NotTo(gomega.HaveOccurred())
38 err = service.Send("this is an integration test", nil)
39 gomega.Expect(err).NotTo(gomega.HaveOccurred())
40 })
41 ginkgo.It("returns the correct service identifier", func() {
42 gomega.Expect(service.GetID()).To(gomega.Equal("join"))
43 })
44 })
45 })
46
47 var _ = ginkgo.Describe("the join config", func() {
48 ginkgo.BeforeEach(func() {
49 config = &join.Config{}
50 pkr = format.NewPropKeyResolver(config)
51 })
52 ginkgo.When("updating it using an url", func() {
53 ginkgo.It("should update the API key using the password part of the url", func() {
54 url := createURL("dummy", "TestToken", "testDevice")
55 err := config.SetURL(url)
56 gomega.Expect(err).NotTo(gomega.HaveOccurred())
57 gomega.Expect(config.APIKey).To(gomega.Equal("TestToken"))
58 })
59 ginkgo.It("should error if supplied with an empty token", func() {
60 url := createURL("user", "", "testDevice")
61 expectErrorGivenURL(join.ErrAPIKeyMissing, url)
62 })
63 })
64 ginkgo.When("getting the current config", func() {
65 ginkgo.It("should return the config that is currently set as an url", func() {
66 config.APIKey = "test-token"
67
68 url := config.GetURL()
69 password, _ := url.User.Password()
70 gomega.Expect(password).To(gomega.Equal(config.APIKey))
71 gomega.Expect(url.Scheme).To(gomega.Equal("join"))
72 })
73 })
74 ginkgo.When("setting a config key", func() {
75 ginkgo.It("should split it by commas if the key is devices", func() {
76 err := pkr.Set("devices", "a,b,c,d")
77 gomega.Expect(err).NotTo(gomega.HaveOccurred())
78 gomega.Expect(config.Devices).To(gomega.Equal([]string{"a", "b", "c", "d"}))
79 })
80 ginkgo.It("should update icon when an icon is supplied", func() {
81 err := pkr.Set("icon", "https://example.com/icon.png")
82 gomega.Expect(err).NotTo(gomega.HaveOccurred())
83 gomega.Expect(config.Icon).To(gomega.Equal("https://example.com/icon.png"))
84 })
85 ginkgo.It("should update the title when it is supplied", func() {
86 err := pkr.Set("title", "new title")
87 gomega.Expect(err).NotTo(gomega.HaveOccurred())
88 gomega.Expect(config.Title).To(gomega.Equal("new title"))
89 })
90 ginkgo.It("should return an error if the key is not recognized", func() {
91 err := pkr.Set("devicey", "a,b,c,d")
92 gomega.Expect(err).To(gomega.HaveOccurred())
93 })
94 })
95 ginkgo.When("getting a config key", func() {
96 ginkgo.It("should join it with commas if the key is devices", func() {
97 config.Devices = []string{"a", "b", "c"}
98 value, err := pkr.Get("devices")
99 gomega.Expect(err).NotTo(gomega.HaveOccurred())
100 gomega.Expect(value).To(gomega.Equal("a,b,c"))
101 })
102 ginkgo.It("should return an error if the key is not recognized", func() {
103 _, err := pkr.Get("devicey")
104 gomega.Expect(err).To(gomega.HaveOccurred())
105 })
106 })
107
108 ginkgo.When("listing the query fields", func() {
109 ginkgo.It(
110 "should return the keys \"devices\", \"icon\", \"title\" in alphabetical order",
111 func() {
112 fields := pkr.QueryFields()
113 gomega.Expect(fields).To(gomega.Equal([]string{"devices", "icon", "title"}))
114 },
115 )
116 })
117
118 ginkgo.When("parsing the configuration URL", func() {
119 ginkgo.It("should be identical after de-/serialization", func() {
120 input := "join://Token:apikey@join?devices=dev1%2Cdev2&icon=warning&title=hey"
121 config := &join.Config{}
122 gomega.Expect(config.SetURL(testutils.URLMust(input))).To(gomega.Succeed())
123 gomega.Expect(config.GetURL().String()).To(gomega.Equal(input))
124 })
125 })
126
127 ginkgo.Describe("sending the payload", func() {
128 var err error
129
130 ginkgo.BeforeEach(func() {
131 httpmock.Activate()
132 })
133 ginkgo.AfterEach(func() {
134 httpmock.DeactivateAndReset()
135 })
136 ginkgo.It("should not report an error if the server accepts the payload", func() {
137 config := join.Config{
138 APIKey: "apikey",
139 Devices: []string{"dev1"},
140 }
141 serviceURL := config.GetURL()
142 service := join.Service{}
143 err = service.Initialize(serviceURL, nil)
144 gomega.Expect(err).NotTo(gomega.HaveOccurred())
145
146 httpmock.RegisterResponder(
147 "POST",
148 "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush",
149 httpmock.NewStringResponder(200, ``),
150 )
151
152 err = service.Send("Message", nil)
153 gomega.Expect(err).NotTo(gomega.HaveOccurred())
154 })
155 })
156 })
157
158 func TestJoin(t *testing.T) {
159 t.Parallel()
160 gomega.RegisterFailHandler(ginkgo.Fail)
161 ginkgo.RunSpecs(t, "Join Suite")
162 }
163
164 func createURL(username, token, devices string) *url.URL {
165 return &url.URL{
166 User: url.UserPassword("Token", token),
167 Host: username,
168 RawQuery: "devices=" + devices,
169 }
170 }
171
172 func expectErrorGivenURL(expectedErr error, serviceURL *url.URL) {
173 err := config.SetURL(serviceURL)
174 gomega.Expect(err).To(gomega.HaveOccurred())
175 gomega.Expect(err.Error()).To(gomega.Equal(expectedErr.Error()))
176 }