|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 | # pylint: disable=redefined-outer-name,unused-argument |
| 18 | +import base64 |
18 | 19 | import os |
19 | 20 | from typing import Any, Callable, Dict, cast |
20 | 21 | from unittest import mock |
@@ -1519,6 +1520,132 @@ def test_request_session_with_ssl_client_cert() -> None: |
1519 | 1520 | assert "Could not find the TLS certificate file, invalid path: path_to_client_cert" in str(e.value) |
1520 | 1521 |
|
1521 | 1522 |
|
| 1523 | +def test_rest_catalog_with_basic_auth_type(rest_mock: Mocker) -> None: |
| 1524 | + # Given |
| 1525 | + rest_mock.get( |
| 1526 | + f"{TEST_URI}v1/config", |
| 1527 | + json={"defaults": {}, "overrides": {}}, |
| 1528 | + status_code=200, |
| 1529 | + ) |
| 1530 | + # Given |
| 1531 | + catalog_properties = { |
| 1532 | + "uri": TEST_URI, |
| 1533 | + "auth": { |
| 1534 | + "type": "basic", |
| 1535 | + "basic": { |
| 1536 | + "username": "one", |
| 1537 | + "password": "two", |
| 1538 | + }, |
| 1539 | + }, |
| 1540 | + } |
| 1541 | + catalog = RestCatalog("rest", **catalog_properties) # type: ignore |
| 1542 | + assert catalog.uri == TEST_URI |
| 1543 | + |
| 1544 | + encoded_user_pass = base64.b64encode(b"one:two").decode() |
| 1545 | + expected_auth_header = f"Basic {encoded_user_pass}" |
| 1546 | + assert rest_mock.last_request.headers["Authorization"] == expected_auth_header |
| 1547 | + |
| 1548 | + |
| 1549 | +def test_rest_catalog_with_custom_auth_type() -> None: |
| 1550 | + # Given |
| 1551 | + catalog_properties = { |
| 1552 | + "uri": TEST_URI, |
| 1553 | + "auth": { |
| 1554 | + "type": "custom", |
| 1555 | + "impl": "dummy.nonexistent.package", |
| 1556 | + "custom": { |
| 1557 | + "property1": "one", |
| 1558 | + "property2": "two", |
| 1559 | + }, |
| 1560 | + }, |
| 1561 | + } |
| 1562 | + with pytest.raises(ValueError) as e: |
| 1563 | + # Missing namespace |
| 1564 | + RestCatalog("rest", **catalog_properties) # type: ignore |
| 1565 | + assert "Could not load AuthManager class for 'dummy.nonexistent.package'" in str(e.value) |
| 1566 | + |
| 1567 | + |
| 1568 | +def test_rest_catalog_with_custom_basic_auth_type(rest_mock: Mocker) -> None: |
| 1569 | + # Given |
| 1570 | + catalog_properties = { |
| 1571 | + "uri": TEST_URI, |
| 1572 | + "auth": { |
| 1573 | + "type": "custom", |
| 1574 | + "impl": "pyiceberg.catalog.rest.auth.BasicAuthManager", |
| 1575 | + "custom": { |
| 1576 | + "username": "one", |
| 1577 | + "password": "two", |
| 1578 | + }, |
| 1579 | + }, |
| 1580 | + } |
| 1581 | + rest_mock.get( |
| 1582 | + f"{TEST_URI}v1/config", |
| 1583 | + json={"defaults": {}, "overrides": {}}, |
| 1584 | + status_code=200, |
| 1585 | + ) |
| 1586 | + catalog = RestCatalog("rest", **catalog_properties) # type: ignore |
| 1587 | + assert catalog.uri == TEST_URI |
| 1588 | + |
| 1589 | + encoded_user_pass = base64.b64encode(b"one:two").decode() |
| 1590 | + expected_auth_header = f"Basic {encoded_user_pass}" |
| 1591 | + assert rest_mock.last_request.headers["Authorization"] == expected_auth_header |
| 1592 | + |
| 1593 | + |
| 1594 | +def test_rest_catalog_with_custom_auth_type_no_impl() -> None: |
| 1595 | + # Given |
| 1596 | + catalog_properties = { |
| 1597 | + "uri": TEST_URI, |
| 1598 | + "auth": { |
| 1599 | + "type": "custom", |
| 1600 | + "custom": { |
| 1601 | + "property1": "one", |
| 1602 | + "property2": "two", |
| 1603 | + }, |
| 1604 | + }, |
| 1605 | + } |
| 1606 | + with pytest.raises(ValueError) as e: |
| 1607 | + # Missing namespace |
| 1608 | + RestCatalog("rest", **catalog_properties) # type: ignore |
| 1609 | + assert "auth.impl must be specified when using custom auth.type" in str(e.value) |
| 1610 | + |
| 1611 | + |
| 1612 | +def test_rest_catalog_with_non_custom_auth_type_impl() -> None: |
| 1613 | + # Given |
| 1614 | + catalog_properties = { |
| 1615 | + "uri": TEST_URI, |
| 1616 | + "auth": { |
| 1617 | + "type": "basic", |
| 1618 | + "impl": "basic.package", |
| 1619 | + "basic": { |
| 1620 | + "username": "one", |
| 1621 | + "password": "two", |
| 1622 | + }, |
| 1623 | + }, |
| 1624 | + } |
| 1625 | + with pytest.raises(ValueError) as e: |
| 1626 | + # Missing namespace |
| 1627 | + RestCatalog("rest", **catalog_properties) # type: ignore |
| 1628 | + assert "auth.impl can only be specified when using custom auth.type" in str(e.value) |
| 1629 | + |
| 1630 | + |
| 1631 | +def test_rest_catalog_with_unsupported_auth_type() -> None: |
| 1632 | + # Given |
| 1633 | + catalog_properties = { |
| 1634 | + "uri": TEST_URI, |
| 1635 | + "auth": { |
| 1636 | + "type": "unsupported", |
| 1637 | + "unsupported": { |
| 1638 | + "property1": "one", |
| 1639 | + "property2": "two", |
| 1640 | + }, |
| 1641 | + }, |
| 1642 | + } |
| 1643 | + with pytest.raises(ValueError) as e: |
| 1644 | + # Missing namespace |
| 1645 | + RestCatalog("rest", **catalog_properties) # type: ignore |
| 1646 | + assert "Could not load AuthManager class for 'unsupported'" in str(e.value) |
| 1647 | + |
| 1648 | + |
1522 | 1649 | EXAMPLE_ENV = {"PYICEBERG_CATALOG__PRODUCTION__URI": TEST_URI} |
1523 | 1650 |
|
1524 | 1651 |
|
|
0 commit comments