| 1 |
package twilio |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"net/url" |
| 9 |
"testing" |
| 10 |
"text/template" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestClient_Call_Success(t *testing.T) { |
| 16 |
var body string |
| 17 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 |
require.Equal(t, "/2010-04-01/Accounts/AC1234567890/Calls.json", r.URL.Path) |
| 19 |
require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization")) |
| 20 |
require.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) |
| 21 |
require.Equal(t, "ntfy/1.2.3", r.Header.Get("User-Agent")) |
| 22 |
b, err := io.ReadAll(r.Body) |
| 23 |
require.Nil(t, err) |
| 24 |
body = string(b) |
| 25 |
})) |
| 26 |
defer server.Close() |
| 27 |
|
| 28 |
c := NewClient(testConfig(server.URL)) |
| 29 |
require.Nil(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there", Sender: "phil"})) |
| 30 |
|
| 31 |
form, err := url.ParseQuery(body) |
| 32 |
require.Nil(t, err) |
| 33 |
require.Equal(t, "+1234567890", form.Get("From")) |
| 34 |
require.Equal(t, "+11122233344", form.Get("To")) |
| 35 |
require.Contains(t, form.Get("Twiml"), "You have a message from notify on topic mytopic. Message:") |
| 36 |
require.Contains(t, form.Get("Twiml"), "hi there") |
| 37 |
require.Contains(t, form.Get("Twiml"), "This message was sent by user phil.") |
| 38 |
} |
| 39 |
|
| 40 |
// TestClient_Call_EscapesXML ensures that user-controlled fields cannot break out of the |
| 41 |
// TwiML document, i.e. that a message containing XML is escaped rather than interpreted |
| 42 |
func TestClient_Call_EscapesXML(t *testing.T) { |
| 43 |
var body string |
| 44 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 45 |
b, err := io.ReadAll(r.Body) |
| 46 |
require.Nil(t, err) |
| 47 |
body = string(b) |
| 48 |
})) |
| 49 |
defer server.Close() |
| 50 |
|
| 51 |
c := NewClient(testConfig(server.URL)) |
| 52 |
data := &CallData{ |
| 53 |
Topic: "mytopic", |
| 54 |
Message: `</Say><Say>evil</Say>`, |
| 55 |
Tags: []string{"<tag>"}, |
| 56 |
Sender: `phil & "friends"`, |
| 57 |
} |
| 58 |
require.Nil(t, c.Call("+11122233344", data)) |
| 59 |
|
| 60 |
form, err := url.ParseQuery(body) |
| 61 |
require.Nil(t, err) |
| 62 |
twiml := form.Get("Twiml") |
| 63 |
require.NotContains(t, twiml, "<Say>evil</Say>") |
| 64 |
require.Contains(t, twiml, "</Say><Say>evil</Say>") |
| 65 |
require.Contains(t, twiml, "phil & "friends"") |
| 66 |
// The caller's data must not be modified by the escaping |
| 67 |
require.Equal(t, `</Say><Say>evil</Say>`, data.Message) |
| 68 |
require.Equal(t, []string{"<tag>"}, data.Tags) |
| 69 |
} |
| 70 |
|
| 71 |
func TestClient_Call_CustomCallFormat(t *testing.T) { |
| 72 |
var body string |
| 73 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 |
b, err := io.ReadAll(r.Body) |
| 75 |
require.Nil(t, err) |
| 76 |
body = string(b) |
| 77 |
})) |
| 78 |
defer server.Close() |
| 79 |
|
| 80 |
conf := testConfig(server.URL) |
| 81 |
conf.CallFormat = template.Must(template.New("twiml").Parse(`<Response><Say>{{.Message}} von {{.Sender}}</Say></Response>`)) |
| 82 |
c := NewClient(conf) |
| 83 |
require.Nil(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there", Sender: "phil"})) |
| 84 |
|
| 85 |
form, err := url.ParseQuery(body) |
| 86 |
require.Nil(t, err) |
| 87 |
require.Equal(t, "<Response><Say>hi there von phil</Say></Response>", form.Get("Twiml")) |
| 88 |
} |
| 89 |
|
| 90 |
// TestClient_Call_RendersAllFields covers the fields that the default TwiML template does not |
| 91 |
// use, i.e. Title, Priority and Tags, including the escaping of every tag |
| 92 |
func TestClient_Call_RendersAllFields(t *testing.T) { |
| 93 |
var body string |
| 94 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 95 |
b, err := io.ReadAll(r.Body) |
| 96 |
require.Nil(t, err) |
| 97 |
body = string(b) |
| 98 |
})) |
| 99 |
defer server.Close() |
| 100 |
|
| 101 |
conf := testConfig(server.URL) |
| 102 |
conf.CallFormat = template.Must(template.New("twiml").Parse(`<Response><Say>{{.Title}}/{{.Priority}}{{range .Tags}}/{{.}}{{end}}</Say></Response>`)) |
| 103 |
c := NewClient(conf) |
| 104 |
data := &CallData{ |
| 105 |
Topic: "mytopic", |
| 106 |
Title: "a <title>", |
| 107 |
Priority: 5, |
| 108 |
Tags: []string{"<one>", "two & three"}, |
| 109 |
} |
| 110 |
require.Nil(t, c.Call("+11122233344", data)) |
| 111 |
|
| 112 |
form, err := url.ParseQuery(body) |
| 113 |
require.Nil(t, err) |
| 114 |
require.Equal(t, "<Response><Say>a <title>/5/<one>/two & three</Say></Response>", form.Get("Twiml")) |
| 115 |
} |
| 116 |
|
| 117 |
func TestClient_Call_TemplateError(t *testing.T) { |
| 118 |
conf := testConfig("http://dummy.invalid") |
| 119 |
conf.CallFormat = template.Must(template.New("twiml").Parse(`{{.DoesNotExist}}`)) |
| 120 |
c := NewClient(conf) |
| 121 |
require.Error(t, c.Call("+11122233344", &CallData{Topic: "mytopic"})) |
| 122 |
} |
| 123 |
|
| 124 |
// TestClient_Call_Created ensures that a 201 Created is treated as a success. The Twilio Calls |
| 125 |
// API returns 201 (not 200) for a successfully queued call, so this must not be an error. |
| 126 |
func TestClient_Call_Created(t *testing.T) { |
| 127 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 128 |
w.WriteHeader(http.StatusCreated) |
| 129 |
w.Write([]byte(`{"status":"queued"}`)) |
| 130 |
})) |
| 131 |
defer server.Close() |
| 132 |
|
| 133 |
c := NewClient(testConfig(server.URL)) |
| 134 |
require.Nil(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there"})) |
| 135 |
} |
| 136 |
|
| 137 |
// TestClient_Call_TwilioError ensures that a non-2xx response from Twilio is returned as an |
| 138 |
// error, so that the server counts it as a failure instead of a success. Twilio rejects calls |
| 139 |
// with a 4xx, e.g. for an invalid "To" number, or when the account is out of funds. |
| 140 |
func TestClient_Call_TwilioError(t *testing.T) { |
| 141 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 142 |
w.WriteHeader(http.StatusBadRequest) |
| 143 |
w.Write([]byte(`{"code":21211,"message":"Invalid 'To' Phone Number: +invalid"}`)) |
| 144 |
})) |
| 145 |
defer server.Close() |
| 146 |
|
| 147 |
c := NewClient(testConfig(server.URL)) |
| 148 |
err := c.Call("+invalid", &CallData{Topic: "mytopic", Message: "hi there"}) |
| 149 |
require.Error(t, err) |
| 150 |
require.Contains(t, err.Error(), "400") |
| 151 |
} |
| 152 |
|
| 153 |
func TestClient_Call_TwilioServerError(t *testing.T) { |
| 154 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 155 |
w.WriteHeader(http.StatusInternalServerError) |
| 156 |
})) |
| 157 |
defer server.Close() |
| 158 |
|
| 159 |
c := NewClient(testConfig(server.URL)) |
| 160 |
require.Error(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there"})) |
| 161 |
} |
| 162 |
|
| 163 |
// TestClient_Call_TransportError ensures that a call to an unreachable Twilio API returns an |
| 164 |
// error, so that the server can count it as a failure |
| 165 |
func TestClient_Call_TransportError(t *testing.T) { |
| 166 |
c := NewClient(testConfig(closedServerURL(t))) |
| 167 |
require.Error(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there"})) |
| 168 |
} |
| 169 |
|
| 170 |
func TestClient_Call_InvalidBaseURL(t *testing.T) { |
| 171 |
c := NewClient(testConfig("://invalid")) |
| 172 |
require.Error(t, c.Call("+11122233344", &CallData{Topic: "mytopic", Message: "hi there"})) |
| 173 |
} |
| 174 |
|
| 175 |
// TestClient_Verify_Created ensures that a 201 Created is treated as a success. The Twilio |
| 176 |
// Verify API returns 201 (not 200) when it creates a verification, so this must not be an error. |
| 177 |
func TestClient_Verify_Created(t *testing.T) { |
| 178 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 179 |
w.WriteHeader(http.StatusCreated) |
| 180 |
w.Write([]byte(`{"status":"pending"}`)) |
| 181 |
})) |
| 182 |
defer server.Close() |
| 183 |
|
| 184 |
c := NewClient(testConfig(server.URL)) |
| 185 |
require.Nil(t, c.Verify("+12223334444", "sms")) |
| 186 |
} |
| 187 |
|
| 188 |
// TestClient_Verify_TwilioError ensures that a non-2xx response from Twilio is returned as an |
| 189 |
// error. Without this, no SMS is sent, but the user is still told to check their phone. |
| 190 |
func TestClient_Verify_TwilioError(t *testing.T) { |
| 191 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 192 |
w.WriteHeader(http.StatusBadRequest) |
| 193 |
w.Write([]byte(`{"code":60200,"message":"Invalid parameter"}`)) |
| 194 |
})) |
| 195 |
defer server.Close() |
| 196 |
|
| 197 |
c := NewClient(testConfig(server.URL)) |
| 198 |
err := c.Verify("+12223334444", "sms") |
| 199 |
require.Error(t, err) |
| 200 |
require.Contains(t, err.Error(), "400") |
| 201 |
} |
| 202 |
|
| 203 |
func TestClient_Verify_Unauthorized(t *testing.T) { |
| 204 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 205 |
w.WriteHeader(http.StatusUnauthorized) |
| 206 |
})) |
| 207 |
defer server.Close() |
| 208 |
|
| 209 |
c := NewClient(testConfig(server.URL)) |
| 210 |
require.Error(t, c.Verify("+12223334444", "sms")) |
| 211 |
} |
| 212 |
|
| 213 |
func TestClient_Verify_TransportError(t *testing.T) { |
| 214 |
c := NewClient(testConfig(closedServerURL(t))) |
| 215 |
require.Error(t, c.Verify("+12223334444", "sms")) |
| 216 |
} |
| 217 |
|
| 218 |
func TestClient_CheckVerify_TransportError(t *testing.T) { |
| 219 |
c := NewClient(testConfig(closedServerURL(t))) |
| 220 |
err := c.CheckVerify("+12223334444", "123456") |
| 221 |
require.Error(t, err) |
| 222 |
require.False(t, errors.Is(err, ErrVerificationExpired)) |
| 223 |
} |
| 224 |
|
| 225 |
func TestClient_Verify_Success(t *testing.T) { |
| 226 |
var body string |
| 227 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 228 |
require.Equal(t, "/v2/Services/VA1234567890/Verifications", r.URL.Path) |
| 229 |
require.Equal(t, "Basic QUMxMjM0NTY3ODkwOkFBRUFBMTIzNDU2Nzg5MA==", r.Header.Get("Authorization")) |
| 230 |
b, err := io.ReadAll(r.Body) |
| 231 |
require.Nil(t, err) |
| 232 |
body = string(b) |
| 233 |
})) |
| 234 |
defer server.Close() |
| 235 |
|
| 236 |
c := NewClient(testConfig(server.URL)) |
| 237 |
require.Nil(t, c.Verify("+12223334444", "sms")) |
| 238 |
require.Equal(t, "Channel=sms&To=%2B12223334444", body) |
| 239 |
} |
| 240 |
|
| 241 |
func TestClient_CheckVerify_Success(t *testing.T) { |
| 242 |
var body string |
| 243 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 244 |
require.Equal(t, "/v2/Services/VA1234567890/VerificationCheck", r.URL.Path) |
| 245 |
b, err := io.ReadAll(r.Body) |
| 246 |
require.Nil(t, err) |
| 247 |
body = string(b) |
| 248 |
})) |
| 249 |
defer server.Close() |
| 250 |
|
| 251 |
c := NewClient(testConfig(server.URL)) |
| 252 |
require.Nil(t, c.CheckVerify("+12223334444", "123456")) |
| 253 |
require.Equal(t, "Code=123456&To=%2B12223334444", body) |
| 254 |
} |
| 255 |
|
| 256 |
// TestClient_CheckVerify_Expired ensures that a 404 from the Twilio Verify API is |
| 257 |
// mapped to ErrVerificationExpired, which the server turns into an HTTP 410 |
| 258 |
func TestClient_CheckVerify_Expired(t *testing.T) { |
| 259 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 260 |
w.WriteHeader(http.StatusNotFound) |
| 261 |
})) |
| 262 |
defer server.Close() |
| 263 |
|
| 264 |
c := NewClient(testConfig(server.URL)) |
| 265 |
err := c.CheckVerify("+12223334444", "123456") |
| 266 |
require.True(t, errors.Is(err, ErrVerificationExpired)) |
| 267 |
} |
| 268 |
|
| 269 |
func TestClient_CheckVerify_OtherError(t *testing.T) { |
| 270 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 271 |
w.WriteHeader(http.StatusInternalServerError) |
| 272 |
})) |
| 273 |
defer server.Close() |
| 274 |
|
| 275 |
c := NewClient(testConfig(server.URL)) |
| 276 |
err := c.CheckVerify("+12223334444", "123456") |
| 277 |
require.Error(t, err) |
| 278 |
require.False(t, errors.Is(err, ErrVerificationExpired)) |
| 279 |
} |
| 280 |
|
| 281 |
// closedServerURL returns the URL of a server that is not listening anymore, to simulate an |
| 282 |
// unreachable Twilio API |
| 283 |
func closedServerURL(t *testing.T) string { |
| 284 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 285 |
t.Fatal("Should not be called") |
| 286 |
})) |
| 287 |
server.Close() |
| 288 |
return server.URL |
| 289 |
} |
| 290 |
|
| 291 |
func testConfig(baseURL string) *Config { |
| 292 |
return &Config{ |
| 293 |
Account: "AC1234567890", |
| 294 |
AuthToken: "AAEAA1234567890", |
| 295 |
PhoneNumber: "+1234567890", |
| 296 |
CallsBaseURL: baseURL, |
| 297 |
VerifyBaseURL: baseURL, |
| 298 |
VerifyService: "VA1234567890", |
| 299 |
BuildVersion: "1.2.3", |
| 300 |
} |
| 301 |
} |