Debian Codegraph @

testing/e2e/ntfy/basic_test.go

raw symbols SCIP
1 package e2e_test
2
3 import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "net/http"
10 "net/url"
11 "strings"
12 "time"
13
14 "github.com/onsi/ginkgo/v2"
15 "github.com/onsi/gomega"
16
17 "github.com/nicholas-fedor/shoutrrr/pkg/services/push/ntfy"
18 "github.com/nicholas-fedor/shoutrrr/pkg/types"
19 )
20
21 type ntfyMessage struct {
22 ID string `json:"id"`
23 Event string `json:"event"`
24 Topic string `json:"topic"`
25 Message string `json:"message"`
26 Title string `json:"title"`
27 Priority int `json:"priority"`
28 Tags []string `json:"tags"`
29 ContentType string `json:"content_type"`
30 }
31
32 const defaultMessageTimeout = 5 * time.Second
33
34 var _ = ginkgo.Describe("ntfy E2E Basic Tests", func() {
35 ginkgo.When("running e2e tests against a real ntfy server", func() {
36 ginkgo.BeforeEach(func() {
37 if !isNtfyServerAvailable() {
38 ginkgo.Skip("ntfy server not available, skipping e2e tests")
39 }
40 })
41
42 ginkgo.It("should send basic text content", func() {
43 serviceURLStr := buildServiceURL()
44 if serviceURLStr == "" {
45 ginkgo.Skip("ntfy server not configured, skipping basic text test")
46 }
47
48 service := initializeService(serviceURLStr)
49
50 message := "E2E Test: Basic text content notification"
51
52 gomega.Expect(service.Send(message, nil)).NotTo(gomega.HaveOccurred())
53
54 verifyMessageReceived(service.Config.Topic, message)
55 })
56
57 ginkgo.It("should send an empty message", func() {
58 serviceURLStr := buildServiceURL()
59 if serviceURLStr == "" {
60 ginkgo.Skip("ntfy server not configured, skipping empty message test")
61 }
62
63 service := initializeService(serviceURLStr)
64
65 gomega.Expect(service.Send("", nil)).NotTo(gomega.HaveOccurred())
66
67 ginkgo.GinkgoWriter.Write([]byte("Empty message sent successfully\n"))
68 })
69
70 ginkgo.It("should send a message with a title via params", func() {
71 serviceURLStr := buildServiceURL()
72 if serviceURLStr == "" {
73 ginkgo.Skip("ntfy server not configured, skipping title test")
74 }
75
76 service := initializeService(serviceURLStr)
77
78 topic := service.Config.Topic
79 message := "E2E Test: Message with title"
80 expectedTitle := "Test Title"
81
82 gomega.Expect(service.Send(message, &types.Params{
83 "title": expectedTitle,
84 })).NotTo(gomega.HaveOccurred())
85
86 verifyMessageReceivedWithTitle(topic, message, expectedTitle)
87 })
88
89 ginkgo.It("should send a message with priority via params", func() {
90 serviceURLStr := buildServiceURL()
91 if serviceURLStr == "" {
92 ginkgo.Skip("ntfy server not configured, skipping priority test")
93 }
94
95 service := initializeService(serviceURLStr)
96
97 topic := service.Config.Topic
98 message := "E2E Test: High priority message"
99
100 gomega.Expect(service.Send(message, &types.Params{
101 "priority": "5",
102 })).NotTo(gomega.HaveOccurred())
103
104 verifyMessageReceivedWithPriority(topic, message, 5)
105 })
106
107 ginkgo.It("should send a message with tags", func() {
108 serviceURLStr := buildServiceURL()
109 if serviceURLStr == "" {
110 ginkgo.Skip("ntfy server not configured, skipping tags test")
111 }
112
113 service := initializeService(serviceURLStr)
114
115 topic := service.Config.Topic
116 message := "E2E Test: Tagged message"
117
118 gomega.Expect(service.Send(message, &types.Params{
119 "tags": "warning,skull",
120 })).NotTo(gomega.HaveOccurred())
121
122 verifyMessageReceivedWithTags(topic, message, []string{"warning", "skull"})
123 })
124
125 ginkgo.It("should send a message with markdown", func() {
126 serviceURLStr := buildServiceURL()
127 if serviceURLStr == "" {
128 ginkgo.Skip("ntfy server not configured, skipping markdown test")
129 }
130
131 serviceURLStr += "&markdown=yes"
132 service := initializeService(serviceURLStr)
133
134 topic := service.Config.Topic
135 message := "E2E Test: **bold** and *italic* markdown"
136
137 gomega.Expect(service.Send(message, nil)).NotTo(gomega.HaveOccurred())
138
139 msg, err := pollForMessage(topic, message)
140 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "message should be received by ntfy server")
141 gomega.Expect(msg).NotTo(gomega.BeNil())
142 gomega.Expect(msg.Event).To(gomega.Equal("message"))
143 gomega.Expect(msg.Topic).To(gomega.ContainSubstring(topic))
144 gomega.Expect(msg.ContentType).To(gomega.Equal("text/markdown"), "received event should reflect Markdown Content-Type")
145 })
146
147 ginkgo.It("should send a message with special characters and unicode", func() {
148 serviceURLStr := buildServiceURL()
149 if serviceURLStr == "" {
150 ginkgo.Skip("ntfy server not configured, skipping unicode test")
151 }
152
153 service := initializeService(serviceURLStr)
154
155 topic := service.Config.Topic
156 message := "E2E Test: Special chars <>@#$% and unicode 世界 🌍"
157
158 gomega.Expect(service.Send(message, nil)).NotTo(gomega.HaveOccurred())
159
160 verifyMessageReceived(topic, message)
161 })
162 })
163
164 ginkgo.When("no server is configured", func() {
165 ginkgo.It("should return the correct service ID", func() {
166 service := &ntfy.Service{}
167 gomega.Expect(service.GetID()).To(gomega.Equal("ntfy"))
168 })
169 })
170 })
171
172 func pollForMessage(topic, expectedMessage string) (*ntfyMessage, error) {
173 baseURL := getNtfyBaseURL()
174 apiURL := fmt.Sprintf("%s/%s/json?poll=1&message=%s", baseURL, topic, url.QueryEscape(expectedMessage))
175
176 ctx, cancel := context.WithTimeout(context.Background(), defaultMessageTimeout)
177 defer cancel()
178
179 req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody)
180 if err != nil {
181 return nil, fmt.Errorf("creating request: %w", err)
182 }
183
184 client := &http.Client{Timeout: defaultMessageTimeout}
185
186 resp, err := client.Do(req)
187 if err != nil {
188 return nil, fmt.Errorf("making request: %w", err)
189 }
190
191 defer func() { _ = resp.Body.Close() }()
192
193 if resp.StatusCode != http.StatusOK {
194 return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
195 }
196
197 body, err := io.ReadAll(resp.Body)
198 if err != nil {
199 return nil, fmt.Errorf("reading response: %w", err)
200 }
201
202 lines := strings.SplitSeq(strings.TrimSpace(string(body)), "\n")
203 for line := range lines {
204 if line == "" {
205 continue
206 }
207
208 var msg ntfyMessage
209 if err := json.Unmarshal([]byte(line), &msg); err != nil {
210 continue
211 }
212
213 if msg.Message == expectedMessage {
214 return &msg, nil
215 }
216 }
217
218 return nil, errors.New("message not found in response")
219 }
220
221 func verifyMessageReceived(topic, message string) {
222 msg, err := pollForMessage(topic, message)
223 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "message should be received by ntfy server")
224 gomega.Expect(msg).NotTo(gomega.BeNil())
225 gomega.Expect(msg.Event).To(gomega.Equal("message"))
226 gomega.Expect(msg.Topic).To(gomega.ContainSubstring(topic))
227 }
228
229 func verifyMessageReceivedWithTitle(topic, message, expectedTitle string) {
230 msg, err := pollForMessage(topic, message)
231 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "message should be received by ntfy server")
232 gomega.Expect(msg).NotTo(gomega.BeNil())
233 gomega.Expect(msg.Event).To(gomega.Equal("message"))
234 gomega.Expect(msg.Title).To(gomega.Equal(expectedTitle))
235 }
236
237 func verifyMessageReceivedWithPriority(topic, message string, expectedPriority int) {
238 msg, err := pollForMessage(topic, message)
239 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "message should be received by ntfy server")
240 gomega.Expect(msg).NotTo(gomega.BeNil())
241 gomega.Expect(msg.Event).To(gomega.Equal("message"))
242 gomega.Expect(msg.Priority).To(gomega.Equal(expectedPriority))
243 }
244
245 func verifyMessageReceivedWithTags(topic, message string, expectedTags []string) {
246 msg, err := pollForMessage(topic, message)
247 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "message should be received by ntfy server")
248 gomega.Expect(msg).NotTo(gomega.BeNil())
249 gomega.Expect(msg.Event).To(gomega.Equal("message"))
250 gomega.Expect(msg.Tags).To(gomega.ConsistOf(expectedTags))
251 }