|
| 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_api_token(async_mpt_vendor, api_token_factory): |
| 11 | + new_api_token_request_data = api_token_factory() |
| 12 | + created_api_token = await async_mpt_vendor.accounts.api_tokens.create( |
| 13 | + new_api_token_request_data |
| 14 | + ) |
| 15 | + |
| 16 | + yield created_api_token |
| 17 | + |
| 18 | + try: |
| 19 | + await async_mpt_vendor.accounts.api_tokens.delete(created_api_token.id) |
| 20 | + except MPTAPIError as error: |
| 21 | + print(f"TEARDOWN - Unable to delete api token: {error.title}") # noqa: WPS421 |
| 22 | + |
| 23 | + |
| 24 | +async def test_get_api_token_by_id(async_mpt_vendor, api_token_id): |
| 25 | + api_token = await async_mpt_vendor.accounts.api_tokens.get(api_token_id) |
| 26 | + assert api_token is not None |
| 27 | + |
| 28 | + |
| 29 | +async def test_list_api_tokens(async_mpt_vendor): |
| 30 | + limit = 10 |
| 31 | + api_tokens = await async_mpt_vendor.accounts.api_tokens.fetch_page(limit=limit) |
| 32 | + assert len(api_tokens) > 0 |
| 33 | + |
| 34 | + |
| 35 | +async def test_get_api_token_by_id_not_found(async_mpt_vendor, invalid_api_token_id): |
| 36 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 37 | + await async_mpt_vendor.accounts.api_tokens.get(invalid_api_token_id) |
| 38 | + |
| 39 | + |
| 40 | +async def test_filter_api_tokens(async_mpt_vendor, api_token_id): |
| 41 | + select_fields = ["-description"] |
| 42 | + |
| 43 | + filtered_api_tokens = ( |
| 44 | + async_mpt_vendor.accounts.api_tokens.filter(RQLQuery(id=api_token_id)) |
| 45 | + .filter(RQLQuery(name="E2E Seeded Token")) |
| 46 | + .select(*select_fields) |
| 47 | + ) |
| 48 | + |
| 49 | + api_tokens = [filtered_api_token async for filtered_api_token in filtered_api_tokens.iterate()] |
| 50 | + |
| 51 | + assert len(api_tokens) == 1 |
| 52 | + |
| 53 | + |
| 54 | +def test_create_api_token(created_api_token): |
| 55 | + new_api_token = created_api_token |
| 56 | + assert new_api_token is not None |
| 57 | + |
| 58 | + |
| 59 | +async def test_delete_api_token(async_mpt_vendor, created_api_token): |
| 60 | + await async_mpt_vendor.accounts.api_tokens.delete(created_api_token.id) |
| 61 | + |
| 62 | + |
| 63 | +async def test_delete_api_token_not_found(async_mpt_vendor, invalid_api_token_id): |
| 64 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 65 | + await async_mpt_vendor.accounts.api_tokens.delete(invalid_api_token_id) |
| 66 | + |
| 67 | + |
| 68 | +async def test_update_api_token(async_mpt_vendor, api_token_factory, created_api_token): |
| 69 | + updated_api_token_data = api_token_factory(name="E2E Updated API Token") |
| 70 | + |
| 71 | + updated_api_token = await async_mpt_vendor.accounts.api_tokens.update( |
| 72 | + created_api_token.id, updated_api_token_data |
| 73 | + ) |
| 74 | + |
| 75 | + assert updated_api_token is not None |
| 76 | + |
| 77 | + |
| 78 | +async def test_update_api_token_not_found( |
| 79 | + async_mpt_vendor, api_token_factory, invalid_api_token_id |
| 80 | +): |
| 81 | + updated_api_token_data = api_token_factory(name="Nonexistent API Token") |
| 82 | + |
| 83 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 84 | + await async_mpt_vendor.accounts.api_tokens.update( |
| 85 | + invalid_api_token_id, updated_api_token_data |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +async def test_api_token_disable(async_mpt_vendor, created_api_token): |
| 90 | + disabled_api_token = await async_mpt_vendor.accounts.api_tokens.disable(created_api_token.id) |
| 91 | + |
| 92 | + assert disabled_api_token is not None |
| 93 | + |
| 94 | + |
| 95 | +async def test_api_token_disable_not_found(async_mpt_vendor, invalid_api_token_id): |
| 96 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 97 | + await async_mpt_vendor.accounts.api_tokens.disable(invalid_api_token_id) |
| 98 | + |
| 99 | + |
| 100 | +async def test_api_token_enable(async_mpt_vendor, created_api_token): |
| 101 | + await async_mpt_vendor.accounts.api_tokens.disable(created_api_token.id) |
| 102 | + |
| 103 | + enabled_api_token = await async_mpt_vendor.accounts.api_tokens.enable(created_api_token.id) |
| 104 | + |
| 105 | + assert enabled_api_token is not None |
| 106 | + |
| 107 | + |
| 108 | +async def test_api_token_enable_not_found(async_mpt_vendor, invalid_api_token_id): |
| 109 | + with pytest.raises(MPTAPIError, match=r"404 Not Found"): |
| 110 | + await async_mpt_vendor.accounts.api_tokens.enable(invalid_api_token_id) |
0 commit comments