arina-ielchiieva commented on a change in pull request #1994: DRILL-7203: 
Accept impersonation userName as form field & fix back button for query page
URL: https://github.com/apache/drill/pull/1994#discussion_r385700019
 
 

 ##########
 File path: 
exec/java-exec/src/test/java/org/apache/drill/exec/server/rest/TestQueryWrapper.java
 ##########
 @@ -0,0 +1,137 @@
+/*
+ * 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.
+ */
+package org.apache.drill.exec.server.rest;
+
+import io.netty.channel.DefaultChannelPromise;
+import io.netty.channel.local.LocalAddress;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.proto.UserBitShared;
+import org.apache.drill.exec.proto.helper.QueryIdHelper;
+import org.apache.drill.exec.rpc.user.UserSession;
+import org.apache.drill.exec.server.Drillbit;
+import org.apache.drill.exec.server.options.SystemOptionManager;
+import org.apache.drill.exec.server.rest.auth.DrillUserPrincipal;
+import org.apache.drill.exec.store.sys.PersistentStore;
+import org.apache.drill.exec.work.WorkManager;
+import org.apache.drill.exec.work.foreman.Foreman;
+import org.apache.drill.test.ClusterFixture;
+import org.apache.drill.test.ClusterTest;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+public class TestQueryWrapper extends ClusterTest {
+
+  private static DrillRestServer server;
+  private static WorkManager workManager;
+  private static WebServer webServer;
+
+  private void setupServer() throws IOException {
+    setupServer(false);
+  }
+
+  private void setupServer(boolean impersonationEnabled) {
+    cluster = ClusterFixture.bareBuilder(dirTestWatcher)
+      .clusterSize(1)
+      .configProperty(ExecConstants.ALLOW_LOOPBACK_ADDRESS_BINDING, true)
+      .configProperty(ExecConstants.IMPERSONATION_ENABLED, 
impersonationEnabled)
+      .build();
+    Drillbit drillbit = cluster.drillbit();
+    workManager = drillbit.getManager();
+    webServer = drillbit.getWebServer();
+    final ServletContextHandler servletContextHandler = new 
ServletContextHandler(ServletContextHandler.SESSIONS);
+    servletContextHandler.setContextPath("/");
+    server = new DrillRestServer(workManager, 
servletContextHandler.getServletContext(), drillbit);
+  }
+
+  private QueryWrapper.QueryResult run(String sql) throws Exception {
+    return runWithUserName(sql, null);
+  }
+
+  private QueryWrapper.QueryResult runWithUserName(String sql, String 
userName) throws Exception {
+    if (cluster == null) {
+      setupServer();
+    }
+
+    SystemOptionManager systemOptions = 
cluster.drillbit().getContext().getOptionManager();
+    DrillUserPrincipal principal = new 
DrillUserPrincipal.AnonDrillUserPrincipal();
+    WebSessionResources webSessionResources = new WebSessionResources(
+      cluster.drillbit().getContext().getAllocator(),
+      new LocalAddress("test"),
+      UserSession.Builder.newBuilder()
+        .withOptionManager(systemOptions)
+        
.withCredentials(UserBitShared.UserCredentials.newBuilder().setUserName(principal.getName()).build())
+        .build(),
+      new DefaultChannelPromise(null));
+    WebUserConnection connection = new 
WebUserConnection.AnonWebUserConnection(webSessionResources);
+    QueryWrapper q = new QueryWrapper(sql, "SQL", null, userName);
+    return q.run(workManager, connection);
+  }
+
+
+  @Test
+  public void testShowSchemas() throws Exception {
+    QueryWrapper.QueryResult result = run("SHOW SCHEMAS");
+    assertEquals("COMPLETED", result.queryState);
+    assertNotEquals(0, result.rows.size());
+    assertEquals(1, result.columns.size());
+    assertEquals(result.columns.iterator().next(), "SCHEMA_NAME");
+  }
+
+  @Test
+  public void testImpersonation() throws Exception {
+    setupServer(true);
+    QueryWrapper.QueryResult result = runWithUserName("SELECT CATALOG_NAME, 
SCHEMA_NAME FROM information_schema.SCHEMATA", "alfred");
+    final PersistentStore<UserBitShared.QueryProfile> profiles = 
workManager.getContext().getProfileStoreContext().getCompletedProfileStore();
 
 Review comment:
   Profiles are never used, please remove.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to