|
| 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 created_account_user(async_mpt_vendor, account_user_factory, account_id): |
| 11 | + """Fixture to create and teardown an account user.""" |
| 12 | + ret_account_user = None |
| 13 | + |
| 14 | + async def _created_account_user( # noqa: WPS430 |
| 15 | + first_name: str = "E2E Created", |
| 16 | + last_name: str = "Account User", |
| 17 | + ): |
| 18 | + """Create an account user with the given first and last name.""" |
| 19 | + nonlocal ret_account_user # noqa: WPS420 |
| 20 | + account_user_data = account_user_factory( |
| 21 | + first_name=first_name, |
| 22 | + last_name=last_name, |
| 23 | + ) |
| 24 | + ret_account_user = await async_mpt_vendor.accounts.accounts.users( |
| 25 | + account_id=account_id |
| 26 | + ).create(account_user_data) |
| 27 | + return ret_account_user |
| 28 | + |
| 29 | + yield _created_account_user |
| 30 | + |
| 31 | + if ret_account_user: |
| 32 | + try: |
| 33 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id).delete( |
| 34 | + ret_account_user.id |
| 35 | + ) |
| 36 | + except MPTAPIError: |
| 37 | + print( # noqa: WPS421 |
| 38 | + f"TEARDOWN - Unable to delete account user {ret_account_user.id}", |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +async def created_user_group(async_mpt_ops, user_group_factory): |
| 44 | + """Fixture to create and teardown a user group.""" |
| 45 | + new_user_group_request_data = user_group_factory() |
| 46 | + created_user_group = await async_mpt_ops.accounts.user_groups.create( |
| 47 | + new_user_group_request_data |
| 48 | + ) |
| 49 | + |
| 50 | + yield created_user_group |
| 51 | + |
| 52 | + try: |
| 53 | + await async_mpt_ops.accounts.user_groups.delete(created_user_group.id) |
| 54 | + except MPTAPIError as error: |
| 55 | + print(f"TEARDOWN - Unable to delete user group: {error.title}") # noqa: WPS421 |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +async def created_account_user_group( |
| 60 | + async_mpt_vendor, created_account_user, created_user_group, account_id |
| 61 | +): |
| 62 | + """Fixture to create and teardown an account user group.""" |
| 63 | + created_account_user_data = await created_account_user() |
| 64 | + user_group_data = created_user_group |
| 65 | + create_user_group_data = {"id": user_group_data.id} |
| 66 | + created_account_user_group = ( |
| 67 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 68 | + .groups(user_id=created_account_user_data.id) |
| 69 | + .create(create_user_group_data) |
| 70 | + ) |
| 71 | + yield created_account_user_group |
| 72 | + |
| 73 | + try: |
| 74 | + await ( |
| 75 | + async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 76 | + .groups(user_id=created_account_user_data.id) |
| 77 | + .delete(user_group_data.id) |
| 78 | + ) |
| 79 | + except MPTAPIError as error: |
| 80 | + print(f"TEARDOWN - Unable to delete account user group: {error.title}") # noqa: WPS421 |
| 81 | + |
| 82 | + |
| 83 | +async def test_get_account_user_by_id(async_mpt_vendor, user_id, account_id): |
| 84 | + """Test retrieving an account user by ID.""" |
| 85 | + account_user = await async_mpt_vendor.accounts.accounts.users(account_id=account_id).get( |
| 86 | + user_id |
| 87 | + ) |
| 88 | + assert account_user is not None |
| 89 | + |
| 90 | + |
| 91 | +async def test_list_account_users(async_mpt_vendor, account_id): |
| 92 | + """Test listing account users.""" |
| 93 | + limit = 10 |
| 94 | + |
| 95 | + account_users = await async_mpt_vendor.accounts.accounts.users( |
| 96 | + account_id=account_id |
| 97 | + ).fetch_page(limit=limit) |
| 98 | + |
| 99 | + assert len(account_users) > 0 |
| 100 | + |
| 101 | + |
| 102 | +async def test_get_account_user_by_id_not_found(async_mpt_vendor, invalid_user_id, account_id): |
| 103 | + """Test retrieving an account user by invalid ID.""" |
| 104 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 105 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id).get(invalid_user_id) |
| 106 | + |
| 107 | + |
| 108 | +async def test_filter_account_users(async_mpt_vendor, user_id, account_id): |
| 109 | + """Test filtering account users.""" |
| 110 | + select_fields = ["-name"] |
| 111 | + |
| 112 | + filtered_account_users = ( |
| 113 | + async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 114 | + .filter(RQLQuery(id=user_id)) |
| 115 | + .select(*select_fields) |
| 116 | + ) |
| 117 | + |
| 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 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id).delete( |
| 133 | + account_user_data.id |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +async def test_update_account_user( |
| 138 | + async_mpt_vendor, |
| 139 | + account_user_factory, |
| 140 | + created_account_user, |
| 141 | + account_id, |
| 142 | +): |
| 143 | + """Test updating an account user.""" |
| 144 | + account_user_data = await created_account_user() |
| 145 | + updated_account_user_data = account_user_factory( |
| 146 | + first_name="E2E Updated", |
| 147 | + last_name="Account User", |
| 148 | + ) |
| 149 | + updated_account_user = await async_mpt_vendor.accounts.accounts.users( |
| 150 | + account_id=account_id |
| 151 | + ).update( |
| 152 | + account_user_data.id, |
| 153 | + updated_account_user_data, |
| 154 | + ) |
| 155 | + assert updated_account_user is not None |
| 156 | + |
| 157 | + |
| 158 | +async def test_account_user_resend_invite( |
| 159 | + async_mpt_vendor, |
| 160 | + created_account_user, |
| 161 | + account_id, |
| 162 | +): |
| 163 | + """Test resending an invite to an account user.""" |
| 164 | + account_user_data = await created_account_user() |
| 165 | + resend_invite = await async_mpt_vendor.accounts.accounts.users( |
| 166 | + account_id=account_id |
| 167 | + ).resend_invite(account_user_data.id) |
| 168 | + assert resend_invite is not None |
| 169 | + |
| 170 | + |
| 171 | +async def test_account_user_group_post(created_account_user_group): |
| 172 | + """Test creating an account user group.""" |
| 173 | + created_account_user_group_data = created_account_user_group |
| 174 | + assert created_account_user_group_data is not None |
| 175 | + |
| 176 | + |
| 177 | +async def test_account_user_group_update( |
| 178 | + async_mpt_vendor, |
| 179 | + created_account_user, |
| 180 | + created_user_group, |
| 181 | + account_id, |
| 182 | +): |
| 183 | + """Test updating an account user group.""" |
| 184 | + created_account_user_data = await created_account_user() |
| 185 | + update_user_group_data = [{"id": created_user_group.id}] |
| 186 | + updated_account_user_group = ( |
| 187 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 188 | + .groups(user_id=created_account_user_data.id) |
| 189 | + .update(update_user_group_data) |
| 190 | + ) |
| 191 | + assert updated_account_user_group is not None |
| 192 | + |
| 193 | + |
| 194 | +async def test_account_user_group_delete( |
| 195 | + async_mpt_vendor, |
| 196 | + created_account_user, |
| 197 | + created_user_group, |
| 198 | + account_id, |
| 199 | +): |
| 200 | + """Test deleting an account user group.""" |
| 201 | + created_account_user_data = await created_account_user() |
| 202 | + create_user_group_data = {"id": created_user_group.id} |
| 203 | + ( |
| 204 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 205 | + .groups(user_id=created_account_user_data.id) |
| 206 | + .create(create_user_group_data) |
| 207 | + ) |
| 208 | + ( |
| 209 | + await async_mpt_vendor.accounts.accounts.users(account_id=account_id) |
| 210 | + .groups(user_id=created_account_user_data.id) |
| 211 | + .delete(created_user_group.id) |
| 212 | + ) |
0 commit comments