|
2 | 2 | import typing |
3 | 3 | import pytest |
4 | 4 |
|
| 5 | +from httpx import TimeoutException |
5 | 6 | from unittest.mock import Mock, patch |
6 | 7 | from tempmail import ( |
7 | 8 | TempMailClient, |
@@ -469,3 +470,93 @@ def test_request_exception(self, mock_request): |
469 | 470 | client = TempMailClient("test-api-key") |
470 | 471 | with pytest.raises(TempMailError, match="Request failed"): |
471 | 472 | client.list_domains() |
| 473 | + |
| 474 | + @patch("tempmail.client.httpx.Client.request") |
| 475 | + def test_create_email_with_specific_email(self, mock_request): |
| 476 | + mock_response = Mock() |
| 477 | + mock_response.status_code = 200 |
| 478 | + mock_response.json.return_value = { |
| 479 | + |
| 480 | + "ttl": 86400, |
| 481 | + } |
| 482 | + mock_response.headers = self._rate_limit_headers |
| 483 | + mock_request.return_value = mock_response |
| 484 | + |
| 485 | + client = TempMailClient("test-api-key") |
| 486 | + email = client. create_email( email="[email protected]") |
| 487 | + assert email == EmailAddress( email="[email protected]", ttl=86400) |
| 488 | + |
| 489 | + mock_request.assert_called_once_with( |
| 490 | + method="POST", |
| 491 | + url="https://api.temp-mail.io/v1/emails", |
| 492 | + params=None, |
| 493 | + json={ "email": "[email protected]"}, |
| 494 | + ) |
| 495 | + |
| 496 | + def test_context_manager(self): |
| 497 | + with patch("tempmail.client.httpx.Client.close") as mock_close: |
| 498 | + with TempMailClient("test-api-key") as client: |
| 499 | + assert isinstance(client, TempMailClient) |
| 500 | + mock_close.assert_called_once() |
| 501 | + |
| 502 | + def test_close_method(self): |
| 503 | + with patch("tempmail.client.httpx.Client.close") as mock_close: |
| 504 | + client = TempMailClient("test-api-key") |
| 505 | + client.close() |
| 506 | + mock_close.assert_called_once() |
| 507 | + |
| 508 | + @patch("tempmail.client.httpx.Client.request") |
| 509 | + def test_create_email_with_empty_json_data(self, mock_request): |
| 510 | + mock_response = Mock() |
| 511 | + mock_response.status_code = 200 |
| 512 | + mock_response. json. return_value = { "email": "[email protected]", "ttl": 86400} |
| 513 | + mock_response.headers = self._rate_limit_headers |
| 514 | + mock_request.return_value = mock_response |
| 515 | + |
| 516 | + client = TempMailClient("test-api-key") |
| 517 | + email = client.create_email() |
| 518 | + assert email == EmailAddress( email="[email protected]", ttl=86400) |
| 519 | + |
| 520 | + mock_request.assert_called_once_with( |
| 521 | + method="POST", |
| 522 | + url="https://api.temp-mail.io/v1/emails", |
| 523 | + params=None, |
| 524 | + json=None, |
| 525 | + ) |
| 526 | + |
| 527 | + def test_last_rate_limit_initial_state(self): |
| 528 | + client = TempMailClient("test-api-key") |
| 529 | + assert client.last_rate_limit is None |
| 530 | + |
| 531 | + @patch("tempmail.client.httpx.Client.request") |
| 532 | + def test_error_response_with_different_status_codes(self, mock_request): |
| 533 | + mock_response = Mock() |
| 534 | + mock_response.status_code = 404 |
| 535 | + mock_response.json.return_value = { |
| 536 | + "error": { |
| 537 | + "code": "not_found", |
| 538 | + "detail": "Message not found", |
| 539 | + "type": "request_error", |
| 540 | + }, |
| 541 | + "meta": {"request_id": "123"}, |
| 542 | + } |
| 543 | + mock_response.headers = {} |
| 544 | + mock_request.return_value = mock_response |
| 545 | + |
| 546 | + client = TempMailClient("test-api-key") |
| 547 | + with pytest.raises(TempMailError, match="Message not found"): |
| 548 | + client.get_message("non-existent-id") |
| 549 | + |
| 550 | + def test_httpx_client_timeout_configuration(self): |
| 551 | + client = TempMailClient("test-api-key", timeout=60) |
| 552 | + # httpx.Client.timeout returns a Timeout object |
| 553 | + assert client.client.timeout.read == 60 |
| 554 | + assert client.client.timeout.connect == 60 |
| 555 | + |
| 556 | + @patch("tempmail.client.httpx.Client.request") |
| 557 | + def test_httpx_specific_error_handling(self, mock_request): |
| 558 | + mock_request.side_effect = TimeoutException("Request timeout") |
| 559 | + |
| 560 | + client = TempMailClient("test-api-key") |
| 561 | + with pytest.raises(TempMailError, match="Request failed"): |
| 562 | + client.list_domains() |
0 commit comments