|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2025 Atlan Pte. Ltd. |
| 3 | +"""Async integration tests for OAuth client CRUD operations.""" |
| 4 | + |
| 5 | +import time |
| 6 | +from typing import AsyncGenerator |
| 7 | + |
| 8 | +import pytest |
| 9 | +import pytest_asyncio |
| 10 | + |
| 11 | +from pyatlan.client.aio import AsyncAtlanClient |
| 12 | +from pyatlan.model.oauth_clients import OAuthClient, OAuthClientCreateResponse |
| 13 | +from tests.integration.client import TestId |
| 14 | + |
| 15 | +MODULE_NAME = TestId.make_unique("AsyncOAuthClient") |
| 16 | + |
| 17 | +# Test data |
| 18 | +OAUTH_CLIENT_NAME = f"{MODULE_NAME}_test_client" |
| 19 | +OAUTH_CLIENT_DESCRIPTION = "Async integration test OAuth client" |
| 20 | +OAUTH_CLIENT_DESCRIPTION_UPDATED = "Updated async integration test OAuth client" |
| 21 | +OAUTH_CLIENT_ROLE = "Admin" # Role description |
| 22 | + |
| 23 | + |
| 24 | +async def delete_oauth_client_async(client: AsyncAtlanClient, client_id: str) -> None: |
| 25 | + """Helper to delete an OAuth client.""" |
| 26 | + await client.oauth_client.purge(client_id) |
| 27 | + |
| 28 | + |
| 29 | +@pytest_asyncio.fixture(scope="module") |
| 30 | +async def oauth_client_response( |
| 31 | + client: AsyncAtlanClient, |
| 32 | +) -> AsyncGenerator[OAuthClientCreateResponse, None]: |
| 33 | + """ |
| 34 | + Fixture to create an OAuth client for testing. |
| 35 | + Yields the create response (which includes client_secret). |
| 36 | + Cleans up by deleting the OAuth client after tests. |
| 37 | + """ |
| 38 | + # Create OAuth client |
| 39 | + response = await client.oauth_client.create( |
| 40 | + name=OAUTH_CLIENT_NAME, |
| 41 | + role=OAUTH_CLIENT_ROLE, |
| 42 | + description=OAUTH_CLIENT_DESCRIPTION, |
| 43 | + ) |
| 44 | + assert response is not None |
| 45 | + assert response.client_id is not None |
| 46 | + assert response.client_secret is not None |
| 47 | + |
| 48 | + yield response |
| 49 | + |
| 50 | + # Cleanup |
| 51 | + if response.client_id: |
| 52 | + await delete_oauth_client_async(client, response.client_id) |
| 53 | + |
| 54 | + |
| 55 | +def _assert_oauth_client_create_response(response: OAuthClientCreateResponse): |
| 56 | + """Assert the OAuth client create response has expected values.""" |
| 57 | + assert response is not None |
| 58 | + assert response.id is not None |
| 59 | + assert response.client_id is not None |
| 60 | + assert response.client_id.startswith("oauth-client-") |
| 61 | + assert response.client_secret is not None |
| 62 | + assert response.display_name == OAUTH_CLIENT_NAME |
| 63 | + assert response.description == OAUTH_CLIENT_DESCRIPTION |
| 64 | + assert response.created_by is not None |
| 65 | + assert response.created_at is not None |
| 66 | + |
| 67 | + |
| 68 | +def _assert_oauth_client(oauth_client: OAuthClient, is_updated: bool = False): |
| 69 | + """Assert the OAuth client has expected values.""" |
| 70 | + assert oauth_client is not None |
| 71 | + assert oauth_client.id is not None |
| 72 | + assert oauth_client.client_id is not None |
| 73 | + assert oauth_client.client_id.startswith("oauth-client-") |
| 74 | + assert oauth_client.display_name == OAUTH_CLIENT_NAME |
| 75 | + if is_updated: |
| 76 | + assert oauth_client.description == OAUTH_CLIENT_DESCRIPTION_UPDATED |
| 77 | + else: |
| 78 | + assert oauth_client.description == OAUTH_CLIENT_DESCRIPTION |
| 79 | + |
| 80 | + |
| 81 | +async def test_oauth_client_create( |
| 82 | + client: AsyncAtlanClient, |
| 83 | + oauth_client_response: OAuthClientCreateResponse, |
| 84 | +): |
| 85 | + """Test creating an OAuth client.""" |
| 86 | + _assert_oauth_client_create_response(oauth_client_response) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.order(after="test_oauth_client_create") |
| 90 | +async def test_oauth_client_get_by_id( |
| 91 | + client: AsyncAtlanClient, |
| 92 | + oauth_client_response: OAuthClientCreateResponse, |
| 93 | +): |
| 94 | + """Test retrieving an OAuth client by ID.""" |
| 95 | + assert oauth_client_response.client_id is not None |
| 96 | + time.sleep(2) # Allow time for eventual consistency |
| 97 | + |
| 98 | + oauth_client = await client.oauth_client.get_by_id(oauth_client_response.client_id) |
| 99 | + _assert_oauth_client(oauth_client) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.order(after="test_oauth_client_get_by_id") |
| 103 | +async def test_oauth_client_get_all( |
| 104 | + client: AsyncAtlanClient, |
| 105 | + oauth_client_response: OAuthClientCreateResponse, |
| 106 | +): |
| 107 | + """Test retrieving all OAuth clients.""" |
| 108 | + assert oauth_client_response.client_id is not None |
| 109 | + time.sleep(2) |
| 110 | + |
| 111 | + response = await client.oauth_client.get_all() |
| 112 | + assert response is not None |
| 113 | + assert response.records is not None |
| 114 | + assert len(response.records) >= 1 |
| 115 | + |
| 116 | + # Find our test client |
| 117 | + found = False |
| 118 | + for oauth_client in response.records: |
| 119 | + if oauth_client.client_id == oauth_client_response.client_id: |
| 120 | + found = True |
| 121 | + _assert_oauth_client(oauth_client) |
| 122 | + break |
| 123 | + assert found, f"OAuth client {oauth_client_response.client_id} not found" |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.order(after="test_oauth_client_get_all") |
| 127 | +async def test_oauth_client_get_with_pagination( |
| 128 | + client: AsyncAtlanClient, |
| 129 | + oauth_client_response: OAuthClientCreateResponse, |
| 130 | +): |
| 131 | + """Test retrieving OAuth clients with pagination.""" |
| 132 | + assert oauth_client_response.client_id is not None |
| 133 | + |
| 134 | + response = await client.oauth_client.get(limit=10, offset=0, sort="-createdAt") |
| 135 | + assert response is not None |
| 136 | + assert response.total_record is not None |
| 137 | + assert response.total_record >= 1 |
| 138 | + |
| 139 | + |
| 140 | +@pytest.mark.order(after="test_oauth_client_get_with_pagination") |
| 141 | +async def test_oauth_client_update_description( |
| 142 | + client: AsyncAtlanClient, |
| 143 | + oauth_client_response: OAuthClientCreateResponse, |
| 144 | +): |
| 145 | + """Test updating an OAuth client's description.""" |
| 146 | + assert oauth_client_response.client_id is not None |
| 147 | + time.sleep(2) |
| 148 | + |
| 149 | + updated = await client.oauth_client.update( |
| 150 | + client_id=oauth_client_response.client_id, |
| 151 | + description=OAUTH_CLIENT_DESCRIPTION_UPDATED, |
| 152 | + ) |
| 153 | + _assert_oauth_client(updated, is_updated=True) |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.order(after="test_oauth_client_update_description") |
| 157 | +async def test_oauth_client_verify_update_persisted( |
| 158 | + client: AsyncAtlanClient, |
| 159 | + oauth_client_response: OAuthClientCreateResponse, |
| 160 | +): |
| 161 | + """Verify that the update was persisted.""" |
| 162 | + assert oauth_client_response.client_id is not None |
| 163 | + time.sleep(2) |
| 164 | + |
| 165 | + oauth_client = await client.oauth_client.get_by_id(oauth_client_response.client_id) |
| 166 | + _assert_oauth_client(oauth_client, is_updated=True) |
| 167 | + |
| 168 | + |
| 169 | +async def test_oauth_client_create_with_invalid_role_raises_error( |
| 170 | + client: AsyncAtlanClient, |
| 171 | +): |
| 172 | + """Test that creating an OAuth client with an invalid role raises an error.""" |
| 173 | + from pyatlan.errors import NotFoundError |
| 174 | + |
| 175 | + with pytest.raises(NotFoundError) as exc_info: |
| 176 | + await client.oauth_client.create( |
| 177 | + name="test-invalid-role", |
| 178 | + role="InvalidRole", |
| 179 | + ) |
| 180 | + assert "does not exist" in str(exc_info.value) |
| 181 | + assert "Available roles:" in str(exc_info.value) |
0 commit comments