-
Notifications
You must be signed in to change notification settings - Fork 381
/
tests.py
240 lines (181 loc) · 11.9 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from mock import patch
from twilio.base.exceptions import TwilioRestException
from django.urls import reverse
from temba.channels.models import Channel
from temba.orgs.models import ACCOUNT_SID, ACCOUNT_TOKEN
from temba.tests import TembaTest
from temba.tests.twilio import MockRequestValidator, MockTwilioClient
class TwilioTypeTest(TembaTest):
@patch("temba.ivr.clients.TwilioClient", MockTwilioClient)
@patch("twilio.request_validator.RequestValidator", MockRequestValidator)
def test_claim(self):
self.login(self.admin)
claim_twilio = reverse("channels.types.twilio.claim")
# remove any existing channels
self.org.channels.update(is_active=False)
# make sure twilio is on the claim page
response = self.client.get(reverse("channels.channel_claim"))
self.assertContains(response, "Twilio")
response = self.client.get(claim_twilio)
self.assertEqual(response.status_code, 302)
response = self.client.get(claim_twilio, follow=True)
self.assertEqual(response.request["PATH_INFO"], reverse("orgs.org_twilio_connect"))
# attach a Twilio accont to the org
self.org.config = {ACCOUNT_SID: "account-sid", ACCOUNT_TOKEN: "account-token"}
self.org.save()
# hit the claim page, should now have a claim twilio link
response = self.client.get(reverse("channels.channel_claim"))
self.assertContains(response, claim_twilio)
response = self.client.get(claim_twilio)
self.assertIn("account_trial", response.context)
self.assertFalse(response.context["account_trial"])
with patch("temba.orgs.models.Org.get_twilio_client") as mock_get_twilio_client:
mock_get_twilio_client.return_value = None
response = self.client.get(claim_twilio)
self.assertRedirects(response, reverse("orgs.org_twilio_connect"))
mock_get_twilio_client.side_effect = TwilioRestException(
401, "http://twilio", msg="Authentication Failure", code=20003
)
response = self.client.get(claim_twilio)
self.assertRedirects(response, reverse("orgs.org_twilio_connect"))
with patch("temba.tests.twilio.MockTwilioClient.MockAccounts.get") as mock_get:
mock_get.return_value = MockTwilioClient.MockAccount("Trial")
response = self.client.get(claim_twilio)
self.assertIn("account_trial", response.context)
self.assertTrue(response.context["account_trial"])
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.list") as mock_search:
search_url = reverse("channels.channel_search_numbers")
# try making empty request
response = self.client.post(search_url, {})
self.assertEqual(response.json(), [])
# try searching for US number
mock_search.return_value = [MockTwilioClient.MockPhoneNumber("+12062345678")]
response = self.client.post(search_url, {"country": "US", "area_code": "206"})
self.assertEqual(response.json(), ["+1 206-234-5678", "+1 206-234-5678"])
# try searching without area code
response = self.client.post(search_url, {"country": "US", "area_code": ""})
self.assertEqual(response.json(), ["+1 206-234-5678", "+1 206-234-5678"])
mock_search.return_value = []
response = self.client.post(search_url, {"country": "US", "area_code": ""})
self.assertEqual(
response.json()["error"], "Sorry, no numbers found, please enter another area code and try again."
)
# try searching for non-US number
mock_search.return_value = [MockTwilioClient.MockPhoneNumber("+442812345678")]
response = self.client.post(search_url, {"country": "GB", "area_code": "028"})
self.assertEqual(response.json(), ["+44 28 1234 5678", "+44 28 1234 5678"])
mock_search.return_value = []
response = self.client.post(search_url, {"country": "GB", "area_code": ""})
self.assertEqual(
response.json()["error"], "Sorry, no numbers found, please enter another pattern and try again."
)
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.stream") as mock_numbers:
mock_numbers.return_value = iter([MockTwilioClient.MockPhoneNumber("+12062345678")])
with patch("temba.tests.twilio.MockTwilioClient.MockShortCodes.stream") as mock_short_codes:
mock_short_codes.return_value = iter([])
response = self.client.get(claim_twilio)
self.assertContains(response, "206-234-5678")
# claim it
response = self.client.post(claim_twilio, dict(country="US", phone_number="12062345678"))
self.assertRedirects(response, reverse("public.public_welcome") + "?success")
# make sure it is actually connected
channel = Channel.objects.get(channel_type="T", org=self.org)
self.assertEqual(
channel.role, Channel.ROLE_CALL + Channel.ROLE_ANSWER + Channel.ROLE_SEND + Channel.ROLE_RECEIVE
)
channel_config = channel.config
self.assertEqual(channel_config[Channel.CONFIG_ACCOUNT_SID], "account-sid")
self.assertEqual(channel_config[Channel.CONFIG_AUTH_TOKEN], "account-token")
self.assertTrue(channel_config[Channel.CONFIG_APPLICATION_SID])
self.assertTrue(channel_config[Channel.CONFIG_NUMBER_SID])
# voice only number
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.stream") as mock_numbers:
mock_numbers.return_value = iter([MockTwilioClient.MockPhoneNumber("+554139087835")])
with patch("temba.tests.twilio.MockTwilioClient.MockShortCodes.stream") as mock_short_codes:
mock_short_codes.return_value = iter([])
Channel.objects.all().delete()
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.get") as mock_numbers_get:
mock_numbers_get.return_value = MockTwilioClient.MockPhoneNumber("+554139087835")
response = self.client.get(claim_twilio)
self.assertContains(response, "+55 41 3908-7835")
# claim it
mock_numbers.return_value = iter([MockTwilioClient.MockPhoneNumber("+554139087835")])
response = self.client.post(claim_twilio, dict(country="BR", phone_number="554139087835"))
self.assertRedirects(response, reverse("public.public_welcome") + "?success")
# make sure it is actually connected
channel = Channel.objects.get(channel_type="T", org=self.org)
self.assertEqual(channel.role, Channel.ROLE_CALL + Channel.ROLE_ANSWER)
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.stream") as mock_numbers:
mock_numbers.return_value = iter([MockTwilioClient.MockPhoneNumber("+4545335500")])
with patch("temba.tests.twilio.MockTwilioClient.MockShortCodes.stream") as mock_short_codes:
mock_short_codes.return_value = iter([])
Channel.objects.all().delete()
response = self.client.get(claim_twilio)
self.assertContains(response, "45 33 55 00")
self.assertEqual(mock_numbers.call_args_list[0][1], {"page_size": 1000})
# claim it
response = self.client.post(claim_twilio, dict(country="DK", phone_number="4545335500"))
self.assertRedirects(response, reverse("public.public_welcome") + "?success")
# make sure it is actually connected
Channel.objects.get(channel_type="T", org=self.org)
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.stream") as mock_numbers:
mock_numbers.return_value = iter([])
with patch("temba.tests.twilio.MockTwilioClient.MockShortCodes.stream") as mock_short_codes:
mock_short_codes.return_value = iter([MockTwilioClient.MockShortCode("8080")])
with patch("temba.tests.twilio.MockTwilioClient.MockShortCodes.get") as mock_short_codes_get:
mock_short_codes_get.return_value = MockTwilioClient.MockShortCode("8080")
Channel.objects.all().delete()
self.org.timezone = "America/New_York"
self.org.save()
response = self.client.get(claim_twilio)
self.assertContains(response, "8080")
self.assertContains(response, 'class="country">US') # we look up the country from the timezone
# claim it
mock_short_codes.return_value = iter([MockTwilioClient.MockShortCode("8080")])
response = self.client.post(claim_twilio, dict(country="US", phone_number="8080"))
self.assertRedirects(response, reverse("public.public_welcome") + "?success")
self.assertEqual(mock_numbers.call_args_list[0][1], {"page_size": 1000})
# make sure it is actually connected
Channel.objects.get(channel_type="T", org=self.org)
twilio_channel = self.org.channels.all().first()
# make channel support both sms and voice to check we clear both applications
twilio_channel.role = Channel.ROLE_SEND + Channel.ROLE_RECEIVE + Channel.ROLE_ANSWER + Channel.ROLE_CALL
twilio_channel.save()
self.assertEqual("T", twilio_channel.channel_type)
with self.settings(IS_PROD=True):
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumber.update") as mock_numbers:
# our twilio channel removal should fail on bad auth
mock_numbers.side_effect = TwilioRestException(
401, "http://twilio", msg="Authentication Failure", code=20003
)
self.client.post(reverse("channels.channel_delete", args=[twilio_channel.pk]))
self.assertIsNotNone(self.org.channels.all().first())
# or other arbitrary twilio errors
mock_numbers.side_effect = TwilioRestException(400, "http://twilio", msg="Twilio Error", code=123)
self.client.post(reverse("channels.channel_delete", args=[twilio_channel.pk]))
self.assertIsNotNone(self.org.channels.all().first())
# now lets be successful
mock_numbers.side_effect = None
self.client.post(reverse("channels.channel_delete", args=[twilio_channel.pk]))
self.assertIsNone(self.org.channels.filter(is_active=True).first())
self.assertEqual(
mock_numbers.call_args_list[-1][1], dict(voice_application_sid="", sms_application_sid="")
)
@patch("temba.ivr.clients.TwilioClient", MockTwilioClient)
@patch("twilio.request_validator.RequestValidator", MockRequestValidator)
def test_deactivate(self):
# make our channel of the twilio ilk
self.org.connect_twilio("TEST_SID", "TEST_TOKEN", self.admin)
twilio_channel = self.org.channels.all().first()
twilio_channel.channel_type = "T"
twilio_channel.save()
# mock an authentication failure during the release process
with self.settings(IS_PROD=True):
with patch("temba.tests.twilio.MockTwilioClient.MockPhoneNumbers.update") as mock_numbers:
mock_numbers.side_effect = TwilioRestException(
401, "http://twilio", msg="Authentication Failure", code=20003
)
# releasing shouldn't blow up on auth failures
twilio_channel.release()
twilio_channel.refresh_from_db()
self.assertFalse(twilio_channel.is_active)