timothyw553 commented on code in PR #19054:
URL: https://github.com/apache/iceberg/pull/19054#discussion_r4709783928


##########
core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java:
##########
@@ -504,6 +505,67 @@ public <T extends RESTResponse> T execute(
     restCat.close();
   }
 
+  @Test
+  public void testCreateOrReplaceTableWhenViewAccessIsForbidden() {
+    // server denies access to view endpoints but allows table operations
+    RESTCatalogAdapter adapter =
+        new RESTCatalogAdapter(backendCatalog) {
+          @Override
+          protected <T extends RESTResponse> T execute(
+              HTTPRequest request,
+              Class<T> responseType,
+              Consumer<ErrorResponse> errorHandler,
+              Consumer<Map<String, String>> responseHeaders) {
+            if (request.method() == HTTPMethod.HEAD && 
request.path().contains("/views/")) {
+              throw new ForbiddenException("Forbidden: view access denied");
+            }
+            return super.execute(request, responseType, errorHandler, 
responseHeaders);
+          }
+        };
+
+    RESTCatalog catalog =
+        new RESTCatalog(SessionCatalog.SessionContext.createEmpty(), (config) 
-> adapter);
+    catalog.initialize("test", ImmutableMap.of());
+
+    catalog.createNamespace(NS);
+    catalog.buildTable(TABLE, SCHEMA).create();
+
+    catalog.buildTable(TABLE, 
OTHER_SCHEMA).createOrReplaceTransaction().commitTransaction();
+    Table replaced = catalog.loadTable(TABLE);
+    
assertThat(replaced.schema().columns()).extracting("name").containsExactly("some_id");
+  }
+
+  @Test
+  public void testCreateOrReplaceTableWithExistingViewStillFails() {
+    // server reports that a view with the same name exists
+    RESTCatalogAdapter adapter =
+        new RESTCatalogAdapter(backendCatalog) {
+          @Override
+          public <T extends RESTResponse> T handleRequest(
+              Route route,
+              Map<String, String> vars,
+              HTTPRequest httpRequest,
+              Class<T> responseType,
+              Consumer<Map<String, String>> responseHeaders) {
+            if (route == Route.VIEW_EXISTS) {

Review Comment:
   here we return `null` for `Route.VIEW_EXISTS` and create a table instead of 
a view, so this never hits the new `ForbiddenException` catch and passes with 
or without this PR. could we instead throw `ForbiddenException` on 
`Route.VIEW_EXISTS` with a real view in `backendCatalog` and verify commit 
fails while leaving the view and its version/schema unchanged?
   
   ```diff
    if (route == Route.VIEW_EXISTS) {
   -  return null;
   +  throw new ForbiddenException("Forbidden: view access denied");
    }
   
    catalog.createNamespace(NS);
   -catalog.buildTable(TABLE, SCHEMA).create();
   +backendCatalog.buildView(TABLE)
   +    .withSchema(SCHEMA)
   +    .withDefaultNamespace(NS)
   +    .withQuery("spark", "select 1")
   +    .create();
   
   -assertThatThrownBy(() -> catalog.buildTable(TABLE, 
OTHER_SCHEMA).createOrReplaceTransaction())
   +Transaction transaction =
   +    catalog.buildTable(TABLE, OTHER_SCHEMA).createOrReplaceTransaction();
   +assertThatThrownBy(transaction::commitTransaction)
        .isInstanceOf(AlreadyExistsException.class)
        .hasMessageContaining("View with same name already exists");
   +assertThat(backendCatalog.tableExists(TABLE)).isFalse();
   +assertThat(backendCatalog.viewExists(TABLE)).isTrue();
   ```
   
   could we also add separate tests for creating a missing table after a 403 
and bare `replaceTransaction()` throwing `NoSuchTableException` when no table 
exists?



-- 
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