diff options
-rw-r--r-- | src/args.rs | 1 | ||||
-rw-r--r-- | src/auth.rs | 32 | ||||
-rw-r--r-- | tests/auth.rs | 31 | ||||
-rw-r--r-- | tests/navigation.rs | 3 |
4 files changed, 30 insertions, 37 deletions
diff --git a/src/args.rs b/src/args.rs index 47f4d1f..8fde78a 100644 --- a/src/args.rs +++ b/src/args.rs @@ -172,6 +172,7 @@ pub fn parse_args() -> crate::MiniserveConfig { mod tests { use super::*; use rstest::rstest_parametrize; + use pretty_assertions::assert_eq; /// Helper function that creates a `RequiredAuth` structure fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth { diff --git a/src/auth.rs b/src/auth.rs index 2c98622..1c98c10 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -55,19 +55,17 @@ pub fn parse_basic_auth( /// Return `true` if `basic_auth` is matches any of `required_auth` pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &[RequiredAuth]) -> bool { - required_auth.iter().any( - |RequiredAuth { username, password }| - basic_auth.username == *username && - compare_password(&basic_auth.password, password) - ) + required_auth + .iter() + .any(|RequiredAuth { username, password }| { + basic_auth.username == *username && compare_password(&basic_auth.password, password) + }) } /// Return `true` if `basic_auth_pwd` meets `required_auth_pwd`'s requirement -pub fn compare_password (basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool { +pub fn compare_password(basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool { match &required_auth_pwd { - RequiredAuthPassword::Plain(required_password) => { - *basic_auth_pwd == *required_password - } + RequiredAuthPassword::Plain(required_password) => *basic_auth_pwd == *required_password, RequiredAuthPassword::Sha256(password_hash) => { compare_hash::<Sha256>(basic_auth_pwd, password_hash) } @@ -106,16 +104,9 @@ impl Middleware<crate::MiniserveConfig> for Auth { Ok(auth_req) => auth_req, Err(err) => { let auth_err = ContextualError::HTTPAuthenticationError(Box::new(err)); - return Ok(Response::Done( - HttpResponse::BadRequest().body( - build_unauthorized_response( - &req, - auth_err, - true, - StatusCode::BAD_REQUEST, - ), - ), - )); + return Ok(Response::Done(HttpResponse::BadRequest().body( + build_unauthorized_response(&req, auth_err, true, StatusCode::BAD_REQUEST), + ))); } }; @@ -135,7 +126,7 @@ impl Middleware<crate::MiniserveConfig> for Auth { ContextualError::InvalidHTTPCredentials, true, StatusCode::UNAUTHORIZED, - )) + )), )) } } @@ -178,6 +169,7 @@ fn build_unauthorized_response( mod tests { use super::*; use rstest::{rstest, rstest_parametrize, fixture}; + use pretty_assertions::assert_eq; /// Return a hashing function corresponds to given name fn get_hash_func(name: &str) -> impl FnOnce(&str) -> Vec<u8> { diff --git a/tests/auth.rs b/tests/auth.rs index da0e576..0afaefd 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -3,6 +3,7 @@ mod fixtures; use assert_cmd::prelude::*; use assert_fs::fixture::TempDir; use fixtures::{port, tmpdir, Error, FILES}; +use pretty_assertions::assert_eq; use reqwest::StatusCode; use rstest::{rstest, rstest_parametrize}; use select::document::Document; @@ -136,17 +137,19 @@ fn register_accounts<'a>(command: &'a mut Command) -> &'a mut Command { .arg("--auth") .arg("usr4:sha512:68050a967d061ac480b414bc8f9a6d368ad0082203edcd23860e94c36178aad1a038e061716707d5479e23081a6d920dc6e9f88e5eb789cdd23e211d718d161a") // pwd4 .arg("--auth") - .arg("usr5:sha512:be82a7dccd06122f9e232e9730e67e69e30ec61b268fd9b21a5e5d42db770d45586a1ce47816649a0107e9fadf079d9cf0104f0a3aaa0f67bad80289c3ba25a8") // pwd5 + .arg("usr5:sha512:be82a7dccd06122f9e232e9730e67e69e30ec61b268fd9b21a5e5d42db770d45586a1ce47816649a0107e9fadf079d9cf0104f0a3aaa0f67bad80289c3ba25a8") + // pwd5 } #[rstest_parametrize( - username, password, + username, + password, case("usr0", "pwd0"), case("usr1", "pwd1"), case("usr2", "pwd2"), case("usr3", "pwd3"), case("usr4", "pwd4"), - case("usr5", "pwd5"), + case("usr5", "pwd5") )] fn auth_multiple_accounts_pass( tmpdir: TempDir, @@ -154,9 +157,7 @@ fn auth_multiple_accounts_pass( username: &str, password: &str, ) -> Result<(), Error> { - let mut child = register_accounts( - &mut Command::cargo_bin("miniserve")? - ) + let mut child = register_accounts(&mut Command::cargo_bin("miniserve")?) .arg("-p") .arg(port.to_string()) .arg(tmpdir.path()) @@ -187,19 +188,16 @@ fn auth_multiple_accounts_pass( } #[rstest] -fn auth_multiple_accounts_wrong_username( - tmpdir: TempDir, - port: u16 -) -> Result<(), Error> { +fn auth_multiple_accounts_wrong_username(tmpdir: TempDir, port: u16) -> Result<(), Error> { let mut child = register_accounts( Command::cargo_bin("miniserve")? .arg(tmpdir.path()) .arg("-p") .arg(port.to_string()) .stdout(Stdio::null()) - .stderr(Stdio::null()) + .stderr(Stdio::null()), ) - .spawn()?; + .spawn()?; sleep(Duration::from_secs(1)); @@ -219,13 +217,14 @@ fn auth_multiple_accounts_wrong_username( } #[rstest_parametrize( - username, password, + username, + password, case("usr0", "pwd5"), case("usr1", "pwd4"), case("usr2", "pwd3"), case("usr3", "pwd2"), case("usr4", "pwd1"), - case("usr5", "pwd0"), + case("usr5", "pwd0") )] fn auth_multiple_accounts_wrong_password( tmpdir: TempDir, @@ -239,9 +238,9 @@ fn auth_multiple_accounts_wrong_password( .arg("-p") .arg(port.to_string()) .stdout(Stdio::null()) - .stderr(Stdio::null()) + .stderr(Stdio::null()), ) - .spawn()?; + .spawn()?; sleep(Duration::from_secs(1)); diff --git a/tests/navigation.rs b/tests/navigation.rs index cf2d8c5..94e7355 100644 --- a/tests/navigation.rs +++ b/tests/navigation.rs @@ -4,13 +4,14 @@ mod utils; use assert_cmd::prelude::*; use assert_fs::fixture::TempDir; use fixtures::{port, tmpdir, Error, DEEPLY_NESTED_FILE, DIRECTORIES}; +use pretty_assertions::{assert_eq, assert_ne}; use rstest::rstest; use select::document::Document; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; -use utils::get_link_from_text; use url::Url; +use utils::get_link_from_text; #[rstest] /// The index directory gets a trailing slash. |