Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(mysql): create test for passwordless auth (#3484) #3505

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/mysql/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use anyhow::Context;
use futures::TryStreamExt;
use sqlx::mysql::{MySql, MySqlConnection, MySqlPool, MySqlPoolOptions, MySqlRow};
use sqlx::{Column, Connection, Executor, Row, Statement, TypeInfo};
use sqlx_core::connection::ConnectOptions;
use sqlx_mysql::MySqlConnectOptions;
use sqlx_test::{new, setup_if_needed};
use std::env;
use url::Url;

#[sqlx_macros::test]
async fn it_connects() -> anyhow::Result<()> {
Expand All @@ -14,6 +18,27 @@ async fn it_connects() -> anyhow::Result<()> {
Ok(())
}

#[sqlx_macros::test]
async fn it_connects_without_password() -> anyhow::Result<()> {
setup_if_needed();

let mut url = Url::parse(&env::var("DATABASE_URL").context("expected DATABASE_URL")?)
.context("error parsing DATABASE_URL")?;

url.set_username("no_password").unwrap();
url.set_password(None).unwrap();

let mut conn = MySqlConnectOptions::from_url(&url)?.connect().await?;

let vars = sqlx::raw_sql("SHOW VARIABLES").fetch_all(&mut conn).await?;

assert!(!vars.is_empty());

conn.close().await?;

Ok(())
}

#[sqlx_macros::test]
async fn it_maths() -> anyhow::Result<()> {
let mut conn = new::<MySql>().await?;
Expand Down
8 changes: 8 additions & 0 deletions tests/mysql/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ CREATE TABLE products (
name TEXT,
price NUMERIC CHECK (price > 0)
);

-- Create a user without a password to test passwordless auth.
CREATE USER 'no_password'@'%';

-- The minimum privilege apparently needed to connect to a specific database.
-- Granting no privileges, or just `GRANT USAGE`, gives an "access denied" error.
-- https://github.com/launchbadge/sqlx/issues/3484#issuecomment-2350901546
GRANT SELECT ON sqlx.* TO 'no_password'@'%';