rdblue commented on code in PR #5287:
URL: https://github.com/apache/iceberg/pull/5287#discussion_r930357767


##########
python/tests/catalog/test_rest.py:
##########
@@ -0,0 +1,670 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from uuid import UUID
+
+import pytest
+import requests_mock
+
+from pyiceberg.catalog.base import PropertiesUpdateSummary
+from pyiceberg.catalog.rest import RestCatalog, RestTable, TokenResponse
+from pyiceberg.exceptions import (
+    AlreadyExistsError,
+    BadCredentialsError,
+    NoSuchNamespaceError,
+    NoSuchTableError,
+    TableAlreadyExistsError,
+)
+from pyiceberg.schema import Schema
+from pyiceberg.table.base import Table
+from pyiceberg.table.metadata import TableMetadataV1
+from pyiceberg.table.partitioning import PartitionField, PartitionSpec
+from pyiceberg.table.refs import SnapshotRef, SnapshotRefType
+from pyiceberg.table.snapshots import Operation, Snapshot, Summary
+from pyiceberg.table.sorting import SortField, SortOrder
+from pyiceberg.transforms import IdentityTransform, TruncateTransform
+from pyiceberg.types import (
+    BooleanType,
+    IntegerType,
+    NestedField,
+    StringType,
+)
+
+TEST_HOST = "https://iceberg-test-catalog/";
+TEST_TOKEN = "client:secret"
+
+
+def test_token_200():
+    with requests_mock.Mocker() as m:
+        m.post(
+            f"{TEST_HOST}oauth/tokens",
+            json={
+                "access_token": TEST_TOKEN,
+                "token_type": "Bearer",
+                "expires_in": 86400,
+                "warehouse_id": "8bcb0838-50fc-472d-9ddb-8feb89ef5f1e",
+                "region": "us-west-2",
+                "issued_token_type": 
"urn:ietf:params:oauth:token-type:access_token",
+            },
+            status_code=200,
+        )
+        m.get(
+            f"{TEST_HOST}config",
+            json={"defaults": {}, "overrides": {"prefix": 
"oss/warehouses/8bcb0838-22vo-472d-1925-8feb89lidf1e"}},
+            status_code=200,
+        )
+
+        assert RestCatalog("rest", {}, TEST_HOST, TEST_TOKEN).token == 
TokenResponse(
+            access_token=TEST_TOKEN,
+            token_type="Bearer",
+            expires_in=86400,
+            warehouse_id="8bcb0838-50fc-472d-9ddb-8feb89ef5f1e",
+            region="us-west-2",
+            issued_token_type="urn:ietf:params:oauth:token-type:access_token",
+        )
+
+
+def test_token_401():
+    with requests_mock.Mocker() as m:
+        message = "Invalid client ID: abc"
+        m.post(
+            f"{TEST_HOST}oauth/tokens",
+            json={
+                "error": {
+                    "message": message,
+                    "type": "BadCredentialsException",
+                    "code": 401,
+                }
+            },
+            status_code=401,
+        )
+
+        with pytest.raises(BadCredentialsError) as e:
+            RestCatalog("rest", {}, TEST_HOST, TEST_TOKEN)
+        assert message in str(e.value)
+
+
+def _configure_token_and_config_endpoint(m: requests_mock.Mocker):
+    m.post(
+        f"{TEST_HOST}oauth/tokens",
+        json={
+            "access_token": "eyJ0eXAiOiJK",
+            "token_type": "Bearer",
+            "expires_in": 86400,
+            "warehouse_id": "8bcb0838-50fc-472d-9ddb-8feb89ef5f1e",
+            "region": "us-west-2",
+            "issued_token_type": 
"urn:ietf:params:oauth:token-type:access_token",
+        },
+        status_code=200,
+    )
+    m.get(
+        f"{TEST_HOST}config",
+        json={"defaults": {}, "overrides": {"prefix": 
"oss/warehouses/8bcb0838-22vo-472d-1925-8feb89lidf1e"}},
+        status_code=200,
+    )
+
+
+def test_list_tables_200():
+    with requests_mock.Mocker() as m:
+        _configure_token_and_config_endpoint(m)
+        namespace = "examples"
+        m.get(
+            
f"{TEST_HOST}oss/warehouses/8bcb0838-22vo-472d-1925-8feb89lidf1e/namespaces/{namespace}/tables",
+            json={"identifiers": [{"namespace": ["examples"], "name": 
"fooshare"}]},
+            status_code=200,
+        )
+
+        assert RestCatalog("rest", {}, TEST_HOST, 
TEST_TOKEN).list_tables(namespace) == [("examples", "fooshare")]
+
+
+def test_list_tables_404():
+    with requests_mock.Mocker() as m:
+        _configure_token_and_config_endpoint(m)
+        namespace = "examples"
+        m.get(
+            
f"{TEST_HOST}oss/warehouses/8bcb0838-22vo-472d-1925-8feb89lidf1e/namespaces/{namespace}/tables",
+            json={
+                "error": {
+                    "message": "Namespace does not exist: personal in 
warehouse 8bcb0838-50fc-472d-9ddb-8feb89ef5f1e",
+                    "type": "NoSuchNamespaceException",
+                    "code": 404,
+                }
+            },
+            status_code=404,
+        )
+        with pytest.raises(NoSuchTableError) as e:
+            RestCatalog("rest", {}, TEST_HOST, 
TEST_TOKEN).list_tables(namespace)
+        assert "Namespace does not exist" in str(e.value)
+
+
+def test_list_namespaces_200():
+    with requests_mock.Mocker() as m:
+        _configure_token_and_config_endpoint(m)
+        m.get(
+            
f"{TEST_HOST}oss/warehouses/8bcb0838-22vo-472d-1925-8feb89lidf1e/namespaces",
+            json={"namespaces": [["default"], ["examples"], ["fokko"], 
["system"]]},
+            status_code=200,
+        )
+        assert RestCatalog("rest", {}, TEST_HOST, 
TEST_TOKEN).list_namespaces() == [

Review Comment:
   But if there are no credentials, we should skip the fetch token call, right?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to