|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | + |
| 6 | +from mailtrap.api.resources.contact_events import ContactEventsApi |
| 7 | +from mailtrap.config import GENERAL_HOST |
| 8 | +from mailtrap.exceptions import APIError |
| 9 | +from mailtrap.http import HttpClient |
| 10 | +from mailtrap.models.contacts import ContactEvent |
| 11 | +from mailtrap.models.contacts import ContactEventParams |
| 12 | +from tests import conftest |
| 13 | + |
| 14 | +ACCOUNT_ID = "321" |
| 15 | +EXPORT_ID = 1 |
| 16 | +CONTACT_IDENTIFIER = "d82d0d9e-bbb7-4656-a591-e64682bffae7" |
| 17 | +BASE_CONTACT_EVENTS_URL = ( |
| 18 | + f"https://{GENERAL_HOST}/api/accounts/{ACCOUNT_ID}" |
| 19 | + f"/contacts/{CONTACT_IDENTIFIER}/events" |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def client() -> ContactEventsApi: |
| 25 | + return ContactEventsApi(account_id=ACCOUNT_ID, client=HttpClient(GENERAL_HOST)) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def sample_contact_event_dict() -> dict[str, Any]: |
| 30 | + return { |
| 31 | + "contact_id": CONTACT_IDENTIFIER, |
| 32 | + "contact_email": "[email protected]", |
| 33 | + "name": "UserLogin", |
| 34 | + "params": { |
| 35 | + "user_id": 101, |
| 36 | + "user_name": "John Smith", |
| 37 | + "is_active": True, |
| 38 | + "last_seen": None, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def sample_create_contact_event_params() -> ContactEventParams: |
| 45 | + return ContactEventParams( |
| 46 | + name="UserLogin", |
| 47 | + params={ |
| 48 | + "user_id": 101, |
| 49 | + "user_name": "John Smith", |
| 50 | + "is_active": True, |
| 51 | + "last_seen": None, |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +class TestContactEventsApi: |
| 57 | + |
| 58 | + @pytest.mark.parametrize( |
| 59 | + "status_code,response_json,expected_error_message", |
| 60 | + [ |
| 61 | + ( |
| 62 | + conftest.UNAUTHORIZED_STATUS_CODE, |
| 63 | + conftest.UNAUTHORIZED_RESPONSE, |
| 64 | + conftest.UNAUTHORIZED_ERROR_MESSAGE, |
| 65 | + ), |
| 66 | + ( |
| 67 | + conftest.FORBIDDEN_STATUS_CODE, |
| 68 | + conftest.FORBIDDEN_RESPONSE, |
| 69 | + conftest.FORBIDDEN_ERROR_MESSAGE, |
| 70 | + ), |
| 71 | + ( |
| 72 | + conftest.NOT_FOUND_STATUS_CODE, |
| 73 | + conftest.NOT_FOUND_RESPONSE, |
| 74 | + conftest.NOT_FOUND_ERROR_MESSAGE, |
| 75 | + ), |
| 76 | + ( |
| 77 | + conftest.RATE_LIMIT_ERROR_STATUS_CODE, |
| 78 | + conftest.RATE_LIMIT_ERROR_RESPONSE, |
| 79 | + conftest.RATE_LIMIT_ERROR_MESSAGE, |
| 80 | + ), |
| 81 | + ( |
| 82 | + conftest.INTERNAL_SERVER_ERROR_STATUS_CODE, |
| 83 | + conftest.INTERNAL_SERVER_ERROR_RESPONSE, |
| 84 | + conftest.INTERNAL_SERVER_ERROR_MESSAGE, |
| 85 | + ), |
| 86 | + ( |
| 87 | + 422, |
| 88 | + { |
| 89 | + "errors": { |
| 90 | + "name": [["must be a string", "is too long"]], |
| 91 | + "params": [ |
| 92 | + [ |
| 93 | + "must be a hash", |
| 94 | + "key 'foo' is too long", |
| 95 | + "value for 'bar' is too long", |
| 96 | + ] |
| 97 | + ], |
| 98 | + } |
| 99 | + }, |
| 100 | + ( |
| 101 | + "name: ['must be a string', 'is too long']; " |
| 102 | + "params: ['must be a hash', \"key 'foo' is too long\", " |
| 103 | + "\"value for 'bar' is too long\"]" |
| 104 | + ), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ) |
| 108 | + @responses.activate |
| 109 | + def test_create_should_raise_api_errors( |
| 110 | + self, |
| 111 | + client: ContactEventsApi, |
| 112 | + status_code: int, |
| 113 | + response_json: dict, |
| 114 | + expected_error_message: str, |
| 115 | + sample_create_contact_event_params: ContactEventParams, |
| 116 | + ) -> None: |
| 117 | + responses.post( |
| 118 | + BASE_CONTACT_EVENTS_URL, |
| 119 | + status=status_code, |
| 120 | + json=response_json, |
| 121 | + ) |
| 122 | + |
| 123 | + with pytest.raises(APIError) as exc_info: |
| 124 | + client.create(CONTACT_IDENTIFIER, sample_create_contact_event_params) |
| 125 | + |
| 126 | + print(str(exc_info.value)) |
| 127 | + |
| 128 | + assert expected_error_message in str(exc_info.value) |
| 129 | + |
| 130 | + @responses.activate |
| 131 | + def test_create_should_return_contact_event( |
| 132 | + self, |
| 133 | + client: ContactEventsApi, |
| 134 | + sample_contact_event_dict: dict, |
| 135 | + sample_create_contact_event_params: ContactEventParams, |
| 136 | + ) -> None: |
| 137 | + responses.post( |
| 138 | + BASE_CONTACT_EVENTS_URL, |
| 139 | + json=sample_contact_event_dict, |
| 140 | + status=201, |
| 141 | + ) |
| 142 | + |
| 143 | + result = client.create(CONTACT_IDENTIFIER, sample_create_contact_event_params) |
| 144 | + |
| 145 | + assert isinstance(result, ContactEvent) |
| 146 | + assert result.contact_id == CONTACT_IDENTIFIER |
| 147 | + assert result. contact_email == "[email protected]" |
| 148 | + assert result.name == "UserLogin" |
| 149 | + request = responses.calls[0].request |
| 150 | + assert request.url == BASE_CONTACT_EVENTS_URL |
0 commit comments