Skip to content

Commit a5bfccc

Browse files
author
Robert Segal
committed
Added Accounts account by id users endpoints e2e tests
1 parent 3399c94 commit a5bfccc

File tree

8 files changed

+545
-6
lines changed

8 files changed

+545
-6
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
[flake8]
3+
per-file-ignores =
4+
tests/e2e/accounts/accounts_users/test_sync_accounts_users.py: WPS402,WPS421,AAA01
5+
test_sync_accounts_users.py: WPS402,WPS421,AAA01

e2e_config.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"accounts.module.id": "MOD-1756",
1010
"accounts.module.name": "Access Management",
1111
"accounts.seller.id": "SEL-7310-3075",
12+
"accounts.user.id": "USR-9673-3314",
1213
"accounts.user_group.id": "UGR-6822-0561",
1314
"catalog.product.item.id": "ITM-7255-3950-0751",
1415
"catalog.product.document.id": "PDC-7255-3950-0001",

mpt_api_client/resources/accounts/accounts_user_groups.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from mpt_api_client.http import AsyncService, Service
22
from mpt_api_client.http.mixins import (
33
AsyncCollectionMixin,
4-
AsyncManagedResourceMixin,
4+
AsyncCreateMixin,
5+
AsyncDeleteMixin,
6+
AsyncGetMixin,
57
CollectionMixin,
6-
ManagedResourceMixin,
8+
CreateMixin,
9+
DeleteMixin,
10+
GetMixin,
711
)
812
from mpt_api_client.models import Model
13+
from mpt_api_client.models.model import ResourceData
914

1015

1116
class AccountsUserGroup(Model):
@@ -21,18 +26,48 @@ class AccountsUserGroupsServiceConfig:
2126

2227

2328
class AccountsUserGroupsService(
24-
ManagedResourceMixin[AccountsUserGroup],
29+
GetMixin[AccountsUserGroup],
30+
CreateMixin[AccountsUserGroup],
31+
DeleteMixin,
2532
CollectionMixin[AccountsUserGroup],
2633
Service[AccountsUserGroup],
2734
AccountsUserGroupsServiceConfig,
2835
):
2936
"""Account User Groups Service."""
3037

38+
def update(self, resource_data: ResourceData) -> AccountsUserGroup:
39+
"""Update Account User Group.
40+
41+
Args:
42+
resource_data (ResourceData): Resource data to update.
43+
44+
Returns:
45+
AccountsUserGroup: Updated Account User Group.
46+
"""
47+
response = self.http_client.request("put", self.path, json=resource_data)
48+
49+
return self._model_class.from_response(response)
50+
3151

3252
class AsyncAccountsUserGroupsService(
33-
AsyncManagedResourceMixin[AccountsUserGroup],
53+
AsyncGetMixin[AccountsUserGroup],
54+
AsyncCreateMixin[AccountsUserGroup],
55+
AsyncDeleteMixin,
3456
AsyncCollectionMixin[AccountsUserGroup],
3557
AsyncService[AccountsUserGroup],
3658
AccountsUserGroupsServiceConfig,
3759
):
3860
"""Asynchronous Account User Groups Service."""
61+
62+
async def update(self, resource_data: ResourceData) -> AccountsUserGroup:
63+
"""Update Account User Group.
64+
65+
Args:
66+
resource_data (ResourceData): Resource data to update.
67+
68+
Returns:
69+
AccountsUserGroup: Updated Account User Group.
70+
"""
71+
response = await self.http_client.request("put", self.path, json=resource_data)
72+
73+
return self._model_class.from_response(response)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def invalid_user_id():
6+
return "USR-0000-0000"
7+
8+
9+
@pytest.fixture
10+
def account_user_factory(account_id, user_group_id, uuid_str):
11+
def _account_user( # noqa: WPS430
12+
email: str | None = None, # Must be unique in Marketplace
13+
first_name: str = "E2E Created",
14+
last_name: str = "Account User",
15+
):
16+
if not email:
17+
email = f"e2e_{uuid_str}@dummy.com"
18+
19+
return {
20+
"user": {
21+
"firstName": first_name,
22+
"lastName": last_name,
23+
"email": email,
24+
},
25+
"account": {
26+
"id": account_id,
27+
},
28+
"groups": [
29+
{"id": user_group_id},
30+
],
31+
"invitation": {
32+
"status": "Invited",
33+
},
34+
}
35+
36+
return _account_user
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# noqa: WPS204, WPS420, WPS421, WPS210, AAA01
2+
# noqa: WPS402
3+
import pytest
4+
5+
from mpt_api_client.exceptions import MPTAPIError
6+
from mpt_api_client.rql.query_builder import RQLQuery
7+
8+
pytestmark = [pytest.mark.flaky]
9+
10+
11+
@pytest.fixture
12+
def users(async_mpt_vendor, account_id):
13+
return async_mpt_vendor.accounts.accounts.users(account_id=account_id) # noqa: WPS204
14+
15+
16+
@pytest.fixture
17+
async def created_account_user(async_mpt_vendor, account_user_factory, account_id):
18+
"""Fixture to create and teardown an account user."""
19+
ret_account_user = None
20+
21+
async def _created_account_user(
22+
first_name: str = "E2E Created",
23+
last_name: str = "Account User",
24+
):
25+
"""Create an account user with the given first and last name."""
26+
nonlocal ret_account_user # noqa: WPS420
27+
account_user_data = account_user_factory(
28+
first_name=first_name,
29+
last_name=last_name,
30+
)
31+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
32+
ret_account_user = await users_obj.create(account_user_data)
33+
return ret_account_user
34+
35+
yield _created_account_user
36+
37+
if ret_account_user:
38+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
39+
try:
40+
await users_obj.delete(ret_account_user.id)
41+
except MPTAPIError as error:
42+
print( # noqa: WPS421
43+
f"TEARDOWN - Unable to delete account user {ret_account_user.id}: "
44+
f"{getattr(error, 'title', str(error))}"
45+
)
46+
47+
48+
@pytest.fixture
49+
async def created_user_group(async_mpt_ops, user_group_factory):
50+
"""Fixture to create and teardown a user group."""
51+
new_user_group_request_data = user_group_factory()
52+
created_user_group = await async_mpt_ops.accounts.user_groups.create(
53+
new_user_group_request_data
54+
)
55+
56+
yield created_user_group
57+
58+
try:
59+
await async_mpt_ops.accounts.user_groups.delete(created_user_group.id)
60+
except MPTAPIError as error:
61+
print(f"TEARDOWN - Unable to delete user group: {getattr(error, 'title', str(error))}") # noqa: WPS421
62+
63+
64+
@pytest.fixture
65+
async def created_account_user_group( # noqa: WPS210
66+
async_mpt_vendor, created_account_user, created_user_group, account_id
67+
):
68+
"""Fixture to create and teardown an account user group."""
69+
created_account_user_data = await created_account_user()
70+
user_group_data = created_user_group
71+
create_user_group_data = {"id": user_group_data.id}
72+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
73+
created_account_user_group = await users.groups(user_id=created_account_user_data.id).create(
74+
create_user_group_data
75+
)
76+
yield created_account_user_group
77+
78+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
79+
try:
80+
await users_obj.groups(user_id=created_account_user_data.id).delete(user_group_data.id)
81+
except MPTAPIError as error:
82+
print( # noqa: WPS421
83+
f"TEARDOWN - Unable to delete account user group: {getattr(error, 'title', str(error))}"
84+
)
85+
86+
87+
async def test_get_account_user_by_id(async_mpt_vendor, user_id, account_id):
88+
"""Test retrieving an account user by ID."""
89+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
90+
account_user = await users_obj.get(user_id)
91+
assert account_user is not None
92+
93+
94+
async def test_list_account_users(async_mpt_vendor, account_id):
95+
"""Test listing account users."""
96+
limit = 10
97+
98+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
99+
account_users = await users_obj.fetch_page(limit=limit)
100+
101+
assert len(account_users) > 0
102+
103+
104+
async def test_get_account_user_by_id_not_found(async_mpt_vendor, invalid_user_id, account_id):
105+
"""Test retrieving an account user by invalid ID."""
106+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
107+
108+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
109+
await users_obj.get(invalid_user_id)
110+
111+
112+
async def test_filter_account_users(async_mpt_vendor, user_id, account_id):
113+
"""Test filtering account users."""
114+
select_fields = ["-name"]
115+
116+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
117+
filtered_account_users = users_obj.filter(RQLQuery(id=user_id)).select(*select_fields)
118+
account_users = [user async for user in filtered_account_users.iterate()]
119+
120+
assert len(account_users) == 1
121+
122+
123+
async def test_create_account_user(created_account_user):
124+
"""Test creating an account user."""
125+
account_user_data = await created_account_user()
126+
assert account_user_data is not None
127+
128+
129+
async def test_delete_account_user(async_mpt_vendor, created_account_user, account_id):
130+
"""Test deleting an account user."""
131+
account_user_data = await created_account_user()
132+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
133+
await users_obj.delete(account_user_data.id)
134+
135+
136+
async def test_update_account_user(
137+
async_mpt_vendor,
138+
account_user_factory,
139+
created_account_user,
140+
account_id,
141+
):
142+
"""Test updating an account user."""
143+
account_user_data = await created_account_user()
144+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
145+
updated_account_user_data = account_user_factory(
146+
first_name="E2E Updated",
147+
last_name="Account User",
148+
)
149+
updated_account_user = await users_obj.update(
150+
account_user_data.id,
151+
updated_account_user_data,
152+
)
153+
assert updated_account_user is not None
154+
155+
156+
async def test_account_user_resend_invite(
157+
async_mpt_vendor,
158+
created_account_user,
159+
account_id,
160+
):
161+
"""Test resending an invite to an account user."""
162+
account_user_data = await created_account_user()
163+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
164+
resend_invite = await users_obj.resend_invite(account_user_data.id)
165+
assert resend_invite is not None
166+
167+
168+
def test_account_user_group_post(created_account_user_group): # noqa: AAA01
169+
"""Test creating an account user group."""
170+
created_account_user_group_data = created_account_user_group
171+
assert created_account_user_group_data is not None
172+
173+
174+
async def test_account_user_group_update(
175+
async_mpt_vendor,
176+
created_account_user,
177+
created_user_group,
178+
account_id,
179+
):
180+
"""Test updating an account user group."""
181+
created_account_user_data = await created_account_user()
182+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
183+
update_user_group_data = [{"id": created_user_group.id}]
184+
updated_account_user_group = await users_obj.groups(
185+
user_id=created_account_user_data.id
186+
).update(update_user_group_data)
187+
assert updated_account_user_group is not None
188+
189+
190+
async def test_account_user_group_delete(
191+
async_mpt_vendor,
192+
created_account_user,
193+
created_user_group,
194+
account_id,
195+
):
196+
"""Test deleting an account user group."""
197+
created_account_user_data = await created_account_user()
198+
users_obj = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
199+
create_user_group_data = {"id": created_user_group.id}
200+
await users_obj.groups(user_id=created_account_user_data.id).create(create_user_group_data)
201+
await users_obj.groups(user_id=created_account_user_data.id).delete(created_user_group.id)

0 commit comments

Comments
 (0)