|
| 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