Skip to content

Commit 2f5ba71

Browse files
authored
chore(mysql): create test for passwordless auth (#3484) (#3505)
This isn't a solution for #3484, as that seems to be an issue with privileges on the user's side. However, in the process of figuring that out, I realized we never explicitly test password-less auth.
1 parent dc32f99 commit 2f5ba71

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tests/mysql/mysql.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
use anyhow::Context;
12
use futures::TryStreamExt;
23
use sqlx::mysql::{MySql, MySqlConnection, MySqlPool, MySqlPoolOptions, MySqlRow};
34
use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
5+
use sqlx_core::connection::ConnectOptions;
6+
use sqlx_mysql::MySqlConnectOptions;
47
use sqlx_test::{new, setup_if_needed};
58
use std::env;
9+
use url::Url;
610

711
#[sqlx_macros::test]
812
async fn it_connects() -> anyhow::Result<()> {
@@ -14,6 +18,27 @@ async fn it_connects() -> anyhow::Result<()> {
1418
Ok(())
1519
}
1620

21+
#[sqlx_macros::test]
22+
async fn it_connects_without_password() -> anyhow::Result<()> {
23+
setup_if_needed();
24+
25+
let mut url = Url::parse(&env::var("DATABASE_URL").context("expected DATABASE_URL")?)
26+
.context("error parsing DATABASE_URL")?;
27+
28+
url.set_username("no_password").unwrap();
29+
url.set_password(None).unwrap();
30+
31+
let mut conn = MySqlConnectOptions::from_url(&url)?.connect().await?;
32+
33+
let vars = sqlx::raw_sql("SHOW VARIABLES").fetch_all(&mut conn).await?;
34+
35+
assert!(!vars.is_empty());
36+
37+
conn.close().await?;
38+
39+
Ok(())
40+
}
41+
1742
#[sqlx_macros::test]
1843
async fn it_maths() -> anyhow::Result<()> {
1944
let mut conn = new::<MySql>().await?;

tests/mysql/setup.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ CREATE TABLE products (
2222
name TEXT,
2323
price NUMERIC CHECK (price > 0)
2424
);
25+
26+
-- Create a user without a password to test passwordless auth.
27+
CREATE USER 'no_password'@'%';
28+
29+
-- The minimum privilege apparently needed to connect to a specific database.
30+
-- Granting no privileges, or just `GRANT USAGE`, gives an "access denied" error.
31+
-- https://github.com/launchbadge/sqlx/issues/3484#issuecomment-2350901546
32+
GRANT SELECT ON sqlx.* TO 'no_password'@'%';

0 commit comments

Comments
 (0)