Skip to content

Commit

Permalink
Add test cases for authenticators & defaultAuthenticator (#29656)
Browse files Browse the repository at this point in the history
* Add test cases for authenticators & defaultAuthenticator

* Add test cases
  • Loading branch information
yx9o authored Jan 5, 2024
1 parent 45fad86 commit 645c63b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DefaultAuthorityRuleConfigurationBuilderTest {

Expand All @@ -30,5 +32,7 @@ void assertBuild() {
AuthorityRuleConfiguration actual = new DefaultAuthorityRuleConfigurationBuilder().build();
assertThat(actual.getPrivilegeProvider().getType(), is("ALL_PERMITTED"));
assertThat(actual.getUsers().size(), is(1));
assertNull(actual.getDefaultAuthenticator());
assertTrue(actual.getAuthenticators().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collections;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

Expand All @@ -38,7 +39,21 @@ class NewYamlAuthorityRuleConfigurationSwapperTest {
@Test
void assertSwapToDataNodes() {
Collection<ShardingSphereUser> users = Collections.singleton(new ShardingSphereUser("root", "", "localhost"));
Collection<YamlDataNode> actual = swapper.swapToDataNodes(new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.emptyMap(), null));
assertThat(actual.iterator().next().getKey(), is("authority"));
Collection<YamlDataNode> actual = swapper.swapToDataNodes(new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.singletonMap("md5", new AlgorithmConfiguration("MD5", createProperties())), "scram_sha256"));
YamlDataNode yamlDataNode = actual.iterator().next();
assertThat(yamlDataNode.getKey(), is("authority"));
assertThat(yamlDataNode.getValue(), containsString("user: root@localhost"));
assertThat(yamlDataNode.getValue(), containsString("password: ''"));
assertThat(yamlDataNode.getValue(), containsString("type: ALL_PERMITTED"));
assertThat(yamlDataNode.getValue(), containsString("defaultAuthenticator: scram_sha256"));
assertThat(yamlDataNode.getValue(), containsString("type: MD5"));
assertThat(yamlDataNode.getValue(), containsString("proxy-frontend-database-protocol-type: openGauss"));
}

private Properties createProperties() {
Properties result = new Properties();
result.put("proxy-frontend-database-protocol-type", "openGauss");
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class YamlAuthorityRuleConfigurationSwapperTest {
Expand All @@ -38,22 +39,27 @@ class YamlAuthorityRuleConfigurationSwapperTest {

@Test
void assertSwapToYamlConfiguration() {
AuthorityRuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(Collections.emptyList(), new AlgorithmConfiguration("type", new Properties()), Collections.emptyMap(), null);
AuthorityRuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(Collections.emptyList(), new AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.singletonMap("md5", createAlgorithmConfiguration()), "scram_sha256");
YamlAuthorityRuleConfiguration actual = swapper.swapToYamlConfiguration(authorityRuleConfig);
assertTrue(actual.getUsers().isEmpty());
assertNotNull(actual.getPrivilege());
assertThat(actual.getDefaultAuthenticator(), is("scram_sha256"));
assertThat(actual.getAuthenticators().size(), is(1));
}

@Test
void assertSwapToObject() {
YamlAuthorityRuleConfiguration authorityRuleConfig = new YamlAuthorityRuleConfiguration();
authorityRuleConfig.setUsers(Collections.singletonList(getYamlUser()));
YamlAlgorithmConfiguration yamlAlgorithmConfig = new YamlAlgorithmConfiguration();
yamlAlgorithmConfig.setType("type");
authorityRuleConfig.setPrivilege(yamlAlgorithmConfig);
authorityRuleConfig.setPrivilege(createYamlAlgorithmConfiguration());
authorityRuleConfig.setDefaultAuthenticator("scram_sha256");
authorityRuleConfig.setAuthenticators(Collections.singletonMap("md5", createYamlAlgorithmConfiguration()));
AuthorityRuleConfiguration actual = swapper.swapToObject(authorityRuleConfig);
assertThat(actual.getUsers().size(), is(1));
assertNotNull(actual.getPrivilegeProvider());
assertThat(actual.getDefaultAuthenticator(), is("scram_sha256"));
assertThat(actual.getAuthenticators().size(), is(1));
}

@Test
Expand All @@ -63,6 +69,9 @@ void assertSwapToObjectWithDefaultProvider() {
AuthorityRuleConfiguration actual = swapper.swapToObject(authorityRuleConfig);
assertThat(actual.getUsers().size(), is(1));
assertThat(actual.getPrivilegeProvider().getType(), is("ALL_PERMITTED"));
assertThat(actual.getUsers().size(), is(1));
assertNull(actual.getDefaultAuthenticator());
assertTrue(actual.getAuthenticators().isEmpty());
}

private YamlUserConfiguration getYamlUser() {
Expand All @@ -71,4 +80,14 @@ private YamlUserConfiguration getYamlUser() {
result.setPassword("password");
return result;
}

private AlgorithmConfiguration createAlgorithmConfiguration() {
return new AlgorithmConfiguration("MD5", new Properties());
}

private YamlAlgorithmConfiguration createYamlAlgorithmConfiguration() {
YamlAlgorithmConfiguration result = new YamlAlgorithmConfiguration();
result.setType("MD5");
return result;
}
}

0 comments on commit 645c63b

Please sign in to comment.