Skip to content

Commit 3304412

Browse files
committed
[tests] add more tests
1 parent fb05070 commit 3304412

File tree

3 files changed

+229
-5
lines changed

3 files changed

+229
-5
lines changed

android_sms_gateway/ahttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ async def _process_response(self, response: aiohttp.ClientResponse) -> dict:
8080
if response.status == 204:
8181
return {}
8282

83-
return response.json()
83+
return await response.json()
8484
except aiohttp.ClientResponseError as e:
8585
# Extract error message from response if available
8686
error_data = {}
8787
try:
88-
error_data = response.json()
88+
error_data = await response.json()
8989
except ValueError:
9090
# Response is not JSON
9191
pass

tests/test_domain.py

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import datetime
23

34
from android_sms_gateway.enums import WebhookEvent, MessagePriority
45
from android_sms_gateway.domain import (
@@ -7,6 +8,7 @@
78
Webhook,
89
Message,
910
TextMessage,
11+
DataMessage,
1012
)
1113

1214

@@ -265,3 +267,219 @@ def test_message_asdict(
265267
)
266268

267269
assert message.asdict() == expected
270+
271+
272+
# Test for Message with data_message instead of text_message
273+
def test_message_with_data_message_only():
274+
"""Test creating a message with data_message only"""
275+
data_msg = DataMessage(data="base64encodeddata", port=1234)
276+
message = Message(
277+
phone_numbers=["123", "456"],
278+
data_message=data_msg,
279+
with_delivery_report=True,
280+
is_encrypted=False,
281+
)
282+
283+
assert message.data_message == data_msg
284+
assert message.text_message is None
285+
286+
287+
def test_message_serialization_with_data_message():
288+
"""Test serialization includes data_message"""
289+
data_msg = DataMessage(data="base64encodeddata", port=1234)
290+
message = Message(
291+
phone_numbers=["123", "456"],
292+
data_message=data_msg,
293+
with_delivery_report=True,
294+
is_encrypted=False,
295+
id="msg_123",
296+
device_id="device_001",
297+
)
298+
299+
expected_dict = {
300+
"dataMessage": {"data": "base64encodeddata", "port": 1234},
301+
"phoneNumbers": ["123", "456"],
302+
"withDeliveryReport": True,
303+
"isEncrypted": False,
304+
"id": "msg_123",
305+
"deviceId": "device_001",
306+
}
307+
308+
assert message.asdict() == expected_dict
309+
310+
311+
# Test for Message with both ttl and valid_until (should raise ValueError)
312+
def test_message_with_both_ttl_and_valid_until_raises_error():
313+
"""Test that providing both ttl and valid_until raises ValueError"""
314+
text_msg = TextMessage(text="Hello, world!")
315+
316+
with pytest.raises(ValueError, match="ttl and valid_until are mutually exclusive"):
317+
Message(
318+
phone_numbers=["123", "456"],
319+
text_message=text_msg,
320+
ttl=300,
321+
valid_until=datetime.datetime.now() + datetime.timedelta(seconds=600),
322+
)
323+
324+
325+
def test_message_with_ttl_only():
326+
"""Test that providing only ttl works correctly"""
327+
text_msg = TextMessage(text="Hello, world!")
328+
message = Message(
329+
phone_numbers=["123", "456"],
330+
text_message=text_msg,
331+
ttl=300,
332+
)
333+
334+
assert message.ttl == 300
335+
assert message.valid_until is None
336+
assert "ttl" in message.asdict()
337+
assert "validUntil" not in message.asdict()
338+
339+
340+
def test_message_with_valid_until_only():
341+
"""Test that providing only valid_until works correctly"""
342+
text_msg = TextMessage(text="Hello, world!")
343+
valid_until_time = datetime.datetime.now() + datetime.timedelta(seconds=600)
344+
message = Message(
345+
phone_numbers=["123", "456"],
346+
text_message=text_msg,
347+
valid_until=valid_until_time,
348+
)
349+
350+
assert message.valid_until == valid_until_time
351+
assert message.ttl is None
352+
assert "validUntil" in message.asdict()
353+
assert "ttl" not in message.asdict()
354+
355+
356+
# Test content property for both text and data messages, plus error case
357+
def test_message_content_property_with_text_message():
358+
"""Test content property returns text_message when text is set"""
359+
text_msg = TextMessage(text="Hello, world!")
360+
message = Message(
361+
phone_numbers=["123", "456"],
362+
text_message=text_msg,
363+
)
364+
365+
assert message.content == "Hello, world!"
366+
367+
368+
def test_message_content_property_with_data_message():
369+
"""Test content property returns data_message when data is set"""
370+
data_msg = DataMessage(data="base64encodeddata", port=1234)
371+
message = Message(
372+
phone_numbers=["123", "456"],
373+
data_message=data_msg,
374+
)
375+
376+
assert message.content == "base64encodeddata"
377+
378+
379+
def test_message_without_text_or_data_raises_error():
380+
"""Test that creating message without text or data raises appropriate error"""
381+
message = Message(phone_numbers=["123", "456"])
382+
383+
with pytest.raises(ValueError, match="Message has no content"):
384+
_ = message.content
385+
386+
387+
# Test serialization including device_id and valid_until
388+
def test_message_serialization_with_device_id():
389+
"""Test serialization includes device_id when present"""
390+
text_msg = TextMessage(text="Hello, world!")
391+
message = Message(
392+
phone_numbers=["123", "456"],
393+
text_message=text_msg,
394+
device_id="device_001",
395+
)
396+
397+
assert "deviceId" in message.asdict()
398+
assert message.asdict()["deviceId"] == "device_001"
399+
400+
401+
def test_message_serialization_with_valid_until():
402+
"""Test serialization includes valid_until when present"""
403+
text_msg = TextMessage(text="Hello, world!")
404+
valid_until_time = datetime.datetime.now() + datetime.timedelta(seconds=600)
405+
message = Message(
406+
phone_numbers=["123", "456"],
407+
text_message=text_msg,
408+
valid_until=valid_until_time,
409+
)
410+
411+
assert "validUntil" in message.asdict()
412+
assert message.asdict()["validUntil"] == valid_until_time.isoformat()
413+
414+
415+
def test_message_serialization_with_ttl():
416+
"""Test serialization includes ttl when present"""
417+
text_msg = TextMessage(text="Hello, world!")
418+
message = Message(
419+
phone_numbers=["123", "456"],
420+
text_message=text_msg,
421+
ttl=300,
422+
)
423+
424+
assert "ttl" in message.asdict()
425+
assert message.asdict()["ttl"] == 300
426+
427+
428+
def test_message_serialization_format_for_text_message():
429+
"""Test serialization format for text message"""
430+
text_msg = TextMessage(text="Hello, world!")
431+
message = Message(
432+
phone_numbers=["123", "456"],
433+
text_message=text_msg,
434+
with_delivery_report=True,
435+
is_encrypted=False,
436+
id="msg_123",
437+
device_id="device_001",
438+
ttl=300,
439+
sim_number=1,
440+
priority=MessagePriority.BYPASS_THRESHOLD,
441+
)
442+
443+
expected_dict = {
444+
"textMessage": {"text": "Hello, world!"},
445+
"phoneNumbers": ["123", "456"],
446+
"withDeliveryReport": True,
447+
"isEncrypted": False,
448+
"id": "msg_123",
449+
"deviceId": "device_001",
450+
"ttl": 300,
451+
"simNumber": 1,
452+
"priority": 100,
453+
}
454+
455+
assert message.asdict() == expected_dict
456+
457+
458+
def test_message_serialization_format_for_data_message():
459+
"""Test serialization format for data message"""
460+
data_msg = DataMessage(data="base64encodeddata", port=1234)
461+
message = Message(
462+
phone_numbers=["123", "456"],
463+
data_message=data_msg,
464+
with_delivery_report=True,
465+
is_encrypted=False,
466+
id="msg_123",
467+
device_id="device_001",
468+
ttl=300,
469+
sim_number=1,
470+
priority=MessagePriority.BYPASS_THRESHOLD,
471+
)
472+
473+
expected_dict = {
474+
"dataMessage": {"data": "base64encodeddata", "port": 1234},
475+
"phoneNumbers": ["123", "456"],
476+
"withDeliveryReport": True,
477+
"isEncrypted": False,
478+
"id": "msg_123",
479+
"deviceId": "device_001",
480+
"ttl": 300,
481+
"simNumber": 1,
482+
"priority": 100,
483+
}
484+
485+
assert message.asdict() == expected_dict

tests/test_error_handling.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import Mock
1+
from unittest.mock import AsyncMock, Mock
22

33
import aiohttp
44
import httpx
@@ -175,12 +175,15 @@ class TestAiohttpAsyncHttpClientErrorHandling:
175175
@pytest.mark.asyncio
176176
async def test_raises_bad_request_error_for_400(self):
177177
"""Test that BadRequestError is raised for 400 status."""
178+
json = AsyncMock()
179+
json.return_value = {"error": "bad request"}
180+
178181
mock_response = Mock()
179182
mock_response.status = 400
180183
mock_response.raise_for_status.side_effect = aiohttp.ClientResponseError(
181184
request_info=Mock(), history=(), status=400, message="400 Client Error"
182185
)
183-
mock_response.json.return_value = {"error": "bad request"}
186+
mock_response.json = json
184187

185188
client = AiohttpAsyncHttpClient()
186189

@@ -193,12 +196,15 @@ async def test_raises_bad_request_error_for_400(self):
193196
@pytest.mark.asyncio
194197
async def test_raises_not_found_error_for_404(self):
195198
"""Test that NotFoundError is raised for 404 status."""
199+
json = AsyncMock()
200+
json.return_value = {"error": "not found"}
201+
196202
mock_response = Mock()
197203
mock_response.status = 404
198204
mock_response.raise_for_status.side_effect = aiohttp.ClientResponseError(
199205
request_info=Mock(), history=(), status=404, message="404 Not Found"
200206
)
201-
mock_response.json.return_value = {"error": "not found"}
207+
mock_response.json = json
202208

203209
client = AiohttpAsyncHttpClient()
204210

0 commit comments

Comments
 (0)