|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +import responses |
| 5 | + |
| 6 | +from mailtrap.api.resources.sub_accounts import SubAccountsApi |
| 7 | +from mailtrap.config import GENERAL_HOST |
| 8 | +from mailtrap.exceptions import APIError |
| 9 | +from mailtrap.http import HttpClient |
| 10 | +from mailtrap.models.organizations import CreateSubAccountParams |
| 11 | +from mailtrap.models.organizations import SubAccount |
| 12 | +from tests import conftest |
| 13 | + |
| 14 | +ORGANIZATION_ID = "1001" |
| 15 | +SUB_ACCOUNT_ID = 12345 |
| 16 | +BASE_SUB_ACCOUNTS_URL = ( |
| 17 | + f"https://{GENERAL_HOST}/api/organizations/{ORGANIZATION_ID}/sub_accounts" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def client() -> SubAccountsApi: |
| 23 | + return SubAccountsApi( |
| 24 | + client=HttpClient(GENERAL_HOST), organization_id=ORGANIZATION_ID |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def sample_sub_account_dict() -> dict[str, Any]: |
| 30 | + return {"id": SUB_ACCOUNT_ID, "name": "Development Team Account"} |
| 31 | + |
| 32 | + |
| 33 | +class TestSubAccountsApi: |
| 34 | + |
| 35 | + @pytest.mark.parametrize( |
| 36 | + "status_code,response_json,expected_error_message", |
| 37 | + [ |
| 38 | + ( |
| 39 | + conftest.UNAUTHORIZED_STATUS_CODE, |
| 40 | + conftest.UNAUTHORIZED_RESPONSE, |
| 41 | + conftest.UNAUTHORIZED_ERROR_MESSAGE, |
| 42 | + ), |
| 43 | + ( |
| 44 | + conftest.FORBIDDEN_STATUS_CODE, |
| 45 | + conftest.FORBIDDEN_RESPONSE, |
| 46 | + conftest.FORBIDDEN_ERROR_MESSAGE, |
| 47 | + ), |
| 48 | + ], |
| 49 | + ) |
| 50 | + @responses.activate |
| 51 | + def test_get_list_should_raise_api_errors( |
| 52 | + self, |
| 53 | + client: SubAccountsApi, |
| 54 | + status_code: int, |
| 55 | + response_json: dict, |
| 56 | + expected_error_message: str, |
| 57 | + ) -> None: |
| 58 | + responses.get( |
| 59 | + BASE_SUB_ACCOUNTS_URL, |
| 60 | + status=status_code, |
| 61 | + json=response_json, |
| 62 | + ) |
| 63 | + |
| 64 | + with pytest.raises(APIError) as exc_info: |
| 65 | + client.get_list() |
| 66 | + |
| 67 | + assert expected_error_message in str(exc_info.value) |
| 68 | + |
| 69 | + @responses.activate |
| 70 | + def test_get_list_should_return_sub_accounts_list( |
| 71 | + self, client: SubAccountsApi, sample_sub_account_dict: dict |
| 72 | + ) -> None: |
| 73 | + responses.get( |
| 74 | + BASE_SUB_ACCOUNTS_URL, |
| 75 | + json=[ |
| 76 | + sample_sub_account_dict, |
| 77 | + {"id": 12346, "name": "QA Team Account"}, |
| 78 | + ], |
| 79 | + status=200, |
| 80 | + ) |
| 81 | + |
| 82 | + sub_accounts = client.get_list() |
| 83 | + |
| 84 | + assert isinstance(sub_accounts, list) |
| 85 | + assert all(isinstance(s, SubAccount) for s in sub_accounts) |
| 86 | + assert len(sub_accounts) == 2 |
| 87 | + assert sub_accounts[0].id == SUB_ACCOUNT_ID |
| 88 | + assert sub_accounts[0].name == "Development Team Account" |
| 89 | + |
| 90 | + @responses.activate |
| 91 | + def test_get_list_should_return_empty_list(self, client: SubAccountsApi) -> None: |
| 92 | + responses.get(BASE_SUB_ACCOUNTS_URL, json=[], status=200) |
| 93 | + |
| 94 | + sub_accounts = client.get_list() |
| 95 | + |
| 96 | + assert isinstance(sub_accounts, list) |
| 97 | + assert len(sub_accounts) == 0 |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "status_code,response_json,expected_error_message", |
| 101 | + [ |
| 102 | + ( |
| 103 | + conftest.UNAUTHORIZED_STATUS_CODE, |
| 104 | + conftest.UNAUTHORIZED_RESPONSE, |
| 105 | + conftest.UNAUTHORIZED_ERROR_MESSAGE, |
| 106 | + ), |
| 107 | + ( |
| 108 | + conftest.FORBIDDEN_STATUS_CODE, |
| 109 | + conftest.FORBIDDEN_RESPONSE, |
| 110 | + conftest.FORBIDDEN_ERROR_MESSAGE, |
| 111 | + ), |
| 112 | + ( |
| 113 | + conftest.VALIDATION_ERRORS_STATUS_CODE, |
| 114 | + {"errors": "Name is invalid"}, |
| 115 | + "Name is invalid", |
| 116 | + ), |
| 117 | + ], |
| 118 | + ) |
| 119 | + @responses.activate |
| 120 | + def test_create_should_raise_api_errors( |
| 121 | + self, |
| 122 | + client: SubAccountsApi, |
| 123 | + status_code: int, |
| 124 | + response_json: dict, |
| 125 | + expected_error_message: str, |
| 126 | + ) -> None: |
| 127 | + responses.post( |
| 128 | + BASE_SUB_ACCOUNTS_URL, status=status_code, json=response_json |
| 129 | + ) |
| 130 | + |
| 131 | + with pytest.raises(APIError) as exc_info: |
| 132 | + client.create(CreateSubAccountParams(name="New Team Account")) |
| 133 | + |
| 134 | + assert expected_error_message in str(exc_info.value) |
| 135 | + |
| 136 | + @responses.activate |
| 137 | + def test_create_should_return_sub_account_and_wrap_body_under_account_key( |
| 138 | + self, client: SubAccountsApi, sample_sub_account_dict: dict |
| 139 | + ) -> None: |
| 140 | + responses.post( |
| 141 | + BASE_SUB_ACCOUNTS_URL, |
| 142 | + json={"id": 12347, "name": "New Team Account"}, |
| 143 | + status=200, |
| 144 | + ) |
| 145 | + |
| 146 | + sub_account = client.create( |
| 147 | + CreateSubAccountParams(name="New Team Account") |
| 148 | + ) |
| 149 | + |
| 150 | + assert isinstance(sub_account, SubAccount) |
| 151 | + assert sub_account.id == 12347 |
| 152 | + assert sub_account.name == "New Team Account" |
| 153 | + |
| 154 | + assert len(responses.calls) == 1 |
| 155 | + assert ( |
| 156 | + responses.calls[0].request.body |
| 157 | + == b'{"account": {"name": "New Team Account"}}' |
| 158 | + ) |
0 commit comments