You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2024/01/01 09:53:48 UTC

(shardingsphere) branch master updated: Add test case for AllPermittedPrivilege (#29618)

This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 33de11ff8a2 Add test case for AllPermittedPrivilege (#29618)
33de11ff8a2 is described below

commit 33de11ff8a2866f32f7af2a10f370e963cbae911
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Jan 1 17:53:43 2024 +0800

    Add test case for AllPermittedPrivilege (#29618)
---
 .../DatabasePermittedPrivilegeProviderTest.java    | 25 +++++++++---
 .../privilege/DatabasePermittedPrivilegesTest.java | 44 ++++++++++++++++++++++
 .../simple/AllPermittedPrivilegeProviderTest.java} | 21 +++++------
 .../privilege/AllPermittedPrivilegesTest.java      | 30 +++++++++++++++
 4 files changed, 103 insertions(+), 17 deletions(-)

diff --git a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
index c387b7dadb2..8eb81a2b2da 100644
--- a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
+++ b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
@@ -24,21 +24,34 @@ import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
-import java.util.Collections;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 class DatabasePermittedPrivilegeProviderTest {
     
     @Test
     void assertBuild() {
-        Properties props = PropertiesBuilder.build(new Property("user-database-mappings", "root@localhost=test, user1@127.0.0.1=db_dal_admin, user1@=test, user1@=test1, user1@=*"));
+        Properties props = PropertiesBuilder.build(new Property("user-database-mappings", "root@localhost=*, user1@127.0.0.1=sys_db, user1@=foo_db, user1@=bar_db, user2@=*"));
         PrivilegeProvider provider = TypedSPILoader.getService(PrivilegeProvider.class, "DATABASE_PERMITTED", props);
-        Map<Grantee, ShardingSpherePrivileges> actual = provider.build(Collections.singletonList(new ShardingSphereUser("user1", "", "127.0.0.2")));
-        Assertions.assertTrue(actual.get(new Grantee("user1", "127.0.0.2")).hasPrivileges("test"));
-        Assertions.assertTrue(actual.get(new Grantee("user1", "127.0.0.2")).hasPrivileges("db_dal_admin"));
+        Map<Grantee, ShardingSpherePrivileges> actual = provider.build(Arrays.asList(
+                new ShardingSphereUser("root", "", "localhost"),
+                new ShardingSphereUser("user1", "", "127.0.0.1"),
+                new ShardingSphereUser("user1", "", "%"),
+                new ShardingSphereUser("user3", "", "%")));
+        assertThat(actual.size(), is(4));
+        assertTrue(actual.get(new Grantee("root", "localhost")).hasPrivileges("sys_db"));
+        assertTrue(actual.get(new Grantee("user1", "127.0.0.1")).hasPrivileges("sys_db"));
+        assertTrue(actual.get(new Grantee("user1", "127.0.0.1")).hasPrivileges("foo_db"));
+        assertTrue(actual.get(new Grantee("user1", "%")).hasPrivileges("bar_db"));
+        assertFalse(actual.get(new Grantee("user1", "%")).hasPrivileges("sys_db"));
+        assertFalse(actual.get(new Grantee("user3", "%")).hasPrivileges("sys_db"));
     }
 }
diff --git a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/privilege/DatabasePermittedPrivilegesTest.java b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/privilege/DatabasePermittedPrivilegesTest.java
new file mode 100644
index 00000000000..e7d62ff273a
--- /dev/null
+++ b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/privilege/DatabasePermittedPrivilegesTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.shardingsphere.authority.provider.database.privilege;
+
+import org.apache.shardingsphere.authority.constant.AuthorityConstants;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DatabasePermittedPrivilegesTest {
+    
+    @Test
+    void assertHasPrivilegesWithWildcard() {
+        assertTrue(new DatabasePermittedPrivileges(Collections.singleton(AuthorityConstants.PRIVILEGE_WILDCARD)).hasPrivileges("foo_db"));
+    }
+    
+    @Test
+    void assertHasPrivileges() {
+        assertTrue(new DatabasePermittedPrivileges(Collections.singleton("foo_db")).hasPrivileges("foo_db"));
+    }
+    
+    @Test
+    void assertHasNotPrivileges() {
+        assertFalse(new DatabasePermittedPrivileges(Collections.singleton("foo_db")).hasPrivileges("bar_db"));
+    }
+}
diff --git a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
similarity index 57%
copy from kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
copy to kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
index c387b7dadb2..80c693e6c60 100644
--- a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
+++ b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
@@ -15,30 +15,29 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.authority.provider.database;
+package org.apache.shardingsphere.authority.provider.simple;
 
 import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.provider.simple.privilege.AllPermittedPrivileges;
 import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
 import org.apache.shardingsphere.infra.metadata.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import java.util.Collections;
 import java.util.Map;
-import java.util.Properties;
 
-class DatabasePermittedPrivilegeProviderTest {
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class AllPermittedPrivilegeProviderTest {
     
     @Test
     void assertBuild() {
-        Properties props = PropertiesBuilder.build(new Property("user-database-mappings", "root@localhost=test, user1@127.0.0.1=db_dal_admin, user1@=test, user1@=test1, user1@=*"));
-        PrivilegeProvider provider = TypedSPILoader.getService(PrivilegeProvider.class, "DATABASE_PERMITTED", props);
-        Map<Grantee, ShardingSpherePrivileges> actual = provider.build(Collections.singletonList(new ShardingSphereUser("user1", "", "127.0.0.2")));
-        Assertions.assertTrue(actual.get(new Grantee("user1", "127.0.0.2")).hasPrivileges("test"));
-        Assertions.assertTrue(actual.get(new Grantee("user1", "127.0.0.2")).hasPrivileges("db_dal_admin"));
+        Map<Grantee, ShardingSpherePrivileges> actual = TypedSPILoader.getService(PrivilegeProvider.class, "ALL_PERMITTED").build(Collections.singleton(new ShardingSphereUser("root@%")));
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(new Grantee("root", "%")), instanceOf(AllPermittedPrivileges.class));
     }
 }
diff --git a/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/privilege/AllPermittedPrivilegesTest.java b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/privilege/AllPermittedPrivilegesTest.java
new file mode 100644
index 00000000000..d22c7626267
--- /dev/null
+++ b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/privilege/AllPermittedPrivilegesTest.java
@@ -0,0 +1,30 @@
+/*
+ * 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.shardingsphere.authority.provider.simple.privilege;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class AllPermittedPrivilegesTest {
+    
+    @Test
+    void assertHasPrivileges() {
+        assertTrue(new AllPermittedPrivileges().hasPrivileges("foo_db"));
+    }
+}