|
1 | 1 | import pytest |
| 2 | +import datetime |
2 | 3 |
|
3 | 4 | from android_sms_gateway.enums import WebhookEvent, MessagePriority |
4 | 5 | from android_sms_gateway.domain import ( |
|
7 | 8 | Webhook, |
8 | 9 | Message, |
9 | 10 | TextMessage, |
| 11 | + DataMessage, |
10 | 12 | ) |
11 | 13 |
|
12 | 14 |
|
@@ -265,3 +267,219 @@ def test_message_asdict( |
265 | 267 | ) |
266 | 268 |
|
267 | 269 | 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 |
0 commit comments