|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | + |
| 6 | +from mailtrap.api.resources.suppressions import SuppressionsApi |
| 7 | +from mailtrap.config import GENERAL_HOST |
| 8 | +from mailtrap.exceptions import APIError |
| 9 | +from mailtrap.http import HttpClient |
| 10 | +from mailtrap.models.suppressions import SendingStream |
| 11 | +from mailtrap.models.suppressions import Suppression |
| 12 | +from mailtrap.models.suppressions import SuppressionType |
| 13 | +from tests import conftest |
| 14 | + |
| 15 | +ACCOUNT_ID = "321" |
| 16 | +SUPPRESSION_ID = "supp_123456" |
| 17 | +BASE_SUPPRESSIONS_URL = f"https://{GENERAL_HOST}/api/accounts/{ACCOUNT_ID}/suppressions" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def suppressions_api() -> SuppressionsApi: |
| 22 | + return SuppressionsApi(account_id=ACCOUNT_ID, client=HttpClient(GENERAL_HOST)) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def sample_suppression_dict() -> dict[str, Any]: |
| 27 | + return { |
| 28 | + "id": SUPPRESSION_ID, |
| 29 | + "type": "hard bounce", |
| 30 | + "created_at": "2023-01-01T10:00:00Z", |
| 31 | + |
| 32 | + "sending_stream": "transactional", |
| 33 | + "domain_name": "example.com", |
| 34 | + "message_bounce_category": "invalid_recipient", |
| 35 | + "message_category": "bounce", |
| 36 | + "message_client_ip": "192.168.1.1", |
| 37 | + "message_created_at": "2023-01-01T09:59:00Z", |
| 38 | + "message_esp_response": "550 5.1.1 User unknown", |
| 39 | + "message_esp_server_type": "postfix", |
| 40 | + "message_outgoing_ip": "10.0.0.1", |
| 41 | + "message_recipient_mx_name": "mx.example.com", |
| 42 | + "message_sender_email": "[email protected]", |
| 43 | + "message_subject": "Test Email", |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +class TestSuppressionsApi: |
| 48 | + |
| 49 | + @pytest.mark.parametrize( |
| 50 | + "status_code,response_json,expected_error_message", |
| 51 | + [ |
| 52 | + ( |
| 53 | + conftest.UNAUTHORIZED_STATUS_CODE, |
| 54 | + conftest.UNAUTHORIZED_RESPONSE, |
| 55 | + conftest.UNAUTHORIZED_ERROR_MESSAGE, |
| 56 | + ), |
| 57 | + ( |
| 58 | + conftest.FORBIDDEN_STATUS_CODE, |
| 59 | + conftest.FORBIDDEN_RESPONSE, |
| 60 | + conftest.FORBIDDEN_ERROR_MESSAGE, |
| 61 | + ), |
| 62 | + ], |
| 63 | + ) |
| 64 | + @responses.activate |
| 65 | + def test_get_suppressions_should_raise_api_errors( |
| 66 | + self, |
| 67 | + suppressions_api: SuppressionsApi, |
| 68 | + status_code: int, |
| 69 | + response_json: dict, |
| 70 | + expected_error_message: str, |
| 71 | + ) -> None: |
| 72 | + responses.get( |
| 73 | + BASE_SUPPRESSIONS_URL, |
| 74 | + status=status_code, |
| 75 | + json=response_json, |
| 76 | + ) |
| 77 | + |
| 78 | + with pytest.raises(APIError) as exc_info: |
| 79 | + suppressions_api.get_list() |
| 80 | + |
| 81 | + assert expected_error_message in str(exc_info.value) |
| 82 | + |
| 83 | + @responses.activate |
| 84 | + def test_get_suppressions_should_return_suppression_list( |
| 85 | + self, suppressions_api: SuppressionsApi, sample_suppression_dict: dict |
| 86 | + ) -> None: |
| 87 | + responses.get( |
| 88 | + BASE_SUPPRESSIONS_URL, |
| 89 | + json=[sample_suppression_dict], |
| 90 | + status=200, |
| 91 | + ) |
| 92 | + |
| 93 | + suppressions = suppressions_api.get_list() |
| 94 | + |
| 95 | + assert isinstance(suppressions, list) |
| 96 | + assert all(isinstance(s, Suppression) for s in suppressions) |
| 97 | + assert suppressions[0].id == SUPPRESSION_ID |
| 98 | + |
| 99 | + @responses.activate |
| 100 | + def test_get_suppressions_with_email_filter_should_return_filtered_list( |
| 101 | + self, suppressions_api: SuppressionsApi, sample_suppression_dict: dict |
| 102 | + ) -> None: |
| 103 | + email_filter = "[email protected]" |
| 104 | + responses.get( |
| 105 | + BASE_SUPPRESSIONS_URL, |
| 106 | + json=[sample_suppression_dict], |
| 107 | + status=200, |
| 108 | + ) |
| 109 | + |
| 110 | + suppressions = suppressions_api.get_list(email=email_filter) |
| 111 | + |
| 112 | + assert isinstance(suppressions, list) |
| 113 | + assert all(isinstance(s, Suppression) for s in suppressions) |
| 114 | + assert suppressions[0].id == SUPPRESSION_ID |
| 115 | + assert suppressions[0].email == email_filter |
| 116 | + |
| 117 | + @pytest.mark.parametrize( |
| 118 | + "status_code,response_json,expected_error_message", |
| 119 | + [ |
| 120 | + ( |
| 121 | + conftest.UNAUTHORIZED_STATUS_CODE, |
| 122 | + conftest.UNAUTHORIZED_RESPONSE, |
| 123 | + conftest.UNAUTHORIZED_ERROR_MESSAGE, |
| 124 | + ), |
| 125 | + ( |
| 126 | + conftest.FORBIDDEN_STATUS_CODE, |
| 127 | + conftest.FORBIDDEN_RESPONSE, |
| 128 | + conftest.FORBIDDEN_ERROR_MESSAGE, |
| 129 | + ), |
| 130 | + ( |
| 131 | + conftest.NOT_FOUND_STATUS_CODE, |
| 132 | + conftest.NOT_FOUND_RESPONSE, |
| 133 | + conftest.NOT_FOUND_ERROR_MESSAGE, |
| 134 | + ), |
| 135 | + ], |
| 136 | + ) |
| 137 | + @responses.activate |
| 138 | + def test_delete_suppression_should_raise_api_errors( |
| 139 | + self, |
| 140 | + suppressions_api: SuppressionsApi, |
| 141 | + status_code: int, |
| 142 | + response_json: dict, |
| 143 | + expected_error_message: str, |
| 144 | + ) -> None: |
| 145 | + responses.delete( |
| 146 | + f"{BASE_SUPPRESSIONS_URL}/{SUPPRESSION_ID}", |
| 147 | + status=status_code, |
| 148 | + json=response_json, |
| 149 | + ) |
| 150 | + |
| 151 | + with pytest.raises(APIError) as exc_info: |
| 152 | + suppressions_api.delete(SUPPRESSION_ID) |
| 153 | + |
| 154 | + assert expected_error_message in str(exc_info.value) |
| 155 | + |
| 156 | + @responses.activate |
| 157 | + def test_delete_suppression_should_return_deleted_suppression( |
| 158 | + self, suppressions_api: SuppressionsApi, sample_suppression_dict: dict |
| 159 | + ) -> None: |
| 160 | + responses.delete( |
| 161 | + f"{BASE_SUPPRESSIONS_URL}/{SUPPRESSION_ID}", |
| 162 | + json=sample_suppression_dict, |
| 163 | + status=200, |
| 164 | + ) |
| 165 | + |
| 166 | + deleted_suppression = suppressions_api.delete(SUPPRESSION_ID) |
| 167 | + |
| 168 | + assert isinstance(deleted_suppression, Suppression) |
| 169 | + assert deleted_suppression.id == SUPPRESSION_ID |
| 170 | + assert deleted_suppression.type == SuppressionType.HARD_BOUNCE |
| 171 | + assert deleted_suppression. email == "[email protected]" |
| 172 | + assert deleted_suppression.sending_stream == SendingStream.TRANSACTIONAL |
0 commit comments