Debian Codegraph @

testing/e2e/matrix/error_handling_test.go

raw
1 package e2e_test
2
3 import (
4 "context"
5 "errors"
6 "net/url"
7 "strings"
8 "time"
9
10 "github.com/onsi/ginkgo/v2"
11 "github.com/onsi/gomega"
12
13 "github.com/nicholas-fedor/shoutrrr/internal/testutils"
14 "github.com/nicholas-fedor/shoutrrr/pkg/services/chat/matrix"
15 )
16
17 var _ = ginkgo.Describe("Matrix Service E2E Error Handling", func() {
18 // TestE2EContextCancellation tests behavior when operations are canceled via context.
19 ginkgo.Describe("Context Cancellation", func() {
20 ginkgo.It("should handle canceled context gracefully", func() {
21 serviceURL := buildServiceURL()
22 if serviceURL == "" {
23 ginkgo.Skip("Matrix server not configured, skipping test. " +
24 "Set SHOUTRRR_MATRIX_USER and SHOUTRRR_MATRIX_PASSWORD environment variables")
25 }
26
27 parsedURL, err := url.Parse(serviceURL)
28 gomega.Expect(err).NotTo(gomega.HaveOccurred())
29
30 var svc *matrix.Service
31 if sharedService != nil {
32 svc = sharedService
33 } else {
34 svc = &matrix.Service{}
35 err = svc.Initialize(parsedURL, testutils.TestLogger())
36 gomega.Expect(err).NotTo(gomega.HaveOccurred())
37 }
38
39 // Create a canceled context
40 ctx, cancel := context.WithCancel(context.Background())
41 cancel()
42
43 err = svc.SendWithContext(ctx, "Test message with canceled context", nil)
44 gomega.Expect(errors.Is(err, context.Canceled)).To(gomega.BeTrue(),
45 "expected context.Canceled error, got: %v", err)
46 })
47
48 ginkgo.It("should handle deadline exceeded context gracefully", func() {
49 serviceURL := buildServiceURL()
50 if serviceURL == "" {
51 ginkgo.Skip("Matrix server not configured, skipping test. " +
52 "Set SHOUTRRR_MATRIX_USER and SHOUTRRR_MATRIX_PASSWORD environment variables")
53 }
54
55 parsedURL, err := url.Parse(serviceURL)
56 gomega.Expect(err).NotTo(gomega.HaveOccurred())
57
58 var svc *matrix.Service
59 if sharedService != nil {
60 svc = sharedService
61 } else {
62 svc = &matrix.Service{}
63 err = svc.Initialize(parsedURL, testutils.TestLogger())
64 gomega.Expect(err).NotTo(gomega.HaveOccurred())
65 }
66
67 // Create a context with a very short timeout
68 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
69 defer cancel()
70
71 // Wait for the context to expire
72 <-ctx.Done()
73
74 err = svc.SendWithContext(ctx, "Test message with expired deadline", nil)
75 gomega.Expect(errors.Is(err, context.DeadlineExceeded)).To(gomega.BeTrue(),
76 "expected context.DeadlineExceeded error, got: %v", err)
77 })
78 })
79
80 // TestE2EContextTimeout tests behavior when operations timeout.
81 // We test this by using an unreachable room or network conditions.
82 ginkgo.Describe("Context Timeout", func() {
83 ginkgo.It("should timeout when server is unreachable", func() {
84 // Use a URL with a non-existent room that will cause timeout
85 // This tests the internal timeout handling
86 invalidURL := "matrix://user:password@localhost:9999?room=%23nonexistent:invalid&disableTLS=true"
87 parsedURL, err := url.Parse(invalidURL)
88 gomega.Expect(err).NotTo(gomega.HaveOccurred())
89
90 service := &matrix.Service{}
91
92 err = service.Initialize(parsedURL, testutils.TestLogger())
93 if err != nil {
94 // If initialization fails due to unreachable server, that's expected
95 gomega.Expect(err).To(gomega.HaveOccurred())
96
97 return
98 }
99
100 // Send should timeout or fail due to network issues
101 err = service.Send("This should timeout", nil)
102 gomega.Expect(err).To(gomega.HaveOccurred())
103 })
104
105 ginkgo.It("should handle slow server response with timeout", func() {
106 serviceURL := buildServiceURL()
107 if serviceURL == "" {
108 ginkgo.Skip("Matrix server not configured, skipping test")
109 }
110
111 // Parse the service URL
112 parsedURL, err := url.Parse(serviceURL)
113 gomega.Expect(err).NotTo(gomega.HaveOccurred())
114
115 service := &matrix.Service{}
116
117 err = service.Initialize(parsedURL, testutils.TestLogger())
118 if err != nil {
119 ginkgo.Skip("Cannot initialize service, skipping test")
120 }
121
122 // Try to send to a room that doesn't exist
123 // This will cause the server to take time to respond with an error
124 invalidRoomURL := parsedURL
125 q := invalidRoomURL.Query()
126 q.Set("room", "#definitely-does-not-exist-12345:invalid.local")
127 invalidRoomURL.RawQuery = q.Encode()
128
129 // Re-initialize with invalid room
130 service = &matrix.Service{}
131 err = service.Initialize(invalidRoomURL, testutils.TestLogger())
132 gomega.Expect(err).NotTo(gomega.HaveOccurred())
133
134 // Send should eventually timeout or return error
135 err = service.Send("Test timeout handling", nil)
136 gomega.Expect(err).To(gomega.HaveOccurred())
137 })
138 })
139
140 // TestE2ERoomJoinFailure tests behavior when joining a room fails.
141 ginkgo.Describe("Room Join Failure", func() {
142 ginkgo.It("should return error when room doesn't exist", func() {
143 serviceURL := buildServiceURL()
144 if serviceURL == "" {
145 ginkgo.Skip("Matrix server not configured, skipping test")
146 }
147
148 parsedURL, err := url.Parse(serviceURL)
149 gomega.Expect(err).NotTo(gomega.HaveOccurred())
150
151 // Add a non-existent room
152 q := parsedURL.Query()
153 q.Set("room", "#nonexistent-room-that-does-not-exist-12345:invalid.local")
154 parsedURL.RawQuery = q.Encode()
155
156 service := &matrix.Service{}
157 err = service.Initialize(parsedURL, testutils.TestLogger())
158 gomega.Expect(err).NotTo(gomega.HaveOccurred())
159
160 // Try to send - should fail to join room
161 err = service.Send("Test message to non-existent room", nil)
162 gomega.Expect(err).To(gomega.HaveOccurred())
163 // Error should mention the room or joining failure
164 gomega.Expect(err.Error()).To(gomega.SatisfyAny(
165 gomega.ContainSubstring("join"),
166 gomega.ContainSubstring("room"),
167 gomega.ContainSubstring("404"),
168 gomega.ContainSubstring("not found"),
169 ))
170 })
171
172 ginkgo.It("should return error for invalid room alias format", func() {
173 serviceURL := buildServiceURL()
174 if serviceURL == "" {
175 ginkgo.Skip("Matrix server not configured, skipping test")
176 }
177
178 parsedURL, err := url.Parse(serviceURL)
179 gomega.Expect(err).NotTo(gomega.HaveOccurred())
180
181 // Add an invalid room alias (missing colon for server name)
182 q := parsedURL.Query()
183 q.Set("room", "invalid-room-alias")
184 parsedURL.RawQuery = q.Encode()
185
186 service := &matrix.Service{}
187 err = service.Initialize(parsedURL, testutils.TestLogger())
188 gomega.Expect(err).NotTo(gomega.HaveOccurred())
189
190 // Try to send - may fail or succeed depending on server handling
191 err = service.Send("Test message with invalid room alias", nil)
192 // We're just checking it doesn't panic; error is optional
193 _ = err
194 })
195 })
196
197 // TestE2ENetworkError tests behavior when network errors occur.
198 ginkgo.Describe("Network Error Handling", func() {
199 ginkgo.It("should return error for unreachable server", func() {
200 // Use a port that's unlikely to be open
201 invalidURL := "matrix://user:password@localhost:65432?disableTLS=true"
202 parsedURL, err := url.Parse(invalidURL)
203 gomega.Expect(err).NotTo(gomega.HaveOccurred())
204
205 service := &matrix.Service{}
206
207 err = service.Initialize(parsedURL, testutils.TestLogger())
208 if err != nil {
209 // Connection refused is expected (platform-agnostic)
210 gomega.Expect(err.Error()).To(gomega.SatisfyAny(
211 gomega.ContainSubstring("refused"),
212 gomega.ContainSubstring("no such host"),
213 gomega.ContainSubstring("timeout"),
214 gomega.ContainSubstring("i/o timeout"),
215 ))
216
217 return
218 }
219
220 // If initialization succeeded (unlikely), try to send
221 err = service.Send("Test message", nil)
222 gomega.Expect(err).To(gomega.HaveOccurred())
223 })
224
225 ginkgo.It("should handle invalid host gracefully", func() {
226 // Use a clearly invalid host
227 invalidURL := "matrix://user:[email protected]:8008?disableTLS=true"
228 parsedURL, err := url.Parse(invalidURL)
229 gomega.Expect(err).NotTo(gomega.HaveOccurred())
230
231 service := &matrix.Service{}
232 err = service.Initialize(parsedURL, testutils.TestLogger())
233 // Should fail with DNS/network error
234 gomega.Expect(err).To(gomega.HaveOccurred())
235 gomega.Expect(err.Error()).To(gomega.SatisfyAny(
236 gomega.ContainSubstring("no such host"),
237 gomega.ContainSubstring("dial"),
238 gomega.ContainSubstring("timeout"),
239 ))
240 })
241
242 ginkgo.It("should handle invalid credentials with clear error", func() {
243 // Use valid host but invalid credentials
244 serviceURL := buildServiceURL()
245 if serviceURL == "" {
246 // Use localhost as fallback
247 serviceURL = "matrix://invaliduser:invalidpassword@localhost:8008?disableTLS=true"
248 } else {
249 // Replace credentials in existing URL
250 parsedURL, _ := url.Parse(serviceURL)
251 parsedURL.User = url.UserPassword("invaliduser", "invalidpassword")
252 serviceURL = parsedURL.String()
253 }
254
255 parsedURL, err := url.Parse(serviceURL)
256 gomega.Expect(err).NotTo(gomega.HaveOccurred())
257
258 service := &matrix.Service{}
259 err = service.Initialize(parsedURL, testutils.TestLogger())
260 // Authentication should fail
261 gomega.Expect(err).To(gomega.HaveOccurred())
262 })
263
264 ginkgo.It("should handle invalid host gracefully", func() {
265 // Construct a test-specific invalid URL directly
266 invalidURL, err := url.Parse("matrix://user:pass@invalid-host-for-testing:9999?disableTLS=true")
267 gomega.Expect(err).NotTo(gomega.HaveOccurred())
268
269 service := &matrix.Service{}
270 err = service.Initialize(invalidURL, testutils.TestLogger())
271 // Should fail due to invalid host
272 gomega.Expect(err).To(gomega.HaveOccurred())
273 })
274 })
275
276 // Additional edge case tests
277 ginkgo.Describe("Edge Cases", func() {
278 ginkgo.It("should handle empty message gracefully", func() {
279 if sharedService != nil {
280 err := sharedService.Send("", nil)
281 // Empty message should not cause an error
282 gomega.Expect(err).NotTo(gomega.HaveOccurred())
283
284 return
285 }
286
287 serviceURL := buildServiceURL()
288 if serviceURL == "" {
289 ginkgo.Skip("Matrix server not configured, skipping test")
290 }
291
292 parsedURL, err := url.Parse(serviceURL)
293 gomega.Expect(err).NotTo(gomega.HaveOccurred())
294
295 service := &matrix.Service{}
296 err = service.Initialize(parsedURL, testutils.TestLogger())
297 gomega.Expect(err).NotTo(gomega.HaveOccurred())
298
299 err = service.Send("", nil)
300 // Empty message should not cause an error
301 gomega.Expect(err).NotTo(gomega.HaveOccurred())
302 })
303
304 ginkgo.It("should handle very long message", func() {
305 if sharedService != nil {
306 longMessage := strings.Repeat("A very long message. ", 1000)
307 err := sharedService.Send(longMessage, nil)
308 // Long message should be sent successfully
309 gomega.Expect(err).NotTo(gomega.HaveOccurred())
310
311 return
312 }
313
314 serviceURL := buildServiceURL()
315 if serviceURL == "" {
316 ginkgo.Skip("Matrix server not configured, skipping test")
317 }
318
319 parsedURL, err := url.Parse(serviceURL)
320 gomega.Expect(err).NotTo(gomega.HaveOccurred())
321
322 service := &matrix.Service{}
323 err = service.Initialize(parsedURL, testutils.TestLogger())
324 gomega.Expect(err).NotTo(gomega.HaveOccurred())
325
326 longMessage := strings.Repeat("Test message content. ", 500)
327 err = service.Send(longMessage, nil)
328 gomega.Expect(err).NotTo(gomega.HaveOccurred())
329 })
330
331 ginkgo.It("should respect configured timeout", func() {
332 // The default HTTP timeout is 10 seconds
333 // We test that the service doesn't hang indefinitely
334 serviceURL := buildServiceURL()
335 if serviceURL == "" {
336 ginkgo.Skip("Matrix server not configured, skipping test")
337 }
338
339 parsedURL, err := url.Parse(serviceURL)
340 gomega.Expect(err).NotTo(gomega.HaveOccurred())
341
342 service := &matrix.Service{}
343 err = service.Initialize(parsedURL, testutils.TestLogger())
344 gomega.Expect(err).NotTo(gomega.HaveOccurred())
345
346 // Send should complete within reasonable time (not hang indefinitely)
347 done := make(chan error, 1)
348
349 go func() {
350 done <- service.Send("Timeout test message", nil)
351 }()
352
353 select {
354 case err := <-done:
355 // Completed within the timeout - must succeed
356 gomega.Expect(err).NotTo(gomega.HaveOccurred())
357 case <-time.After(15 * time.Second):
358 ginkgo.Fail("Send operation timed out after 15 seconds (expected < 10s)")
359 }
360 })
361 })
362 })