Debian Codegraph @

testing/integration/twilio/config_test.go

raw
1 package twilio_test
2
3 import (
4 "net/http"
5 "testing"
6 "testing/synctest"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/mock"
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestServiceURLRoundTrip(t *testing.T) {
14 t.Parallel()
15 synctest.Test(t, func(t *testing.T) {
16 mockClient := &MockHTTPClient{}
17 service := createTestService(t, validTwilioURL, mockClient)
18
19 serviceURL := service.Config.GetURL()
20 assert.Equal(t, "twilio", serviceURL.Scheme)
21 assert.Equal(t, "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", serviceURL.User.Username())
22
23 password, hasPassword := serviceURL.User.Password()
24 assert.True(t, hasPassword)
25 assert.Equal(t, "authToken", password)
26 assert.Equal(t, "+15551234567", serviceURL.Host)
27
28 mockClient.AssertExpectations(t)
29 })
30 }
31
32 func TestConfigMultipleRecipients(t *testing.T) {
33 t.Parallel()
34 synctest.Test(t, func(t *testing.T) {
35 mockClient := &MockHTTPClient{}
36 service := createTestService(
37 t,
38 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+15551234567/+15559876543/+15551111111",
39 mockClient,
40 )
41
42 assert.Equal(t, []string{"+15559876543", "+15551111111"}, service.Config.ToNumbers)
43
44 serviceURL := service.Config.GetURL()
45 assert.Equal(t, "/+15559876543/+15551111111", serviceURL.Path)
46
47 mockClient.AssertExpectations(t)
48 })
49 }
50
51 func TestConfigPhoneNumberNormalization(t *testing.T) {
52 t.Parallel()
53
54 tests := []struct {
55 name string
56 url string
57 expectedFrom string
58 expectedTo []string
59 }{
60 {
61 "strips dashes",
62 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+1-555-123-4567/+1-555-987-6543",
63 "+15551234567",
64 []string{"+15559876543"},
65 },
66 {
67 "strips parentheses",
68 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+1(555)1234567/+1(555)9876543",
69 "+15551234567",
70 []string{"+15559876543"},
71 },
72 {
73 "strips dots",
74 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@+1.555.123.4567/+1.555.987.6543",
75 "+15551234567",
76 []string{"+15559876543"},
77 },
78 }
79
80 for _, tt := range tests {
81 t.Run(tt.name, func(t *testing.T) {
82 t.Parallel()
83 synctest.Test(t, func(t *testing.T) {
84 mockClient := &MockHTTPClient{}
85 service := createTestService(t, tt.url, mockClient)
86
87 assert.Equal(t, tt.expectedFrom, service.Config.FromNumber)
88 assert.Equal(t, tt.expectedTo, service.Config.ToNumbers)
89
90 mockClient.AssertExpectations(t)
91 })
92 })
93 }
94 }
95
96 func TestConfigMessagingServiceSID(t *testing.T) {
97 t.Parallel()
98 synctest.Test(t, func(t *testing.T) {
99 mockClient := &MockHTTPClient{}
100 service := createTestService(
101 t,
102 "twilio://ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:authToken@MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/+15559876543",
103 mockClient,
104 )
105
106 assert.Equal(t, "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", service.Config.FromNumber)
107
108 mockClient.AssertExpectations(t)
109 })
110 }
111
112 func TestConfigTitleParameter(t *testing.T) {
113 t.Parallel()
114 synctest.Test(t, func(t *testing.T) {
115 mockClient := &MockHTTPClient{}
116 service := createTestService(
117 t,
118 validTwilioURL+"?title=MyTitle",
119 mockClient,
120 )
121
122 assert.Equal(t, "MyTitle", service.Config.Title)
123
124 // Verify title appears in sent message
125 mockClient.On("Do", mock.Anything).
126 Return(createMockResponse(http.StatusCreated, `{"sid": "SM123"}`), nil).
127 Once()
128
129 err := service.Send("Test body", nil)
130 require.NoError(t, err)
131
132 assertRequestContains(t, mockClient, "Body=MyTitle")
133
134 mockClient.AssertExpectations(t)
135 })
136 }
137
138 func TestConfigEmptyTitle(t *testing.T) {
139 t.Parallel()
140 synctest.Test(t, func(t *testing.T) {
141 mockClient := &MockHTTPClient{}
142 service := createTestService(t, validTwilioURL, mockClient)
143
144 assert.Empty(t, service.Config.Title)
145
146 mockClient.AssertExpectations(t)
147 })
148 }