Skip to content

Commit 0ca4012

Browse files
author
Robert Segal
committed
Added Accounts accounts e2e CRUD and state endpoints tests
1 parent fde5e48 commit 0ca4012

11 files changed

Lines changed: 474 additions & 28 deletions

File tree

e2e_config.test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"catalog.product.id": "PRD-7255-3950",
33
"accounts.seller.id": "SEL-7310-3075",
4-
"catalog.product.parameter_group.id": "PGR-7255-3950-0001"
4+
"catalog.product.parameter_group.id": "PGR-7255-3950-0001",
5+
"accounts.account.id": "ACC-9042-0088"
56
}

mpt_api_client/resources/accounts/account.py

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from typing import override
2+
13
from mpt_api_client.http import AsyncService, Service
24
from mpt_api_client.http.mixins import (
35
AsyncCollectionMixin,
4-
AsyncCreateMixin,
6+
AsyncCreateWithIconMixin,
57
AsyncGetMixin,
6-
AsyncUpdateMixin,
8+
AsyncUpdateWithIconMixin,
79
CollectionMixin,
8-
CreateMixin,
10+
CreateWithIconMixin,
911
GetMixin,
10-
UpdateMixin,
12+
UpdateWithIconMixin,
1113
)
14+
from mpt_api_client.http.types import FileTypes
1215
from mpt_api_client.models import Model
16+
from mpt_api_client.models.model import ResourceData
1317
from mpt_api_client.resources.accounts.accounts_users import (
1418
AccountsUsersService,
1519
AsyncAccountsUsersService,
@@ -31,14 +35,14 @@ class Account(Model):
3135
class AccountsServiceConfig:
3236
"""Accounts service configuration."""
3337

34-
_endpoint = "/public/v1/accounts"
38+
_endpoint = "/public/v1/accounts/accounts"
3539
_model_class = Account
3640
_collection_key = "data"
3741

3842

3943
class AccountsService(
40-
CreateMixin[Account],
41-
UpdateMixin[Account],
44+
CreateWithIconMixin[Account],
45+
UpdateWithIconMixin[Account],
4246
ActivatableMixin[Account],
4347
EnablableMixin[Account],
4448
ValidateMixin[Account],
@@ -49,6 +53,63 @@ class AccountsService(
4953
):
5054
"""Accounts service."""
5155

56+
@override
57+
def create(
58+
self,
59+
resource_data: ResourceData,
60+
logo: FileTypes,
61+
data_key: str = "account",
62+
icon_key: str = "logo",
63+
) -> Account:
64+
"""
65+
Create a new account with logo.
66+
67+
Args:
68+
resource_data (ResourceData): Account data.
69+
logo: Logo image in jpg, png, GIF, etc.
70+
data_key: Key for the account data.
71+
icon_key: Key for the logo.
72+
73+
Returns:
74+
Account: The created account.
75+
"""
76+
return super().create(
77+
resource_data=resource_data,
78+
icon=logo,
79+
data_key=data_key,
80+
icon_key=icon_key,
81+
)
82+
83+
@override
84+
def update(
85+
self,
86+
resource_id: str,
87+
resource_data: ResourceData,
88+
logo: FileTypes,
89+
data_key: str = "account",
90+
icon_key: str = "logo",
91+
) -> Account:
92+
"""
93+
Update an existing account with logo.
94+
95+
Args:
96+
resource_id (str): The ID of the account to update.
97+
resource_data (ResourceData): Account data.
98+
logo: Logo image in jpg, png, GIF, etc.
99+
data_key: Key for the account data.
100+
icon_key: Key for the logo.
101+
102+
Returns:
103+
Account: The updated account.
104+
"""
105+
return super().update(
106+
resource_id=resource_id,
107+
resource_data=resource_data,
108+
icon=logo,
109+
data_key=data_key,
110+
icon_key=icon_key,
111+
)
112+
52113
def users(self, account_id: str) -> AccountsUsersService:
53114
"""Return account users service."""
54115
return AccountsUsersService(
@@ -57,8 +118,8 @@ def users(self, account_id: str) -> AccountsUsersService:
57118

58119

59120
class AsyncAccountsService(
60-
AsyncCreateMixin[Account],
61-
AsyncUpdateMixin[Account],
121+
AsyncCreateWithIconMixin[Account],
122+
AsyncUpdateWithIconMixin[Account],
62123
AsyncActivatableMixin[Account],
63124
AsyncEnablableMixin[Account],
64125
AsyncValidateMixin[Account],
@@ -69,6 +130,63 @@ class AsyncAccountsService(
69130
):
70131
"""Async Accounts service."""
71132

133+
@override
134+
async def create(
135+
self,
136+
resource_data: ResourceData,
137+
logo: FileTypes,
138+
data_key: str = "account",
139+
icon_key: str = "logo",
140+
) -> Account:
141+
"""
142+
Create a new account with logo.
143+
144+
Args:
145+
resource_data (ResourceData): Account data.
146+
logo: Logo image in jpg, png, GIF, etc.
147+
data_key: Key for the account data.
148+
icon_key: Key for the logo.
149+
150+
Returns:
151+
Account: The created account.
152+
"""
153+
return await super().create(
154+
resource_data=resource_data,
155+
icon=logo,
156+
data_key=data_key,
157+
icon_key=icon_key,
158+
)
159+
160+
@override
161+
async def update(
162+
self,
163+
resource_id: str,
164+
resource_data: ResourceData,
165+
logo: FileTypes,
166+
data_key: str = "account",
167+
icon_key: str = "logo",
168+
) -> Account:
169+
"""
170+
Update an existing account with logo.
171+
172+
Args:
173+
resource_id (str): The ID of the account to update.
174+
resource_data (ResourceData): Account data.
175+
logo: Logo image in jpg, png, GIF, etc.
176+
data_key: Key for the account data.
177+
icon_key: Key for the logo.
178+
179+
Returns:
180+
Account: The updated account.
181+
"""
182+
return await super().update(
183+
resource_id=resource_id,
184+
resource_data=resource_data,
185+
icon=logo,
186+
data_key=data_key,
187+
icon_key=icon_key,
188+
)
189+
72190
def users(self, account_id: str) -> AsyncAccountsUsersService:
73191
"""Return account users service."""
74192
return AsyncAccountsUsersService(
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import pytest
2+
3+
from mpt_api_client.exceptions import MPTAPIError
4+
from mpt_api_client.rql.query_builder import RQLQuery
5+
6+
pytestmark = [pytest.mark.flaky]
7+
8+
9+
@pytest.fixture
10+
async def async_created_account(logger, async_mpt_ops, account, account_icon):
11+
account_data = account()
12+
13+
res_account = await async_mpt_ops.accounts.accounts.create(account_data, logo=account_icon)
14+
15+
yield res_account
16+
17+
try:
18+
await async_mpt_ops.accounts.accounts.deactivate(res_account.id)
19+
except MPTAPIError as error:
20+
print("TEARDOWN - Unable to deactivate account: %s", error.title) # noqa: WPS421
21+
22+
23+
async def test_get_account_by_id_not_found(async_mpt_ops):
24+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
25+
await async_mpt_ops.accounts.accounts.get("INVALID-ID")
26+
27+
28+
async def test_get_account_by_id(async_mpt_ops, account_id):
29+
account = await async_mpt_ops.accounts.accounts.get(account_id)
30+
assert account is not None
31+
32+
33+
async def test_list_accounts(async_mpt_ops):
34+
limit = 10
35+
accounts_page = await async_mpt_ops.accounts.accounts.fetch_page(limit=limit)
36+
assert len(accounts_page) > 0
37+
38+
39+
def test_create_account(async_created_account):
40+
account = async_created_account
41+
assert account is not None
42+
43+
44+
async def test_update_account(async_mpt_ops, async_created_account, account, account_icon):
45+
updated_data = account(name="Updated Account Name")
46+
47+
updated_account = await async_mpt_ops.accounts.accounts.update(
48+
async_created_account.id, updated_data, logo=account_icon
49+
)
50+
51+
assert updated_account is not None
52+
53+
54+
async def test_update_account_invalid_data(
55+
async_mpt_ops, account, async_created_account, account_icon
56+
):
57+
updated_data = account(name="")
58+
59+
with pytest.raises(MPTAPIError, match=r"400 Bad Request"):
60+
await async_mpt_ops.accounts.accounts.update(
61+
async_created_account.id, updated_data, logo=account_icon
62+
)
63+
64+
65+
async def test_update_account_not_found(async_mpt_ops, account, invalid_account_id, account_icon):
66+
non_existent_account = account(name="Non Existent Account")
67+
68+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
69+
await async_mpt_ops.accounts.accounts.update(
70+
invalid_account_id, non_existent_account, logo=account_icon
71+
)
72+
73+
74+
async def test_account_enable(async_mpt_ops, account, async_created_account):
75+
await async_mpt_ops.accounts.accounts.disable(async_created_account.id)
76+
77+
account = await async_mpt_ops.accounts.accounts.enable(async_created_account.id)
78+
79+
assert account is not None
80+
81+
82+
async def test_account_enable_not_found(async_mpt_ops, invalid_account_id):
83+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
84+
await async_mpt_ops.accounts.accounts.enable(invalid_account_id)
85+
86+
87+
async def test_account_disable(async_mpt_ops, async_created_account):
88+
account = await async_mpt_ops.accounts.accounts.disable(async_created_account.id)
89+
90+
assert account is not None
91+
92+
93+
async def test_account_disable_not_found(async_mpt_ops, invalid_account_id):
94+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
95+
await async_mpt_ops.accounts.accounts.disable(invalid_account_id)
96+
97+
98+
async def test_account_rql_filter(async_mpt_ops, account_id):
99+
selected_fields = ["-address"]
100+
filtered_accounts = (
101+
async_mpt_ops.accounts.accounts.filter(RQLQuery(id=account_id))
102+
.filter(RQLQuery(name="Test Api Client Vendor"))
103+
.select(*selected_fields)
104+
)
105+
106+
accounts = [account async for account in filtered_accounts.iterate()]
107+
108+
assert len(accounts) > 0

0 commit comments

Comments
 (0)