From b09ae0d7bb095ff71b5356f489bdef462688b23a Mon Sep 17 00:00:00 2001 From: khai96_ Date: Sat, 4 May 2019 13:29:38 +0700 Subject: Add integration test where it rejects authentication --- tests/auth.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/auth.rs b/tests/auth.rs index f43553b..e52df52 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -24,7 +24,7 @@ use std::time::Duration; "testpassword" ), )] -fn auth_works( +fn auth_accepts( tmpdir: TempDir, port: u16, cli_auth_arg: &str, @@ -57,3 +57,60 @@ fn auth_works( Ok(()) } + +#[rstest_parametrize( + cli_auth_arg, client_username, client_password, + case("rightuser:rightpassword", "wronguser", "rightpassword"), + case( + "rightuser:sha256:314eee236177a721d0e58d3ca4ff01795cdcad1e8478ba8183a2e58d69c648c0", + "wronguser", + "rightpassword" + ), + case( + "rightuser:sha512:84ec4056571afeec9f5b59453305877e9a66c3f9a1d91733fde759b370c1d540b9dc58bfc88c5980ad2d020c3a8ee84f21314a180856f5a82ba29ecba29e2cab", + "wronguser", + "rightpassword" + ), + case("rightuser:rightpassword", "rightuser", "wrongpassword"), + case( + "rightuser:sha256:314eee236177a721d0e58d3ca4ff01795cdcad1e8478ba8183a2e58d69c648c0", + "rightuser", + "wrongpassword" + ), + case( + "rightuser:sha512:84ec4056571afeec9f5b59453305877e9a66c3f9a1d91733fde759b370c1d540b9dc58bfc88c5980ad2d020c3a8ee84f21314a180856f5a82ba29ecba29e2cab", + "rightuser", + "wrongpassword" + ), +)] +fn auth_rejects( + tmpdir: TempDir, + port: u16, + cli_auth_arg: &str, + client_username: &str, + client_password: &str, +) -> Result<(), Error> { + let mut child = Command::cargo_bin("miniserve")? + .arg(tmpdir.path()) + .arg("-p") + .arg(port.to_string()) + .arg("-a") + .arg(cli_auth_arg) + .stdout(Stdio::null()) + .spawn()?; + + sleep(Duration::from_secs(1)); + + let client = reqwest::Client::new(); + let status = client + .get(format!("http://localhost:{}", port).as_str()) + .basic_auth(client_username, Some(client_password)) + .send()? + .status(); + + assert_eq!(status.canonical_reason(), Some("Unauthorized")); + + child.kill()?; + + Ok(()) +} -- cgit v1.2.3 From 67b2ec8e70d0f781b3f57762c1e8e28c2102c15d Mon Sep 17 00:00:00 2001 From: khai96_ Date: Sat, 4 May 2019 13:36:32 +0700 Subject: Add assertion of status code to auth_works --- tests/auth.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/auth.rs b/tests/auth.rs index f43553b..af2db58 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -43,11 +43,15 @@ fn auth_works( sleep(Duration::from_secs(1)); let client = reqwest::Client::new(); - let body = client + let response = client .get(format!("http://localhost:{}", port).as_str()) .basic_auth(client_username, Some(client_password)) - .send()? - .error_for_status()?; + .send()?; + + let status_code = response.status(); + assert_eq!(status_code.canonical_reason(), Some("OK")); + + let body = response.error_for_status()?; let parsed = Document::from_read(body)?; for &file in FILES { assert!(parsed.find(Text).any(|x| x.text() == file)); -- cgit v1.2.3 From 8870039e1dbc2ab1cc31018554373c4cbcc7e357 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" Date: Mon, 6 May 2019 04:39:31 +0000 Subject: Bump rstest from `9f2ac13` to `833bca6` Bumps [rstest](https://github.com/la10736/rstest) from `9f2ac13` to `833bca6`. - [Release notes](https://github.com/la10736/rstest/releases) - [Commits](https://github.com/la10736/rstest/compare/9f2ac13853e876ffef76f49443e7f2eca17cba92...833bca6718f929992c57170a98f256cec3ac2036) Signed-off-by: dependabot[bot] --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9734566..f014b99 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1744,7 +1744,7 @@ dependencies = [ [[package]] name = "rstest" version = "0.2.2" -source = "git+https://github.com/la10736/rstest.git#9f2ac13853e876ffef76f49443e7f2eca17cba92" +source = "git+https://github.com/la10736/rstest.git#833bca6718f929992c57170a98f256cec3ac2036" dependencies = [ "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.29 (registry+https://github.com/rust-lang/crates.io-index)", -- cgit v1.2.3 From d3d90f1b46c441f4bd9066d671bc2119665f2f1e Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 6 May 2019 20:58:22 +0700 Subject: Use constant for assertion --- tests/auth.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/auth.rs b/tests/auth.rs index e52df52..96cdcb5 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -9,6 +9,7 @@ use select::predicate::Text; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; +use reqwest::StatusCode; #[rstest_parametrize( cli_auth_arg, client_username, client_password, @@ -108,7 +109,7 @@ fn auth_rejects( .send()? .status(); - assert_eq!(status.canonical_reason(), Some("Unauthorized")); + assert_eq!(status, StatusCode::UNAUTHORIZED); child.kill()?; -- cgit v1.2.3 From 03b4cbad39e17cc6f083b77801177daba101d169 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 6 May 2019 21:02:19 +0700 Subject: Use constant for assertion --- tests/auth.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/auth.rs b/tests/auth.rs index af2db58..9882a39 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -9,6 +9,7 @@ use select::predicate::Text; use std::process::{Command, Stdio}; use std::thread::sleep; use std::time::Duration; +use reqwest::StatusCode; #[rstest_parametrize( cli_auth_arg, client_username, client_password, @@ -49,7 +50,7 @@ fn auth_works( .send()?; let status_code = response.status(); - assert_eq!(status_code.canonical_reason(), Some("OK")); + assert_eq!(status_code, StatusCode::OK); let body = response.error_for_status()?; let parsed = Document::from_read(body)?; -- cgit v1.2.3 From 8bcf7ba0376c0ab54938624a8798cb4e7e156448 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Mon, 6 May 2019 21:13:42 +0700 Subject: Add example for --auth formatting --- src/args.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/args.rs b/src/args.rs index 63799a0..a67583a 100644 --- a/src/args.rs +++ b/src/args.rs @@ -40,6 +40,7 @@ struct CLIArgs { /// Set authentication. Currently supported formats: /// username:password, username:sha256:hash, username:sha512:hash + /// (e.g. joe:123, joe:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3) #[structopt(short = "a", long = "auth", parse(try_from_str = "parse_auth"))] auth: Option, -- cgit v1.2.3