Skip to content

Commit ad87439

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

7 files changed

Lines changed: 537 additions & 6 deletions

File tree

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: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# noqa: WPS204, WPS402, AAA01
2+
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)
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( # noqa: WPS430
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 = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
32+
ret_account_user = await users.create(account_user_data)
33+
return ret_account_user
34+
35+
yield _created_account_user
36+
37+
if ret_account_user:
38+
try:
39+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
40+
await users.delete(ret_account_user.id)
41+
except MPTAPIError as error:
42+
print(
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))}")
62+
63+
64+
@pytest.fixture
65+
async def created_account_user_group(
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+
try:
79+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
80+
await users.groups(user_id=created_account_user_data.id).delete(user_group_data.id)
81+
except MPTAPIError as error:
82+
print(
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 = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
90+
account_user = await users.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 = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
99+
account_users = await users.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 = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
107+
with pytest.raises(MPTAPIError, match=r"404 Not Found"):
108+
await users.get(invalid_user_id)
109+
110+
111+
async def test_filter_account_users(async_mpt_vendor, user_id, account_id):
112+
"""Test filtering account users."""
113+
select_fields = ["-name"]
114+
115+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
116+
filtered_account_users = users.filter(RQLQuery(id=user_id)).select(*select_fields)
117+
account_users = [user async for user in filtered_account_users.iterate()]
118+
119+
assert len(account_users) == 1
120+
121+
122+
async def test_create_account_user(created_account_user):
123+
"""Test creating an account user."""
124+
account_user_data = await created_account_user()
125+
assert account_user_data is not None
126+
127+
128+
async def test_delete_account_user(async_mpt_vendor, created_account_user, account_id):
129+
"""Test deleting an account user."""
130+
account_user_data = await created_account_user()
131+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
132+
await users.delete(account_user_data.id)
133+
134+
135+
async def test_update_account_user(
136+
async_mpt_vendor,
137+
account_user_factory,
138+
created_account_user,
139+
account_id,
140+
):
141+
"""Test updating an account user."""
142+
account_user_data = await created_account_user()
143+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
144+
updated_account_user_data = account_user_factory(
145+
first_name="E2E Updated",
146+
last_name="Account User",
147+
)
148+
updated_account_user = await users.update(
149+
account_user_data.id,
150+
updated_account_user_data,
151+
)
152+
assert updated_account_user is not None
153+
154+
155+
async def test_account_user_resend_invite(
156+
async_mpt_vendor,
157+
created_account_user,
158+
account_id,
159+
):
160+
"""Test resending an invite to an account user."""
161+
account_user_data = await created_account_user()
162+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
163+
resend_invite = await users.resend_invite(account_user_data.id)
164+
assert resend_invite is not None
165+
166+
167+
def test_account_user_group_post(created_account_user_group): # noqa: AAA01
168+
"""Test creating an account user group."""
169+
created_account_user_group_data = created_account_user_group
170+
assert created_account_user_group_data is not None
171+
172+
173+
async def test_account_user_group_update(
174+
async_mpt_vendor,
175+
created_account_user,
176+
created_user_group,
177+
account_id,
178+
):
179+
"""Test updating an account user group."""
180+
created_account_user_data = await created_account_user()
181+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
182+
update_user_group_data = [{"id": created_user_group.id}]
183+
updated_account_user_group = await users.groups(user_id=created_account_user_data.id).update(
184+
update_user_group_data
185+
)
186+
assert updated_account_user_group is not None
187+
188+
189+
async def test_account_user_group_delete(
190+
async_mpt_vendor,
191+
created_account_user,
192+
created_user_group,
193+
account_id,
194+
):
195+
"""Test deleting an account user group."""
196+
created_account_user_data = await created_account_user()
197+
users = async_mpt_vendor.accounts.accounts.users(account_id=account_id)
198+
create_user_group_data = {"id": created_user_group.id}
199+
await users.groups(user_id=created_account_user_data.id).create(create_user_group_data)
200+
await users.groups(user_id=created_account_user_data.id).delete(created_user_group.id)

0 commit comments

Comments
 (0)