|
| 1 | +package tech.ydb.jdbc; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.sql.Statement; |
| 7 | +import java.util.Properties; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.AfterAll; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 13 | + |
| 14 | +import tech.ydb.jdbc.common.QueryType; |
| 15 | +import tech.ydb.jdbc.impl.helper.ExceptionAssert; |
| 16 | +import tech.ydb.jdbc.impl.helper.JdbcUrlHelper; |
| 17 | +import tech.ydb.test.junit5.YdbHelperExtension; |
| 18 | + |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author Aleksandr Gorshenin |
| 22 | + */ |
| 23 | +public class YdbDriverStaticCredsTest { |
| 24 | + @RegisterExtension |
| 25 | + private static final YdbHelperExtension ydb = new YdbHelperExtension(); |
| 26 | + |
| 27 | + private static final JdbcUrlHelper jdbcURL = new JdbcUrlHelper(ydb); |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + public static void createUsers() throws SQLException { |
| 31 | + try (Connection connection = DriverManager.getConnection(jdbcURL.build())) { |
| 32 | + try (Statement statement = connection.createStatement()) { |
| 33 | + statement.execute(QueryType.SCHEME_QUERY.getPrefix() + "\n" |
| 34 | + + "CREATE USER user1 PASSWORD NULL;" |
| 35 | + + "CREATE USER user2 PASSWORD 'pwss';" |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @AfterAll |
| 42 | + public static void dropUsers() throws SQLException { |
| 43 | + try (Connection connection = DriverManager.getConnection(jdbcURL.build())) { |
| 44 | + try (Statement statement = connection.createStatement()) { |
| 45 | + statement.execute(QueryType.SCHEME_QUERY.getPrefix() + "\n" |
| 46 | + + "DROP USER IF EXISTS user1, user2;" |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private ConnectionSupplier connectByProperties(String username, String password) { |
| 53 | + final Properties props = new Properties(); |
| 54 | + props.put("user", username); |
| 55 | + if (password != null) { |
| 56 | + props.put("password", password); |
| 57 | + } |
| 58 | + return () -> DriverManager.getConnection(jdbcURL.disableToken().build(), props); |
| 59 | + } |
| 60 | + |
| 61 | + private ConnectionSupplier connectByAuthority(String username, String password) { |
| 62 | + return () -> DriverManager.getConnection(jdbcURL.disableToken().withAutority(username, password).build()); |
| 63 | + } |
| 64 | + |
| 65 | + private void testConnection(ConnectionSupplier connectionSupplier) throws SQLException { |
| 66 | + try (Connection connection = connectionSupplier.get()) { |
| 67 | + try (Statement statement = connection.createStatement()) { |
| 68 | + statement.execute("SELECT 1;"); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void connectOK() throws SQLException { |
| 75 | + testConnection(connectByProperties("user1", "pwss")); |
| 76 | + testConnection(connectByAuthority("user1", "pwss")); |
| 77 | + |
| 78 | + testConnection(connectByProperties("user2", "")); |
| 79 | + testConnection(connectByAuthority("user2", "")); |
| 80 | + |
| 81 | + testConnection(connectByProperties("user2", null)); |
| 82 | + testConnection(connectByAuthority("user2", null)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void connectWring() throws SQLException { |
| 87 | + ExceptionAssert.ydbConfiguration("can't connect", () -> testConnection(connectByProperties("user1", ""))); |
| 88 | + ExceptionAssert.ydbConfiguration("can't connect", () -> testConnection(connectByProperties("user1", null))); |
| 89 | + ExceptionAssert.ydbConfiguration("can't connect", () -> testConnection(connectByProperties("user1", "pass"))); |
| 90 | + |
| 91 | + ExceptionAssert.ydbConfiguration("can't connect", () -> testConnection(connectByProperties("user2", "a"))); |
| 92 | + } |
| 93 | + |
| 94 | + interface ConnectionSupplier { |
| 95 | + Connection get() throws SQLException; |
| 96 | + } |
| 97 | +} |
0 commit comments