|
4 | 4 | from unittest.mock import patch |
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +from httpx import Headers |
7 | 8 | from pydantic.v1 import StrictStr |
8 | 9 |
|
| 10 | +from pyatlan import __version__ as VERSION |
9 | 11 | from pyatlan.client.atlan import DEFAULT_RETRY, AtlanClient |
10 | 12 | from pyatlan.client.common.audit import LOGGER as AUDIT_LOGGER |
11 | 13 | from pyatlan.client.common.search_log import LOGGER as SEARCH_LOG_LOGGER |
|
46 | 48 | Term, |
47 | 49 | ) |
48 | 50 | from pyatlan.model.user import UserMinimalResponse |
| 51 | +from pyatlan.pkg.utils import get_client |
| 52 | +from pyatlan.utils import get_python_version |
49 | 53 | from tests.integration.client import TestId |
50 | 54 | from tests.integration.lineage_test import create_database, delete_asset |
51 | 55 | from tests.integration.requests_test import create_token, delete_token |
@@ -1627,6 +1631,76 @@ def test_client_401_token_refresh( |
1627 | 1631 | assert client.api_key != expired_api_token |
1628 | 1632 | assert results and results.count >= 1 |
1629 | 1633 |
|
| 1634 | + # Verify similar results with get_client() |
| 1635 | + # Setting ATLAN_API_KEY to empty string to force impersonation |
| 1636 | + monkeypatch.setenv("ATLAN_API_KEY", "") |
| 1637 | + assert expired_token_user_id |
| 1638 | + client = get_client(impersonate_user_id=expired_token_user_id) |
| 1639 | + results = ( |
| 1640 | + FluentSearch() |
| 1641 | + .where(CompoundQuery.active_assets()) |
| 1642 | + .where(CompoundQuery.asset_type(AtlasGlossary)) |
| 1643 | + .page_size(100) |
| 1644 | + .execute(client=client) |
| 1645 | + ) |
| 1646 | + |
| 1647 | + # Confirm the API key has been updated and results are returned |
| 1648 | + assert client.api_key != expired_api_token |
| 1649 | + assert results and results.count >= 1 |
| 1650 | + |
| 1651 | + # Verify package headers are set correctly |
| 1652 | + expected_common_headers = Headers( |
| 1653 | + { |
| 1654 | + "User-Agent": f"Atlan-PythonSDK/{VERSION}", |
| 1655 | + "Accept-Encoding": "gzip, deflate", |
| 1656 | + "Accept": "*/*", |
| 1657 | + "Connection": "keep-alive", |
| 1658 | + "x-atlan-agent": "sdk", |
| 1659 | + "x-atlan-agent-id": "python", |
| 1660 | + "x-atlan-client-origin": "product_sdk", |
| 1661 | + "x-atlan-python-version": get_python_version(), |
| 1662 | + "x-atlan-client-type": "sync", |
| 1663 | + } |
| 1664 | + ) |
| 1665 | + |
| 1666 | + # Clear package environment variables to test default headers |
| 1667 | + for var in [ |
| 1668 | + "X_ATLAN_AGENT", |
| 1669 | + "X_ATLAN_AGENT_ID", |
| 1670 | + "X_ATLAN_AGENT_PACKAGE_NAME", |
| 1671 | + "X_ATLAN_AGENT_WORKFLOW_ID", |
| 1672 | + ]: |
| 1673 | + monkeypatch.delenv(var, raising=False) |
| 1674 | + |
| 1675 | + client = get_client( |
| 1676 | + impersonate_user_id=expired_token_user_id, set_pkg_headers=False |
| 1677 | + ) |
| 1678 | + assert expected_common_headers == client._session.headers |
| 1679 | + |
| 1680 | + # Set package environment variables to test package headers |
| 1681 | + monkeypatch.setenv("X_ATLAN_AGENT", "agent_value") |
| 1682 | + monkeypatch.setenv("X_ATLAN_AGENT_ID", "agent_id_value") |
| 1683 | + monkeypatch.setenv("X_ATLAN_AGENT_PACKAGE_NAME", "package_name_value") |
| 1684 | + monkeypatch.setenv("X_ATLAN_AGENT_WORKFLOW_ID", "workflow_id_value") |
| 1685 | + |
| 1686 | + expected = Headers( |
| 1687 | + { |
| 1688 | + "User-Agent": f"Atlan-PythonSDK/{VERSION}", |
| 1689 | + "Accept-Encoding": "gzip, deflate", |
| 1690 | + "Accept": "*/*", |
| 1691 | + "Connection": "keep-alive", |
| 1692 | + "x-atlan-client-origin": "product_sdk", |
| 1693 | + "x-atlan-python-version": get_python_version(), |
| 1694 | + "x-atlan-client-type": "sync", |
| 1695 | + "x-atlan-agent": "agent_value", |
| 1696 | + "x-atlan-agent-id": "agent_id_value", |
| 1697 | + "x-atlan-agent-package-name": "package_name_value", |
| 1698 | + "x-atlan-agent-workflow-id": "workflow_id_value", |
| 1699 | + } |
| 1700 | + ) |
| 1701 | + client = get_client(impersonate_user_id=expired_token_user_id, set_pkg_headers=True) |
| 1702 | + assert expected == client._session.headers |
| 1703 | + |
1630 | 1704 |
|
1631 | 1705 | def test_client_init_from_token_guid( |
1632 | 1706 | client: AtlanClient, token: ApiToken, argo_fake_token: ApiToken, monkeypatch |
|
0 commit comments