Debian Codegraph @

server/server_twilio_test.go

raw
1 package server
2
3 import (
4 "io"
5 "net/http"
6 "net/http/httptest"
7 "sync/atomic"
8 "testing"
9 "text/template"
10
11 "github.com/stretchr/testify/require"
12 "heckel.io/ntfy/v2/user"
13 "heckel.io/ntfy/v2/util"
14 )
15
16 func TestServer_Twilio_Call_Add_Verify_Call_Delete_Success(t *testing.T) {
17 forEachBackend(t, func(t *testing.T, databaseURL string) {
18 var called, verified atomic.Bool
19 var code atomic.Pointer[string]
20 twilioVerifyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21 body, err := io.ReadAll(r.Body)
22 require.Nil(t, err)
23 require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
24 if r.URL.Path == "/v2/Services/VA1234567890/Verifications" {
25 if code.Load() != nil {
26 t.Fatal("Should be only called once")
27 }
28 require.Equal(t, "Channel=sms&To=%2B12223334444", string(body))
29 code.Store(util.String("123456"))
30 } else if r.URL.Path == "/v2/Services/VA1234567890/VerificationCheck" {
31 if verified.Load() {
32 t.Fatal("Should be only called once")
33 }
34 require.Equal(t, "Code=123456&To=%2B12223334444", string(body))
35 verified.Store(true)
36 } else {
37 t.Fatal("Unexpected path:", r.URL.Path)
38 }
39 }))
40 defer twilioVerifyServer.Close()
41 twilioCallsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42 if called.Load() {
43 t.Fatal("Should be only called once")
44 }
45 body, err := io.ReadAll(r.Body)
46 require.Nil(t, err)
47 require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path)
48 require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
49 require.Equal(t, "From=%2B1234567890&To=%2B12223334444&Twiml=%0A%3CResponse%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay+loop%3D%223%22%3E%0A%09%09You+have+a+message+from+notify+on+topic+mytopic.+Message%3A%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09hi+there%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09End+of+message.%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09This+message+was+sent+by+user+phil.+It+will+be+repeated+three+times.%0A%09%09To+unsubscribe+from+calls+like+this%2C+remove+your+phone+number+in+the+notify+web+app.%0A%09%09%3Cbreak+time%3D%223s%22%2F%3E%0A%09%3C%2FSay%3E%0A%09%3CSay%3EGoodbye.%3C%2FSay%3E%0A%3C%2FResponse%3E", string(body))
50 called.Store(true)
51 }))
52 defer twilioCallsServer.Close()
53
54 c := newTestConfigWithAuthFile(t, databaseURL)
55 c.TwilioVerifyBaseURL = twilioVerifyServer.URL
56 c.TwilioCallsBaseURL = twilioCallsServer.URL
57 c.TwilioAccount = "AC1234567890"
58 c.TwilioAuthToken = "AAEAA1234567890"
59 c.TwilioPhoneNumber = "+1234567890"
60 c.TwilioVerifyService = "VA1234567890"
61 s := newTestServer(t, c)
62
63 // Add tier and user
64 require.Nil(t, s.userManager.AddTier(&user.Tier{
65 Code: "pro",
66 MessageLimit: 10,
67 CallLimit: 1,
68 }))
69 require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
70 require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
71 u, err := s.userManager.User("phil")
72 require.Nil(t, err)
73
74 // Send verification code for phone number
75 response := request(t, s, "PUT", "/v1/account/phone/verify", `{"number":"+12223334444","channel":"sms"}`, map[string]string{
76 "authorization": util.BasicAuth("phil", "phil"),
77 })
78 require.Equal(t, 200, response.Code)
79 waitFor(t, func() bool {
80 return *code.Load() == "123456"
81 })
82
83 // Add phone number with code
84 response = request(t, s, "PUT", "/v1/account/phone", `{"number":"+12223334444","code":"123456"}`, map[string]string{
85 "authorization": util.BasicAuth("phil", "phil"),
86 })
87 require.Equal(t, 200, response.Code)
88 waitFor(t, func() bool {
89 return verified.Load()
90 })
91 phoneNumbers, err := s.userManager.PhoneNumbers(u.ID)
92 require.Nil(t, err)
93 require.Equal(t, 1, len(phoneNumbers))
94 require.Equal(t, "+12223334444", phoneNumbers[0])
95
96 // Do the thing
97 response = request(t, s, "POST", "/mytopic", "hi there", map[string]string{
98 "authorization": util.BasicAuth("phil", "phil"),
99 "x-call": "yes",
100 })
101 require.Equal(t, "hi there", toMessage(t, response.Body.String()).Message)
102 waitFor(t, func() bool {
103 return called.Load()
104 })
105
106 // Remove the phone number
107 response = request(t, s, "DELETE", "/v1/account/phone", `{"number":"+12223334444"}`, map[string]string{
108 "authorization": util.BasicAuth("phil", "phil"),
109 })
110 require.Equal(t, 200, response.Code)
111
112 // Verify the phone number is gone from the DB
113 phoneNumbers, err = s.userManager.PhoneNumbers(u.ID)
114 require.Nil(t, err)
115 require.Equal(t, 0, len(phoneNumbers))
116 })
117 }
118
119 func TestServer_Twilio_Call_Success(t *testing.T) {
120 forEachBackend(t, func(t *testing.T, databaseURL string) {
121 var called atomic.Bool
122 twilioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
123 if called.Load() {
124 t.Fatal("Should be only called once")
125 }
126 body, err := io.ReadAll(r.Body)
127 require.Nil(t, err)
128 require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path)
129 require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
130 require.Equal(t, "From=%2B1234567890&To=%2B11122233344&Twiml=%0A%3CResponse%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay+loop%3D%223%22%3E%0A%09%09You+have+a+message+from+notify+on+topic+mytopic.+Message%3A%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09hi+there%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09End+of+message.%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09This+message+was+sent+by+user+phil.+It+will+be+repeated+three+times.%0A%09%09To+unsubscribe+from+calls+like+this%2C+remove+your+phone+number+in+the+notify+web+app.%0A%09%09%3Cbreak+time%3D%223s%22%2F%3E%0A%09%3C%2FSay%3E%0A%09%3CSay%3EGoodbye.%3C%2FSay%3E%0A%3C%2FResponse%3E", string(body))
131 called.Store(true)
132 }))
133 defer twilioServer.Close()
134
135 c := newTestConfigWithAuthFile(t, databaseURL)
136 c.TwilioCallsBaseURL = twilioServer.URL
137 c.TwilioAccount = "AC1234567890"
138 c.TwilioAuthToken = "AAEAA1234567890"
139 c.TwilioPhoneNumber = "+1234567890"
140 s := newTestServer(t, c)
141
142 // Add tier and user
143 require.Nil(t, s.userManager.AddTier(&user.Tier{
144 Code: "pro",
145 MessageLimit: 10,
146 CallLimit: 1,
147 }))
148 require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
149 require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
150 u, err := s.userManager.User("phil")
151 require.Nil(t, err)
152 require.Nil(t, s.userManager.AddPhoneNumber(u.ID, "+11122233344"))
153
154 // Do the thing
155 response := request(t, s, "POST", "/mytopic", "hi there", map[string]string{
156 "authorization": util.BasicAuth("phil", "phil"),
157 "x-call": "+11122233344",
158 })
159 require.Equal(t, "hi there", toMessage(t, response.Body.String()).Message)
160 waitFor(t, func() bool {
161 return called.Load()
162 })
163 })
164 }
165
166 func TestServer_Twilio_Call_Success_With_Yes(t *testing.T) {
167 forEachBackend(t, func(t *testing.T, databaseURL string) {
168 var called atomic.Bool
169 twilioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
170 if called.Load() {
171 t.Fatal("Should be only called once")
172 }
173 body, err := io.ReadAll(r.Body)
174 require.Nil(t, err)
175 require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path)
176 require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
177 require.Equal(t, "From=%2B1234567890&To=%2B11122233344&Twiml=%0A%3CResponse%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay+loop%3D%223%22%3E%0A%09%09You+have+a+message+from+notify+on+topic+mytopic.+Message%3A%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09hi+there%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09End+of+message.%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09This+message+was+sent+by+user+phil.+It+will+be+repeated+three+times.%0A%09%09To+unsubscribe+from+calls+like+this%2C+remove+your+phone+number+in+the+notify+web+app.%0A%09%09%3Cbreak+time%3D%223s%22%2F%3E%0A%09%3C%2FSay%3E%0A%09%3CSay%3EGoodbye.%3C%2FSay%3E%0A%3C%2FResponse%3E", string(body))
178 called.Store(true)
179 }))
180 defer twilioServer.Close()
181
182 c := newTestConfigWithAuthFile(t, databaseURL)
183 c.TwilioCallsBaseURL = twilioServer.URL
184 c.TwilioAccount = "AC1234567890"
185 c.TwilioAuthToken = "AAEAA1234567890"
186 c.TwilioPhoneNumber = "+1234567890"
187 s := newTestServer(t, c)
188
189 // Add tier and user
190 require.Nil(t, s.userManager.AddTier(&user.Tier{
191 Code: "pro",
192 MessageLimit: 10,
193 CallLimit: 1,
194 }))
195 require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
196 require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
197 u, err := s.userManager.User("phil")
198 require.Nil(t, err)
199 require.Nil(t, s.userManager.AddPhoneNumber(u.ID, "+11122233344"))
200
201 // Do the thing
202 response := request(t, s, "POST", "/mytopic", "hi there", map[string]string{
203 "authorization": util.BasicAuth("phil", "phil"),
204 "x-call": "yes", // <<<------
205 })
206 require.Equal(t, "hi there", toMessage(t, response.Body.String()).Message)
207 waitFor(t, func() bool {
208 return called.Load()
209 })
210 })
211 }
212
213 func TestServer_Twilio_Call_Success_with_custom_twiml(t *testing.T) {
214 forEachBackend(t, func(t *testing.T, databaseURL string) {
215 var called atomic.Bool
216 twilioServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
217 if called.Load() {
218 t.Fatal("Should be only called once")
219 }
220 body, err := io.ReadAll(r.Body)
221 require.Nil(t, err)
222 require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path)
223 require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization"))
224 require.Equal(t, "From=%2B1234567890&To=%2B11122233344&Twiml=%0A%3CResponse%3E%0A%09%3CPause+length%3D%221%22%2F%3E%0A%09%3CSay+language%3D%22de-DE%22+loop%3D%223%22%3E%0A%09%09Du+hast+eine+Nachricht+von+notify+im+Thema+mytopic.+Nachricht%3A%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09hi+there%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09Ende+der+Nachricht.%0A%09%09%3Cbreak+time%3D%221s%22%2F%3E%0A%09%09Diese+Nachricht+wurde+von+Benutzer+phil+gesendet.+Sie+wird+drei+Mal+wiederholt.%0A%09%09Um+dich+von+Anrufen+wie+diesen+abzumelden%2C+entferne+deine+Telefonnummer+in+der+notify+web+app.%0A%09%09%3Cbreak+time%3D%223s%22%2F%3E%0A%09%3C%2FSay%3E%0A%09%3CSay+language%3D%22de-DE%22%3EAuf+Wiederh%C3%B6ren.%3C%2FSay%3E%0A%3C%2FResponse%3E", string(body))
225 called.Store(true)
226 }))
227 defer twilioServer.Close()
228
229 c := newTestConfigWithAuthFile(t, databaseURL)
230 c.TwilioCallsBaseURL = twilioServer.URL
231 c.TwilioAccount = "AC1234567890"
232 c.TwilioAuthToken = "AAEAA1234567890"
233 c.TwilioPhoneNumber = "+1234567890"
234 c.TwilioCallFormat = template.Must(template.New("twiml").Parse(`
235 <Response>
236 <Pause length="1"/>
237 <Say language="de-DE" loop="3">
238 Du hast eine Nachricht von notify im Thema {{.Topic}}. Nachricht:
239 <break time="1s"/>
240 {{.Message}}
241 <break time="1s"/>
242 Ende der Nachricht.
243 <break time="1s"/>
244 Diese Nachricht wurde von Benutzer {{.Sender}} gesendet. Sie wird drei Mal wiederholt.
245 Um dich von Anrufen wie diesen abzumelden, entferne deine Telefonnummer in der notify web app.
246 <break time="3s"/>
247 </Say>
248 <Say language="de-DE">Auf Wiederhören.</Say>
249 </Response>`))
250 s := newTestServer(t, c)
251
252 // Add tier and user
253 require.Nil(t, s.userManager.AddTier(&user.Tier{
254 Code: "pro",
255 MessageLimit: 10,
256 CallLimit: 1,
257 }))
258 require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
259 require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
260 u, err := s.userManager.User("phil")
261 require.Nil(t, err)
262 require.Nil(t, s.userManager.AddPhoneNumber(u.ID, "+11122233344"))
263
264 // Do the thing
265 response := request(t, s, "POST", "/mytopic", "hi there", map[string]string{
266 "authorization": util.BasicAuth("phil", "phil"),
267 "x-call": "+11122233344",
268 })
269 require.Equal(t, "hi there", toMessage(t, response.Body.String()).Message)
270 waitFor(t, func() bool {
271 return called.Load()
272 })
273 })
274 }
275
276 func TestServer_Twilio_Call_UnverifiedNumber(t *testing.T) {
277 forEachBackend(t, func(t *testing.T, databaseURL string) {
278 c := newTestConfigWithAuthFile(t, databaseURL)
279 c.TwilioCallsBaseURL = "http://dummy.invalid"
280 c.TwilioAccount = "AC1234567890"
281 c.TwilioAuthToken = "AAEAA1234567890"
282 c.TwilioPhoneNumber = "+1234567890"
283 s := newTestServer(t, c)
284
285 // Add tier and user
286 require.Nil(t, s.userManager.AddTier(&user.Tier{
287 Code: "pro",
288 MessageLimit: 10,
289 CallLimit: 1,
290 }))
291 require.Nil(t, s.userManager.AddUser("phil", "phil", user.RoleUser, false))
292 require.Nil(t, s.userManager.ChangeTier("phil", "pro"))
293
294 // Do the thing
295 response := request(t, s, "POST", "/mytopic", "test", map[string]string{
296 "authorization": util.BasicAuth("phil", "phil"),
297 "x-call": "+11122233344",
298 })
299 require.Equal(t, 40034, toHTTPError(t, response.Body.String()).Code)
300 })
301 }
302
303 func TestServer_Twilio_Call_InvalidNumber(t *testing.T) {
304 forEachBackend(t, func(t *testing.T, databaseURL string) {
305 c := newTestConfigWithAuthFile(t, databaseURL)
306 c.TwilioCallsBaseURL = "https://127.0.0.1"
307 c.TwilioAccount = "AC1234567890"
308 c.TwilioAuthToken = "AAEAA1234567890"
309 c.TwilioPhoneNumber = "+1234567890"
310 s := newTestServer(t, c)
311
312 response := request(t, s, "POST", "/mytopic", "test", map[string]string{
313 "x-call": "+invalid",
314 })
315 require.Equal(t, 40033, toHTTPError(t, response.Body.String()).Code)
316 })
317 }
318
319 func TestServer_Twilio_Call_Anonymous(t *testing.T) {
320 forEachBackend(t, func(t *testing.T, databaseURL string) {
321 c := newTestConfigWithAuthFile(t, databaseURL)
322 c.TwilioCallsBaseURL = "https://127.0.0.1"
323 c.TwilioAccount = "AC1234567890"
324 c.TwilioAuthToken = "AAEAA1234567890"
325 c.TwilioPhoneNumber = "+1234567890"
326 s := newTestServer(t, c)
327
328 response := request(t, s, "POST", "/mytopic", "test", map[string]string{
329 "x-call": "+123123",
330 })
331 require.Equal(t, 40035, toHTTPError(t, response.Body.String()).Code)
332 })
333 }
334
335 func TestServer_Twilio_Call_Unconfigured(t *testing.T) {
336 forEachBackend(t, func(t *testing.T, databaseURL string) {
337 s := newTestServer(t, newTestConfig(t, databaseURL))
338 response := request(t, s, "POST", "/mytopic", "test", map[string]string{
339 "x-call": "+1234",
340 })
341 require.Equal(t, 40032, toHTTPError(t, response.Body.String()).Code)
342 })
343 }