-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sms.py
More file actions
343 lines (303 loc) · 13.3 KB
/
test_sms.py
File metadata and controls
343 lines (303 loc) · 13.3 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from datetime import datetime
from unittest.mock import Mock
import pytest
from devo_global_comms_python.exceptions import DevoValidationException
from devo_global_comms_python.resources.sms import SMSResource
class TestSMSResource:
"""Test cases for the SMS resource with new API implementation."""
@pytest.fixture
def sms_resource(self, mock_client):
"""Create an SMS resource instance."""
return SMSResource(mock_client)
def test_send_sms_success(self, sms_resource, test_phone_number):
"""Test sending an SMS successfully using the new quick-send API."""
# Setup mock response matching the API spec
mock_response = Mock()
mock_response.json.return_value = {
"id": "msg_123456789",
"user_id": "user_123",
"tenant_id": "tenant_123",
"sender_id": "sender_123",
"recipient": test_phone_number,
"message": "Hello, World!",
"account_id": "account_123",
"account_type": "sms",
"status": "queued",
"message_timeline": {},
"message_id": "msg_123456789",
"bulksmsid": "bulk_123",
"sent_date": "2024-01-01T12:00:00Z",
"direction": "sent",
"recipientcontactid": "contact_123",
"api_route": "user-api/sms/quick-send",
"apimode": "quick-send",
"quicksendidentifier": "quick_123",
"hirvalidation": True,
}
sms_resource.client.post.return_value = mock_response
# Test the new send_sms method
result = sms_resource.send_sms(
recipient=test_phone_number,
message="Hello, World!",
sender="+1987654321",
hirvalidation=True,
)
# Verify the response
assert result.id == "msg_123456789"
assert result.recipient == test_phone_number
assert result.message == "Hello, World!"
assert result.status == "queued"
assert result.hirvalidation is True
# Verify the API call
sms_resource.client.post.assert_called_once_with(
"user-api/sms/quick-send",
json={
"sender": "+1987654321",
"recipient": test_phone_number,
"message": "Hello, World!",
"hirvalidation": True,
},
sandbox=False,
)
def test_send_sms_with_invalid_recipient(self, sms_resource):
"""Test sending SMS with invalid recipient phone number."""
with pytest.raises(DevoValidationException):
sms_resource.send_sms(
recipient="invalid-phone",
message="Hello, World!",
sender="+1987654321",
)
def test_send_sms_with_empty_message(self, sms_resource, test_phone_number):
"""Test sending SMS with empty message."""
with pytest.raises(DevoValidationException):
sms_resource.send_sms(recipient=test_phone_number, message="", sender="+1987654321")
def test_send_sms_with_empty_sender(self, sms_resource, test_phone_number):
"""Test sending SMS with empty sender."""
with pytest.raises(DevoValidationException):
sms_resource.send_sms(recipient=test_phone_number, message="Hello, World!", sender="")
def test_get_senders_success(self, sms_resource):
"""Test retrieving senders list successfully."""
# Setup mock response matching the API spec
mock_response = Mock()
mock_response.json.return_value = {
"senders": [
{
"id": "sender_1",
"sender_id": "sender_123",
"gateways_id": "gateway_1",
"phone_number": "+1234567890",
"number": "+1234567890",
"istest": False,
"type": "number",
},
{
"id": "sender_2",
"sender_id": "sender_456",
"gateways_id": "gateway_2",
"phone_number": "+1987654321",
"number": "+1987654321",
"istest": True,
"type": "number",
},
]
}
sms_resource.client.get.return_value = mock_response
result = sms_resource.get_senders()
# Verify the response
assert len(result.senders) == 2
assert result.senders[0].phone_number == "+1234567890"
assert result.senders[0].istest is False
assert result.senders[1].phone_number == "+1987654321"
assert result.senders[1].istest is True
# Verify the API call
sms_resource.client.get.assert_called_once_with("user-api/me/senders", sandbox=False)
def test_buy_number_success(self, sms_resource):
"""Test purchasing a phone number successfully."""
# Setup mock response matching the API spec
mock_response = Mock()
mock_response.json.return_value = {
"features": [
{
"name": "SMS",
"reservable": True,
"region_id": "US",
"number_type": "mobile",
"quickship": True,
"region_information": {
"region_type": "country",
"region_name": "United States",
},
"phone_number": "+1234567890",
"cost_information": {
"monthly_cost": "1.00",
"setup_cost": "0.00",
"currency": "USD",
},
"best_effort": False,
"number_provider_type": "twilio",
}
]
}
sms_resource.client.post.return_value = mock_response
result = sms_resource.buy_number(
region="US",
number="+1234567890",
number_type="mobile",
agency_authorized_representative="Jane Doe",
agency_representative_email="jane.doe@company.com",
is_longcode=True,
is_automated_enabled=True,
)
# Verify the response
assert len(result.features) == 1
feature = result.features[0]
assert feature.phone_number == "+1234567890"
assert feature.region_information.region_name == "United States"
assert feature.cost_information.monthly_cost == "1.00"
# Verify the API call
call_args = sms_resource.client.post.call_args
assert call_args[0][0] == "user-api/numbers/buy"
request_data = call_args[1]["json"]
assert request_data["region"] == "US"
assert request_data["number"] == "+1234567890"
assert request_data["number_type"] == "mobile"
assert request_data["agency_authorized_representative"] == "Jane Doe"
assert request_data["agency_representative_email"] == "jane.doe@company.com"
def test_buy_number_with_invalid_email(self, sms_resource):
"""Test purchasing a number with invalid email."""
with pytest.raises(DevoValidationException):
sms_resource.buy_number(
region="US",
number="+1234567890",
number_type="mobile",
agency_authorized_representative="Jane Doe",
agency_representative_email="invalid-email",
)
def test_buy_number_with_datetime(self, sms_resource):
"""Test purchasing a number with agreement date."""
mock_response = Mock()
mock_response.json.return_value = {"features": []}
sms_resource.client.post.return_value = mock_response
agreement_date = datetime(2024, 1, 1, 12, 0, 0)
sms_resource.buy_number(
region="US",
number="+1234567890",
number_type="mobile",
agency_authorized_representative="Jane Doe",
agency_representative_email="jane.doe@company.com",
agreement_last_sent_date=agreement_date,
)
# Verify the API call includes the datetime
call_args = sms_resource.client.post.call_args
request_data = call_args[1]["json"]
assert "agreement_last_sent_date" in request_data
def test_get_available_numbers_success(self, sms_resource):
"""Test getting available numbers successfully."""
# Setup mock response matching the API spec
mock_response = Mock()
mock_response.json.return_value = {
"numbers": [
{
"features": [
{
"name": "SMS",
"reservable": True,
"region_id": "US",
"number_type": "mobile",
"quickship": True,
"region_information": {
"region_type": "country",
"region_name": "United States",
},
"phone_number": "+1234567890",
"cost_information": {
"monthly_cost": "1.00",
"setup_cost": "0.00",
"currency": "USD",
},
"best_effort": False,
"number_provider_type": "twilio",
}
]
}
]
}
sms_resource.client.get.return_value = mock_response
result = sms_resource.get_available_numbers(region="US", limit=10, type="mobile", page=1)
# Verify the response
assert len(result.numbers) == 1
number_info = result.numbers[0]
assert len(number_info.features) == 1
feature = number_info.features[0]
assert feature.phone_number == "+1234567890"
assert feature.number_type == "mobile"
# Verify the API call
call_args = sms_resource.client.get.call_args
assert call_args[0][0] == "user-api/numbers"
params = call_args[1]["params"]
assert params["region"] == "US"
assert params["limit"] == 10
assert params["type"] == "mobile"
assert params["page"] == 1
def test_get_available_numbers_default_region(self, sms_resource):
"""Test getting available numbers with default region."""
mock_response = Mock()
mock_response.json.return_value = {"numbers": []}
sms_resource.client.get.return_value = mock_response
sms_resource.get_available_numbers()
# Verify default region is used
call_args = sms_resource.client.get.call_args
params = call_args[1]["params"]
assert params["region"] == "US"
def test_get_available_numbers_with_capabilities(self, sms_resource):
"""Test getting available numbers with capabilities filter."""
mock_response = Mock()
mock_response.json.return_value = {"numbers": []}
sms_resource.client.get.return_value = mock_response
capabilities = ["SMS", "MMS"]
sms_resource.get_available_numbers(capabilities=capabilities)
# Verify capabilities are passed
call_args = sms_resource.client.get.call_args
params = call_args[1]["params"]
assert params["capabilities"] == capabilities
# Legacy method tests for backward compatibility
def test_legacy_send_method(self, sms_resource, test_phone_number):
"""Test legacy send method for backward compatibility."""
mock_response = Mock()
mock_response.json.return_value = {
"id": "msg_123456789",
"user_id": "user_123",
"tenant_id": "tenant_123",
"sender_id": "sender_123",
"recipient": test_phone_number,
"message": "Hello, World!",
"account_id": "account_123",
"account_type": "sms",
"status": "queued",
"message_timeline": {},
"message_id": "msg_123456789",
"bulksmsid": "bulk_123",
"sent_date": "2024-01-01T12:00:00Z",
"direction": "sent",
"recipientcontactid": "contact_123",
"api_route": "user-api/sms/quick-send",
"apimode": "quick-send",
"quicksendidentifier": "quick_123",
"hirvalidation": True,
}
sms_resource.client.post.return_value = mock_response
# Test legacy method
result = sms_resource.send(to=test_phone_number, body="Hello, World!", from_="+1987654321")
# Should return the new response type
assert result.id == "msg_123456789"
assert result.recipient == test_phone_number
def test_legacy_send_without_sender_fails(self, sms_resource, test_phone_number):
"""Test legacy send method fails without sender."""
with pytest.raises(DevoValidationException, match="Sender .* is required"):
sms_resource.send(to=test_phone_number, body="Hello, World!")
def test_legacy_methods_not_implemented(self, sms_resource):
"""Test that unsupported legacy methods don't exist."""
# These methods were completely removed for cleaner API
assert not hasattr(sms_resource, "get")
assert not hasattr(sms_resource, "list")
assert not hasattr(sms_resource, "cancel")