Debian Codegraph @

testing/e2e/zulip/error_handling_test.go

raw
1 package e2e_test
2
3 import (
4 "context"
5 "net/url"
6 "strings"
7 "time"
8
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/services/chat/zulip"
14 "github.com/nicholas-fedor/shoutrrr/pkg/types"
15 )
16
17 var _ = ginkgo.Describe("Zulip E2E Error Handling", func() {
18 ginkgo.When("the server is unreachable", func() {
19 ginkgo.It("should fail gracefully with a connection error", func() {
20 serviceURL, err := url.Parse("zulip://[email protected]:[email protected]:44399")
21 gomega.Expect(err).NotTo(gomega.HaveOccurred())
22
23 service := &zulip.Service{}
24 err = service.Initialize(serviceURL, testutils.TestLogger())
25 gomega.Expect(err).NotTo(gomega.HaveOccurred())
26
27 sendCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
28 defer cancel()
29
30 err = service.SendWithContext(sendCtx, "E2E Test: unreachable", nil)
31 gomega.Expect(err).To(gomega.HaveOccurred())
32 })
33 })
34
35 ginkgo.When("sending with context cancellation", func() {
36 ginkgo.It("should abort when context is already canceled", func() {
37 serviceURL, err := url.Parse("zulip://[email protected]:[email protected]:44399")
38 gomega.Expect(err).NotTo(gomega.HaveOccurred())
39
40 service := &zulip.Service{}
41 err = service.Initialize(serviceURL, testutils.TestLogger())
42 gomega.Expect(err).NotTo(gomega.HaveOccurred())
43
44 ctx, cancel := context.WithCancel(context.Background())
45 cancel()
46
47 err = service.SendWithContext(ctx, "E2E Test: canceled", nil)
48 gomega.Expect(err).To(gomega.HaveOccurred())
49 })
50 })
51
52 ginkgo.When("sending with invalid credentials", func() {
53 ginkgo.It("should fail with authentication error from a valid server", func() {
54 serviceURLStr := buildServiceURL()
55 if serviceURLStr == "" {
56 ginkgo.Skip("Zulip server not configured, skipping auth error test")
57 }
58
59 parsedURL, err := url.Parse(serviceURLStr)
60 gomega.Expect(err).NotTo(gomega.HaveOccurred())
61
62 parsedURL.User = url.UserPassword("[email protected]", "wrong-key-invalid-key")
63
64 service := &zulip.Service{}
65 err = service.Initialize(parsedURL, testutils.TestLogger())
66 gomega.Expect(err).NotTo(gomega.HaveOccurred())
67
68 err = service.Send("E2E Test: invalid credentials", nil)
69 gomega.Expect(err).To(gomega.HaveOccurred())
70 gomega.Expect(err.Error()).To(gomega.ContainSubstring("401"))
71 })
72 })
73
74 ginkgo.When("sending with a non-existent stream", func() {
75 ginkgo.It("should fail with error for invalid stream", func() {
76 serviceURLStr := buildServiceURL()
77 if serviceURLStr == "" {
78 ginkgo.Skip("Zulip server not configured, skipping invalid stream test")
79 }
80
81 serviceURL, err := url.Parse(serviceURLStr)
82 gomega.Expect(err).NotTo(gomega.HaveOccurred())
83
84 service := &zulip.Service{}
85 err = service.Initialize(serviceURL, testutils.TestLogger())
86 gomega.Expect(err).NotTo(gomega.HaveOccurred())
87
88 params := &types.Params{
89 "stream": "this-stream-definitely-does-not-exist-" + strings.Repeat("x", 20),
90 }
91
92 err = service.Send("E2E Test: invalid stream", params)
93 gomega.Expect(err).To(gomega.HaveOccurred())
94 })
95 })
96 })