Skip to content

Commit 0a8019b

Browse files
committed
Fix unit tests - mysql_native_password was deprecated
Change-Id: I9ea46b82fbb334ee18c0206a39d69f9dd2abe3dd
1 parent dc71ceb commit 0a8019b

File tree

3 files changed

+145
-53
lines changed

3 files changed

+145
-53
lines changed

mysql-connector-python/tests/cext/test_cext_api.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939
import tests
4040

41+
import mysql.connector
42+
4143
from mysql.connector.constants import ClientFlag, ServerFlag
4244
from mysql.connector.version import VERSION
4345

@@ -53,6 +55,34 @@
5355
LOGGER = logging.getLogger(tests.LOGGER_NAME)
5456

5557

58+
def setUpModule() -> None:
59+
with CMySQLConnection(**tests.get_mysql_config()) as cnx:
60+
global NATIVE_PASSWORD_INSTALLED
61+
62+
user = {
63+
"user": "_bugs",
64+
"host": "%",
65+
"auth_plugin": "mysql_native_password",
66+
"password": "s3cr3t",
67+
}
68+
stmt = (
69+
"CREATE USER IF NOT EXISTS '{user}'@'{host}' IDENTIFIED "
70+
"WITH {auth_plugin} BY '{password}'"
71+
).format(**user)
72+
with cnx.cursor() as cur:
73+
try:
74+
cur.execute("DROP USER IF EXISTS '{user}'@'{host}'".format(**user))
75+
cur.execute(stmt)
76+
cur.execute("DROP USER IF EXISTS '{user}'@'{host}'".format(**user))
77+
NATIVE_PASSWORD_INSTALLED = True
78+
except mysql.connector.errors.DatabaseError:
79+
try:
80+
cur.execute("INSTALL COMPONENT 'file://mysql_native_password'")
81+
NATIVE_PASSWORD_INSTALLED = True
82+
except mysql.connector.errors.DatabaseError:
83+
NATIVE_PASSWORD_INSTALLED = False
84+
85+
5686
def get_variables(cnx, pattern=None, variables=None, global_vars=False):
5787
"""Get session or global system variables
5888
@@ -545,7 +575,7 @@ def test_change_user(self):
545575
if tests.MYSQL_VERSION < (8, 0, 0):
546576
new_users = users[0:4]
547577
else:
548-
new_users = users
578+
new_users = users if NATIVE_PASSWORD_INSTALLED else users[-4:]
549579

550580
for new_user in new_users:
551581
try:
@@ -605,13 +635,17 @@ def test_change_user(self):
605635
(1, 2),
606636
(2, 3),
607637
(3, 0),
608-
(3, 4),
609-
(4, 5),
610-
(5, 3),
611-
(5, 0),
612638
]
639+
if NATIVE_PASSWORD_INSTALLED:
640+
test_cases += [
641+
(3, 4),
642+
(4, 5),
643+
(5, 3),
644+
(5, 0),
645+
]
646+
613647
for user1, user2 in test_cases:
614-
conn_args_user1 = users[user1].copy()
648+
conn_args_user1 = new_users[user1].copy()
615649
try:
616650
conn_args_user1.pop("auth_plugin")
617651
except:
@@ -623,22 +657,22 @@ def test_change_user(self):
623657
cnx_test = CMySQLConnection(**conn_args_user1)
624658
cnx_test.cmd_query("SELECT USER()")
625659
current_user = cnx_test.get_rows()[0][0][0]
626-
self.assertTrue("{user}@".format(**users[user1]) in current_user)
660+
self.assertTrue("{user}@".format(**new_users[user1]) in current_user)
627661

628662
cnx_test.cmd_change_user(
629-
users[user2]["user"],
630-
users[user2]["password"],
631-
users[user2]["database"],
663+
new_users[user2]["user"],
664+
new_users[user2]["password"],
665+
new_users[user2]["database"],
632666
)
633667
cnx_test.cmd_query("SELECT USER()")
634668
rows = cnx_test.get_rows()
635669
current_user = rows[0][0][0]
636670
self.assertNotEqual(user["user"], current_user)
637-
self.assertTrue("{user}@".format(**users[user2]) in current_user)
671+
self.assertTrue("{user}@".format(**new_users[user2]) in current_user)
638672
cnx_test.close()
639673

640674
# cleanup users
641-
for new_user in users:
675+
for new_user in new_users:
642676
try:
643677
cnx.cmd_query("DROP USER '{user}'@'{host}'".format(**new_user))
644678
except:

mysql-connector-python/tests/test_bugs.py

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,34 @@
9494
ERR_NO_CEXT = "C Extension not available"
9595

9696

97+
def setUpModule() -> None:
98+
with MySQLConnection(**tests.get_mysql_config()) as cnx:
99+
global NATIVE_PASSWORD_INSTALLED
100+
101+
user = {
102+
"user": "_bugs",
103+
"host": "%",
104+
"auth_plugin": "mysql_native_password",
105+
"password": "s3cr3t",
106+
}
107+
stmt = (
108+
"CREATE USER IF NOT EXISTS '{user}'@'{host}' IDENTIFIED "
109+
"WITH {auth_plugin} BY '{password}'"
110+
).format(**user)
111+
with cnx.cursor() as cur:
112+
try:
113+
cur.execute("DROP USER IF EXISTS '{user}'@'{host}'".format(**user))
114+
cur.execute(stmt)
115+
cur.execute("DROP USER IF EXISTS '{user}'@'{host}'".format(**user))
116+
NATIVE_PASSWORD_INSTALLED = True
117+
except mysql.connector.errors.DatabaseError:
118+
try:
119+
cur.execute("INSTALL COMPONENT 'file://mysql_native_password'")
120+
NATIVE_PASSWORD_INSTALLED = True
121+
except mysql.connector.errors.DatabaseError:
122+
NATIVE_PASSWORD_INSTALLED = False
123+
124+
97125
class Bug437972Tests(tests.MySQLConnectorTests):
98126
def test_windows_tcp_connection(self):
99127
"""lp:437972 TCP connection to Windows"""
@@ -409,6 +437,9 @@ def test_bits(self):
409437
self.cnx.close()
410438

411439

440+
@unittest.skipIf(
441+
tests.MYSQL_VERSION >= (8, 4, 0), "mysql_native_password is deprecated"
442+
)
412443
class Bug519301(tests.MySQLConnectorTests):
413444
"""lp:519301 Temporary connection failures with 2 exceptions"""
414445

@@ -2464,29 +2495,6 @@ def test_ssl(self):
24642495
class BugOra16217765(tests.MySQLConnectorTests):
24652496
"""BUG#16217765: Fix authentication plugin support"""
24662497

2467-
users = {
2468-
"sha256user": {
2469-
"username": "sha256user",
2470-
"password": "sha256P@ss",
2471-
"auth_plugin": "sha256_password",
2472-
},
2473-
"nativeuser": {
2474-
"username": "nativeuser",
2475-
"password": "nativeP@ss",
2476-
"auth_plugin": "mysql_native_password",
2477-
},
2478-
"sha256user_np": {
2479-
"username": "sha256user_np",
2480-
"password": "",
2481-
"auth_plugin": "sha256_password",
2482-
},
2483-
"nativeuser_np": {
2484-
"username": "nativeuser_np",
2485-
"password": "",
2486-
"auth_plugin": "mysql_native_password",
2487-
},
2488-
}
2489-
24902498
def _create_user(self, cnx, user, password, host, database, plugin):
24912499
self._drop_user(cnx, user, host)
24922500
create_user = "CREATE USER '{user}'@'{host}' IDENTIFIED WITH {plugin}"
@@ -2521,6 +2529,36 @@ def _drop_user(self, cnx, user, host):
25212529
pass
25222530

25232531
def setUp(self):
2532+
self.users = {
2533+
"sha256user": {
2534+
"username": "sha256user",
2535+
"password": "sha256P@ss",
2536+
"auth_plugin": "sha256_password",
2537+
},
2538+
"sha256user_np": {
2539+
"username": "sha256user_np",
2540+
"password": "",
2541+
"auth_plugin": "sha256_password",
2542+
},
2543+
}
2544+
2545+
if NATIVE_PASSWORD_INSTALLED:
2546+
self.users = {
2547+
**self.users,
2548+
**{
2549+
"nativeuser_np": {
2550+
"username": "nativeuser_np",
2551+
"password": "",
2552+
"auth_plugin": "mysql_native_password",
2553+
},
2554+
"nativeuser": {
2555+
"username": "nativeuser",
2556+
"password": "nativeP@ss",
2557+
"auth_plugin": "mysql_native_password",
2558+
},
2559+
},
2560+
}
2561+
25242562
self.errmsg = "AuthPlugin {0} failed: {1}"
25252563
config = tests.get_mysql_config()
25262564
self.host = config["host"]
@@ -2633,6 +2671,9 @@ def test_sha256_nonssl(self):
26332671
),
26342672
)
26352673
def test_native(self):
2674+
if not NATIVE_PASSWORD_INSTALLED:
2675+
self.skipTest("mysql_native_password isn't loaded")
2676+
26362677
config = tests.get_mysql_config()
26372678
config["unix_socket"] = None
26382679

@@ -6609,8 +6650,9 @@ def setUp(self):
66096650
if tests.MYSQL_VERSION < (8, 0, 0):
66106651
self.new_users = self.users[0:4]
66116652
else:
6612-
self.new_users = self.users
6613-
6653+
self.new_users = (
6654+
self.users if NATIVE_PASSWORD_INSTALLED else self.users[-4:]
6655+
)
66146656
for new_user in self.new_users:
66156657
cnx.cmd_query("DROP USER IF EXISTS '{user}'@'{host}'".format(**new_user))
66166658

@@ -6650,13 +6692,17 @@ def test_change_user(self):
66506692
(1, 2),
66516693
(2, 3),
66526694
(3, 0),
6653-
(3, 4),
6654-
(4, 5),
6655-
(5, 3),
6656-
(5, 0),
66576695
]
6696+
if NATIVE_PASSWORD_INSTALLED:
6697+
test_cases += [
6698+
(3, 4),
6699+
(4, 5),
6700+
(5, 3),
6701+
(5, 0),
6702+
]
6703+
66586704
for user1, user2 in test_cases:
6659-
conn_args_user1 = self.users[user1].copy()
6705+
conn_args_user1 = self.new_users[user1].copy()
66606706
try:
66616707
conn_args_user1.pop("auth_plugin")
66626708
except:
@@ -6668,24 +6714,28 @@ def test_change_user(self):
66686714
cnx_test = self.cnx.__class__(**conn_args_user1)
66696715
cnx_test.cmd_query("SELECT USER()")
66706716
first_user = cnx_test.get_rows()[0][0][0]
6671-
self.assertEqual("{user}@{host}".format(**self.users[user1]), first_user)
6717+
self.assertEqual(
6718+
"{user}@{host}".format(**self.new_users[user1]), first_user
6719+
)
66726720

66736721
cnx_test.cmd_change_user(
6674-
self.users[user2]["user"],
6675-
self.users[user2]["password"],
6676-
self.users[user2]["database"],
6722+
self.new_users[user2]["user"],
6723+
self.new_users[user2]["password"],
6724+
self.new_users[user2]["database"],
66776725
)
66786726
cnx_test.cmd_query("SELECT USER()")
66796727
rows = cnx_test.get_rows()
66806728
current_user = rows[0][0][0]
66816729
self.assertNotEqual(first_user, current_user)
6682-
self.assertEqual("{user}@{host}".format(**self.users[user2]), current_user)
6730+
self.assertEqual(
6731+
"{user}@{host}".format(**self.new_users[user2]), current_user
6732+
)
66836733
cnx_test.close()
66846734

66856735
def tearDown(self):
66866736
cnx = MySQLConnection(**self.connect_kwargs)
66876737
# cleanup users
6688-
for new_user in self.users:
6738+
for new_user in self.new_users:
66896739
cnx.cmd_query("DROP USER IF EXISTS '{user}'@'{host}'".format(**new_user))
66906740

66916741

mysqlx-connector-python/tests/test_mysqlx_connection.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,13 @@ def test_auth(self):
676676
except mysqlx.errors.OperationalError:
677677
pass
678678

679+
native_auth = (
680+
"mysql_native_password"
681+
if tests.MYSQL_VERSION < (8, 4, 0)
682+
else "caching_sha2_password"
683+
)
679684
sess.sql(
680-
"CREATE USER 'native'@'%' IDENTIFIED WITH "
681-
"mysql_native_password BY 'test'"
685+
f"CREATE USER 'native'@'%' IDENTIFIED WITH {native_auth} BY 'test'"
682686
).execute()
683687
sess.sql(
684688
"CREATE USER 'sha256'@'%' IDENTIFIED WITH sha256_password BY 'sha256'"
@@ -694,8 +698,9 @@ def test_auth(self):
694698
config["auth"] = "plain"
695699
mysqlx.get_session(config)
696700

697-
config["auth"] = "mysql41"
698-
mysqlx.get_session(config)
701+
if tests.MYSQL_VERSION < (8, 4, 0):
702+
config["auth"] = "mysql41"
703+
mysqlx.get_session(config)
699704

700705
config["user"] = "sha256"
701706
config["password"] = "sha256"
@@ -1059,7 +1064,7 @@ def test_get_default_schema(self):
10591064
self.session.sql("DROP USER IF EXISTS 'def_schema'@'%'").execute()
10601065
self.session.sql(
10611066
"CREATE USER 'def_schema'@'%' IDENTIFIED WITH "
1062-
"mysql_native_password BY 'test'"
1067+
"caching_sha2_password BY 'test'"
10631068
).execute()
10641069
settings = self.connect_kwargs.copy()
10651070
settings["user"] = "def_schema"
@@ -2592,6 +2597,9 @@ def test_compression_algorithms(self):
25922597
self._set_compression_algorithms(default_algorithms)
25932598

25942599

2600+
@unittest.skipIf(
2601+
tests.MYSQL_VERSION >= (8, 4, 0), "mysql_native_password is deprecated"
2602+
)
25952603
class WL14852(tests.MySQLxTests):
25962604
"""WL#14852: Align TLS and SSL options checking and behavior"""
25972605

0 commit comments

Comments
 (0)