Skip to content

Commit 316c429

Browse files
committed
add sub account deletion to SubAccountsApi
1 parent 2e580f4 commit 316c429

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎mailtrap/api/resources/sub_accounts.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import Optional
2+
13
from mailtrap.http import HttpClient
4+
from mailtrap.models.common import DeletedObject
25
from mailtrap.models.organizations import CreateSubAccountParams
36
from mailtrap.models.organizations import SubAccount
47

@@ -27,5 +30,23 @@ def create(self, sub_account_params: CreateSubAccountParams) -> SubAccount:
2730
)
2831
return SubAccount(**response)
2932

30-
def _api_path(self) -> str:
31-
return f"/api/organizations/{self._organization_id}/sub_accounts"
33+
def delete(self, sub_account_id: int) -> DeletedObject:
34+
"""
35+
Delete a sub account of the organization. Requires sub account
36+
management permissions for this organization.
37+
38+
The sub account and all of its data are removed permanently and cannot
39+
be restored. Deleting the organization's last sub account also deletes
40+
the organization. A repeated call for the same sub account returns
41+
a 404 error.
42+
43+
Rate limit: 10 requests per minute per organization.
44+
"""
45+
self._client.delete(self._api_path(sub_account_id))
46+
return DeletedObject(id=sub_account_id)
47+
48+
def _api_path(self, sub_account_id: Optional[int] = None) -> str:
49+
path = f"/api/organizations/{self._organization_id}/sub_accounts"
50+
if sub_account_id is not None:
51+
return f"{path}/{sub_account_id}"
52+
return path

‎tests/unit/api/organizations/test_sub_accounts.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from mailtrap.config import GENERAL_HOST
88
from mailtrap.exceptions import APIError
99
from mailtrap.http import HttpClient
10+
from mailtrap.models.common import DeletedObject
1011
from mailtrap.models.organizations import CreateSubAccountParams
1112
from mailtrap.models.organizations import SubAccount
1213
from tests import conftest
@@ -152,3 +153,59 @@ def test_create_should_return_sub_account_and_wrap_body_under_account_key(
152153
responses.calls[0].request.body
153154
== b'{"account": {"name": "New Team Account"}}'
154155
)
156+
157+
@pytest.mark.parametrize(
158+
"status_code,response_json,expected_error_message",
159+
[
160+
(
161+
conftest.UNAUTHORIZED_STATUS_CODE,
162+
conftest.UNAUTHORIZED_RESPONSE,
163+
conftest.UNAUTHORIZED_ERROR_MESSAGE,
164+
),
165+
(
166+
conftest.FORBIDDEN_STATUS_CODE,
167+
conftest.FORBIDDEN_RESPONSE,
168+
conftest.FORBIDDEN_ERROR_MESSAGE,
169+
),
170+
(
171+
conftest.NOT_FOUND_STATUS_CODE,
172+
conftest.NOT_FOUND_RESPONSE,
173+
conftest.NOT_FOUND_ERROR_MESSAGE,
174+
),
175+
(
176+
conftest.RATE_LIMIT_ERROR_STATUS_CODE,
177+
conftest.RATE_LIMIT_ERROR_RESPONSE,
178+
conftest.RATE_LIMIT_ERROR_MESSAGE,
179+
),
180+
],
181+
)
182+
@responses.activate
183+
def test_delete_should_raise_api_errors(
184+
self,
185+
client: SubAccountsApi,
186+
status_code: int,
187+
response_json: dict,
188+
expected_error_message: str,
189+
) -> None:
190+
responses.delete(
191+
f"{BASE_SUB_ACCOUNTS_URL}/{SUB_ACCOUNT_ID}",
192+
status=status_code,
193+
json=response_json,
194+
)
195+
196+
with pytest.raises(APIError) as exc_info:
197+
client.delete(SUB_ACCOUNT_ID)
198+
199+
assert expected_error_message in str(exc_info.value)
200+
201+
@responses.activate
202+
def test_delete_should_return_deleted_object(self, client: SubAccountsApi) -> None:
203+
responses.delete(
204+
f"{BASE_SUB_ACCOUNTS_URL}/{SUB_ACCOUNT_ID}",
205+
status=204,
206+
)
207+
208+
result = client.delete(SUB_ACCOUNT_ID)
209+
210+
assert isinstance(result, DeletedObject)
211+
assert result.id == SUB_ACCOUNT_ID

0 commit comments

Comments
 (0)