Skip to content

Commit d87feda

Browse files
committed
Add unit tests for static creditials
1 parent 5200e31 commit d87feda

File tree

3 files changed

+125
-8
lines changed

3 files changed

+125
-8
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/test/java/tech/ydb/jdbc/impl/helper/JdbcConnectionExtention.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
public class JdbcConnectionExtention implements
2424
BeforeEachCallback, BeforeAllCallback, AfterEachCallback, AfterAllCallback {
2525

26-
private final YdbHelperExtension ydb;
2726
private final JdbcUrlHelper jdbcURL;
2827

2928
private final Map<ExtensionContext, Connection> map = new HashMap<>();
3029
private final Stack<Connection> stack = new Stack<>();
3130

3231
public JdbcConnectionExtention(YdbHelperExtension ydb, boolean autoCommit) {
33-
this.ydb = ydb;
3432
this.jdbcURL = new JdbcUrlHelper(ydb)
3533
.withArg("failOnTruncatedResult", "true")
3634
.withArg("autoCommit", String.valueOf(autoCommit));

src/test/java/tech/ydb/jdbc/impl/helper/JdbcUrlHelper.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,51 @@
1010
public class JdbcUrlHelper {
1111
private final YdbHelperExtension ydb;
1212
private final String extra;
13+
private final String authority;
14+
private final boolean disableToken;
1315

1416
public JdbcUrlHelper(YdbHelperExtension ydb) {
17+
this(ydb, "", "", false);
18+
}
19+
20+
private JdbcUrlHelper(YdbHelperExtension ydb, String extra, String authority, boolean disableToken) {
1521
this.ydb = ydb;
16-
this.extra = "";
22+
this.extra = extra;
23+
this.authority = authority;
24+
this.disableToken = disableToken;
1725
}
1826

19-
private JdbcUrlHelper(JdbcUrlHelper other, String extra) {
20-
this.ydb = other.ydb;
21-
this.extra = other.extra.isEmpty() ? extra : other.extra + "&" + extra;
27+
public JdbcUrlHelper disableToken() {
28+
return new JdbcUrlHelper(ydb, extra, authority, true);
2229
}
2330

2431
public JdbcUrlHelper withArg(String arg, String value) {
25-
return new JdbcUrlHelper(this, arg + "=" + value);
32+
String newExtra = new StringBuilder(extra)
33+
.append(extra.isEmpty() ? "" : "&")
34+
.append(arg)
35+
.append("=")
36+
.append(value)
37+
.toString();
38+
return new JdbcUrlHelper(ydb, newExtra, authority, disableToken);
39+
}
40+
41+
public JdbcUrlHelper withAutority(String username, String password) {
42+
StringBuilder newAuthority = new StringBuilder(username);
43+
if (password != null && !password.isEmpty()) {
44+
newAuthority = newAuthority.append(":").append(password);
45+
}
46+
return new JdbcUrlHelper(ydb, extra, newAuthority.append("@").toString(), disableToken);
2647
}
2748

2849
public String build() {
2950
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
3051
.append(ydb.useTls() ? "grpcs://" : "grpc://")
52+
.append(authority)
3153
.append(ydb.endpoint())
3254
.append(ydb.database());
3355

3456
char splitter = '?';
35-
if (ydb.authToken() != null) {
57+
if (ydb.authToken() != null && !disableToken) {
3658
jdbc.append(splitter).append("token=").append(ydb.authToken());
3759
splitter = '&';
3860
}

0 commit comments

Comments
 (0)