Debian Codegraph @

pkg/services/chat/xmpp/xmpp_config_test.go

raw
1 package xmpp
2
3 import (
4 "github.com/onsi/ginkgo/v2"
5 "github.com/onsi/gomega"
6
7 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
8 )
9
10 var _ = ginkgo.Describe("config", func() {
11 ginkgo.Describe("SetURL", func() {
12 ginkgo.It("should parse a localpart user, host, and to JID", func() {
13 config := &Config{}
14 err := config.SetURL(testutils.URLMust(
15 "xmpp://alice:[email protected]/[email protected]",
16 ))
17 gomega.Expect(err).NotTo(gomega.HaveOccurred())
18 gomega.Expect(config.User).To(gomega.Equal("alice"))
19 gomega.Expect(config.Password).To(gomega.Equal("secret"))
20 gomega.Expect(config.Host).To(gomega.Equal("xmpp.example.com"))
21 gomega.Expect(config.Port).To(gomega.Equal(DefaultPort))
22 gomega.Expect(config.To).To(gomega.Equal([]string{"[email protected]"}))
23 gomega.Expect(config.scheme).To(gomega.Equal(Scheme))
24 gomega.Expect(config.authJIDString()).To(gomega.Equal("[email protected]"))
25 })
26
27 ginkgo.It("should parse a full auth JID distinct from the dial host", func() {
28 config := &Config{}
29 err := config.SetURL(testutils.URLMust(
30 "xmpp://alice%40jabber.example.com:[email protected]/[email protected]",
31 ))
32 gomega.Expect(err).NotTo(gomega.HaveOccurred())
33 gomega.Expect(config.User).To(gomega.Equal("[email protected]"))
34 gomega.Expect(config.Host).To(gomega.Equal("xmpp.example.com"))
35 gomega.Expect(config.authJIDString()).To(gomega.Equal("[email protected]"))
36 })
37
38 ginkgo.It("should default xmpps to port 5223", func() {
39 config := &Config{}
40 err := config.SetURL(testutils.URLMust(
41 "xmpps://alice:[email protected]/[email protected]",
42 ))
43 gomega.Expect(err).NotTo(gomega.HaveOccurred())
44 gomega.Expect(config.Port).To(gomega.Equal(DefaultTLSPort))
45 gomega.Expect(config.scheme).To(gomega.Equal(SchemeTLS))
46 gomega.Expect(config.implicitTLS()).To(gomega.BeTrue())
47 })
48
49 ginkgo.It("should parse rooms, nick, and roompassword", func() {
50 config := &Config{}
51 err := config.SetURL(testutils.URLMust(
52 "xmpp://alice:[email protected]/" +
53 "[email protected]&nick=bot&roompassword=s3cret",
54 ))
55 gomega.Expect(err).NotTo(gomega.HaveOccurred())
56 gomega.Expect(config.Rooms).To(gomega.Equal([]string{"[email protected]"}))
57 gomega.Expect(config.Nick).To(gomega.Equal("bot"))
58 gomega.Expect(config.RoomPassword).To(gomega.Equal("s3cret"))
59 gomega.Expect(config.mucNick()).To(gomega.Equal("bot"))
60 })
61
62 ginkgo.It("should default nick to the auth JID localpart", func() {
63 config := &Config{}
64 err := config.SetURL(testutils.URLMust(
65 "xmpp://alice:[email protected]/[email protected]",
66 ))
67 gomega.Expect(err).NotTo(gomega.HaveOccurred())
68 gomega.Expect(config.mucNick()).To(gomega.Equal("alice"))
69 })
70
71 ginkgo.It("should reject disabletls on xmpps", func() {
72 config := &Config{}
73 err := config.SetURL(testutils.URLMust(
74 "xmpps://alice:[email protected]/[email protected]&disabletls=yes",
75 ))
76 gomega.Expect(err).To(gomega.MatchError(ErrDisableTLSOnXMPPS))
77 })
78
79 ginkgo.It("should allow disabletls on xmpp", func() {
80 config := &Config{}
81 err := config.SetURL(testutils.URLMust(
82 "xmpp://alice:[email protected]/[email protected]&disabletls=yes",
83 ))
84 gomega.Expect(err).NotTo(gomega.HaveOccurred())
85 gomega.Expect(config.DisableTLS).To(gomega.BeTrue())
86 gomega.Expect(config.useTLS()).To(gomega.BeFalse())
87 })
88
89 ginkgo.It("should reject missing targets", func() {
90 config := &Config{}
91 err := config.SetURL(testutils.URLMust("xmpp://alice:[email protected]/"))
92 gomega.Expect(err).To(gomega.MatchError(ErrMissingTargets))
93 })
94
95 ginkgo.It("should reject missing password", func() {
96 config := &Config{}
97 err := config.SetURL(testutils.URLMust(
98 "xmpp://[email protected]/[email protected]",
99 ))
100 gomega.Expect(err).To(gomega.MatchError(ErrMissingPassword))
101 })
102
103 ginkgo.It("should reject an invalid recipient JID", func() {
104 config := &Config{}
105 err := config.SetURL(testutils.URLMust(
106 "xmpp://alice:[email protected]/?to=not-a-jid",
107 ))
108 gomega.Expect(err).To(gomega.MatchError(ErrInvalidRecipientJID))
109 })
110
111 ginkgo.It("should reject an invalid room JID", func() {
112 config := &Config{}
113 err := config.SetURL(testutils.URLMust(
114 "xmpp://alice:[email protected]/?rooms=not-a-jid",
115 ))
116 gomega.Expect(err).To(gomega.MatchError(ErrInvalidRoomJID))
117 })
118
119 ginkgo.It("should reject an invalid auth JID", func() {
120 config := &Config{}
121 err := config.SetURL(testutils.URLMust(
122 "xmpp://alice%40:[email protected]/[email protected]",
123 ))
124 gomega.Expect(err).To(gomega.MatchError(ErrInvalidUserJID))
125 })
126
127 ginkgo.It("should reject a missing host", func() {
128 config := &Config{}
129 err := config.SetURL(testutils.URLMust(
130 "xmpp://alice:secret@/[email protected]",
131 ))
132 gomega.Expect(err).To(gomega.MatchError(ErrMissingHost))
133 })
134
135 ginkgo.It("should reject a missing user", func() {
136 config := &Config{}
137 err := config.SetURL(testutils.URLMust(
138 "xmpp://:[email protected]/[email protected]",
139 ))
140 gomega.Expect(err).To(gomega.MatchError(ErrMissingUser))
141 })
142
143 ginkgo.It("should default an empty scheme to xmpp", func() {
144 config := &Config{}
145 serviceURL := testutils.URLMust(
146 "xmpp://alice:[email protected]/[email protected]",
147 )
148 serviceURL.Scheme = ""
149 gomega.Expect(config.SetURL(serviceURL)).To(gomega.Succeed())
150 gomega.Expect(config.scheme).To(gomega.Equal(Scheme))
151 })
152
153 ginkgo.It("should reject an unsupported scheme", func() {
154 config := &Config{}
155 err := config.SetURL(testutils.URLMust(
156 "http://alice:[email protected]/[email protected]",
157 ))
158 gomega.Expect(err).To(gomega.MatchError(ErrUnsupportedScheme))
159 })
160
161 ginkgo.It("should reject a port that does not fit in uint16", func() {
162 config := &Config{}
163 err := config.SetURL(testutils.URLMust(
164 "xmpp://alice:[email protected]:70000/[email protected]",
165 ))
166 gomega.Expect(err).To(gomega.HaveOccurred())
167 gomega.Expect(err.Error()).To(gomega.ContainSubstring("parsing port"))
168 })
169
170 ginkgo.It("should reject an unknown query key", func() {
171 config := &Config{}
172 err := config.SetURL(testutils.URLMust(
173 "xmpp://alice:[email protected]/[email protected]&notakey=1",
174 ))
175 gomega.Expect(err).To(gomega.HaveOccurred())
176 gomega.Expect(err.Error()).To(gomega.ContainSubstring("setting query parameter"))
177 })
178
179 ginkgo.It("should parse comma-separated targets and a custom port", func() {
180 config := &Config{}
181 err := config.SetURL(testutils.URLMust(
182 "xmpp://alice:[email protected]:5225/" +
183 "[email protected],[email protected]&[email protected],[email protected]",
184 ))
185 gomega.Expect(err).NotTo(gomega.HaveOccurred())
186 gomega.Expect(config.Port).To(gomega.Equal(uint16(5225)))
187 gomega.Expect(config.To).To(gomega.Equal([]string{"[email protected]", "[email protected]"}))
188 gomega.Expect(config.Rooms).To(gomega.Equal([]string{"[email protected]", "[email protected]"}))
189 })
190
191 ginkgo.It("should accept the dummy docs URL", func() {
192 config := &Config{}
193 err := config.SetURL(testutils.URLMust("xmpp://[email protected]"))
194 gomega.Expect(err).NotTo(gomega.HaveOccurred())
195 })
196
197 ginkgo.It("should accept the dummy xmpps docs URL", func() {
198 config := &Config{}
199 err := config.SetURL(testutils.URLMust("xmpps://[email protected]"))
200 gomega.Expect(err).NotTo(gomega.HaveOccurred())
201 gomega.Expect(config.scheme).To(gomega.Equal(SchemeTLS))
202 gomega.Expect(config.Port).To(gomega.Equal(DefaultTLSPort))
203 })
204
205 ginkgo.It("should not keep omitted fields from a previous SetURL", func() {
206 config := &Config{}
207 gomega.Expect(config.SetURL(testutils.URLMust(
208 "xmpp://alice:[email protected]/" +
209 "[email protected]&[email protected]&nick=bot",
210 ))).To(gomega.Succeed())
211
212 gomega.Expect(config.SetURL(testutils.URLMust(
213 "xmpp://carol:[email protected]/[email protected]",
214 ))).To(gomega.Succeed())
215 gomega.Expect(config.User).To(gomega.Equal("carol"))
216 gomega.Expect(config.Password).To(gomega.Equal("other"))
217 gomega.Expect(config.To).To(gomega.Equal([]string{"[email protected]"}))
218 gomega.Expect(config.Rooms).To(gomega.BeEmpty())
219 gomega.Expect(config.Nick).To(gomega.BeEmpty())
220 })
221
222 ginkgo.It("should leave the receiver unchanged when SetURL fails", func() {
223 config := &Config{}
224 gomega.Expect(config.SetURL(testutils.URLMust(
225 "xmpp://alice:[email protected]/[email protected]",
226 ))).To(gomega.Succeed())
227
228 err := config.SetURL(testutils.URLMust("xmpp://alice:[email protected]/"))
229 gomega.Expect(err).To(gomega.MatchError(ErrMissingTargets))
230 gomega.Expect(config.Password).To(gomega.Equal("secret"))
231 gomega.Expect(config.To).To(gomega.Equal([]string{"[email protected]"}))
232 })
233 })
234
235 ginkgo.Describe("GetURL", func() {
236 ginkgo.It("should round-trip a localpart URL", func() {
237 original := testutils.URLMust(
238 "xmpp://alice:[email protected]:5222/[email protected]",
239 )
240 config := &Config{}
241 gomega.Expect(config.SetURL(original)).To(gomega.Succeed())
242
243 parsed := &Config{}
244 gomega.Expect(parsed.SetURL(config.GetURL())).To(gomega.Succeed())
245 gomega.Expect(parsed.User).To(gomega.Equal(config.User))
246 gomega.Expect(parsed.Password).To(gomega.Equal(config.Password))
247 gomega.Expect(parsed.Host).To(gomega.Equal(config.Host))
248 gomega.Expect(parsed.Port).To(gomega.Equal(config.Port))
249 gomega.Expect(parsed.To).To(gomega.Equal(config.To))
250 gomega.Expect(parsed.scheme).To(gomega.Equal(Scheme))
251 })
252
253 ginkgo.It("should preserve the xmpps scheme", func() {
254 config := &Config{}
255 gomega.Expect(config.SetURL(testutils.URLMust(
256 "xmpps://alice:[email protected]/[email protected]",
257 ))).To(gomega.Succeed())
258 gomega.Expect(config.GetURL().Scheme).To(gomega.Equal(SchemeTLS))
259 })
260
261 ginkgo.It("should default scheme and port when they are unset", func() {
262 config := &Config{Host: "xmpp.example.com", User: "alice", Password: "secret"}
263 parsed := config.GetURL()
264 gomega.Expect(parsed.Scheme).To(gomega.Equal(Scheme))
265 gomega.Expect(parsed.Port()).To(gomega.Equal("5222"))
266 gomega.Expect(parsed.User.Username()).To(gomega.Equal("alice"))
267 })
268
269 ginkgo.It("should omit userinfo when the user is empty", func() {
270 config := &Config{Host: "xmpp.example.com", scheme: Scheme, Port: DefaultPort}
271 gomega.Expect(config.GetURL().User).To(gomega.BeNil())
272 })
273 })
274
275 ginkgo.Describe("mucNick", func() {
276 ginkgo.It("should fall back to the resource name when the auth JID is invalid", func() {
277 config := &Config{User: "alice"}
278 gomega.Expect(config.mucNick()).To(gomega.Equal(Resource))
279 })
280 })
281
282 ginkgo.Describe("splitJID", func() {
283 ginkgo.It("should strip a resource and reject empty or nested domains", func() {
284 local, domain, err := splitJID("[email protected]/phone")
285 gomega.Expect(err).NotTo(gomega.HaveOccurred())
286 gomega.Expect(local).To(gomega.Equal("alice"))
287 gomega.Expect(domain).To(gomega.Equal("jabber.example.com"))
288
289 _, _, err = splitJID("alice@")
290 gomega.Expect(err).To(gomega.MatchError(ErrInvalidUserJID))
291
292 _, _, err = splitJID("alice@[email protected]")
293 gomega.Expect(err).To(gomega.MatchError(ErrInvalidUserJID))
294 })
295 })
296
297 ginkgo.Describe("composeMessage", func() {
298 ginkgo.It("should return the body when title is empty", func() {
299 gomega.Expect(composeMessage("", "hello")).To(gomega.Equal("hello"))
300 })
301
302 ginkgo.It("should prepend the title", func() {
303 gomega.Expect(composeMessage("Alert", "hello")).To(gomega.Equal("Alert\nhello"))
304 })
305
306 ginkgo.It("should return the title when the body is empty", func() {
307 gomega.Expect(composeMessage("Alert", "")).To(gomega.Equal("Alert"))
308 })
309 })
310 })