From e7a21be51f1761e1184cdd7f53a4da77edb558b0 Mon Sep 17 00:00:00 2001 From: brianheineman Date: Thu, 2 May 2024 18:02:23 -0600 Subject: [PATCH] test: add authentication tests --- Cargo.lock | 31 ++++++++----- postgresql_embedded/src/postgresql.rs | 2 - postgresql_embedded/tests/postgresql.rs | 58 ++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d6cc3b..9d033bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,47 +68,48 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstream" -version = "0.6.13" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" dependencies = [ "windows-sys 0.52.0", ] [[package]] name = "anstyle-wincon" -version = "3.0.2" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", "windows-sys 0.52.0", @@ -328,9 +329,9 @@ checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "colorchoice" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "const-oid" @@ -1026,6 +1027,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.10.5" diff --git a/postgresql_embedded/src/postgresql.rs b/postgresql_embedded/src/postgresql.rs index 5deb4d3..e2ecdf1 100644 --- a/postgresql_embedded/src/postgresql.rs +++ b/postgresql_embedded/src/postgresql.rs @@ -338,7 +338,6 @@ impl PostgreSQL { self.settings.port ); let psql = PsqlBuilder::from(&self.settings) - .program_dir(self.settings.binary_dir()) .command(format!( "SELECT 1 FROM pg_database WHERE datname='{}'", database_name.as_ref() @@ -366,7 +365,6 @@ impl PostgreSQL { self.settings.port ); let psql = PsqlBuilder::from(&self.settings) - .program_dir(self.settings.binary_dir()) .command(format!( "DROP DATABASE IF EXISTS \"{}\"", database_name.as_ref() diff --git a/postgresql_embedded/tests/postgresql.rs b/postgresql_embedded/tests/postgresql.rs index 6a059cf..de040ad 100644 --- a/postgresql_embedded/tests/postgresql.rs +++ b/postgresql_embedded/tests/postgresql.rs @@ -1,5 +1,7 @@ use anyhow::bail; use postgresql_archive::LATEST; +use postgresql_commands::psql::PsqlBuilder; +use postgresql_commands::CommandBuilder; use postgresql_embedded::{PostgreSQL, Result, Settings, Status}; use std::fs::{remove_dir_all, remove_file}; use test_log::test; @@ -144,11 +146,63 @@ async fn postgres_concurrency() -> anyhow::Result<()> { Ok(()) } +#[test(tokio::test)] +async fn test_authentication_success() -> Result<()> { + let mut postgresql = PostgreSQL::default(); + postgresql.setup().await?; + postgresql.start().await?; + + let mut psql = PsqlBuilder::from(postgresql.settings()) + .command("SELECT 1") + .no_psqlrc() + .tuples_only() + .build(); + + let output = psql.output()?; + assert!(output.status.success()); + Ok(()) +} + +#[test(tokio::test)] +async fn test_authentication_invalid_username() -> Result<()> { + let mut postgresql = PostgreSQL::default(); + postgresql.setup().await?; + postgresql.start().await?; + + let mut psql = PsqlBuilder::from(postgresql.settings()) + .command("SELECT 1") + .username("invalid") + .no_psqlrc() + .tuples_only() + .build(); + + let output = psql.output()?; + assert!(!output.status.success()); + Ok(()) +} + +#[test(tokio::test)] +async fn test_authentication_invalid_password() -> Result<()> { + let mut postgresql = PostgreSQL::default(); + postgresql.setup().await?; + postgresql.start().await?; + + let mut psql = PsqlBuilder::from(postgresql.settings()) + .command("SELECT 1") + .pg_password("invalid") + .no_psqlrc() + .tuples_only() + .build(); + + let output = psql.output()?; + assert!(!output.status.success()); + Ok(()) +} + #[test(tokio::test)] async fn test_username_setting() -> Result<()> { - let username = "admin".to_string(); let settings = Settings { - username, + username: "admin".to_string(), ..Default::default() }; let mut postgresql = PostgreSQL::new(LATEST, settings);