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(-) (limited to 'tests/auth.rs') 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(-) (limited to 'tests/auth.rs') 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 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(-) (limited to 'tests/auth.rs') 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(-) (limited to 'tests/auth.rs') 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 09105cf479b25171cbe89d7ff3ed4f3feee16c01 Mon Sep 17 00:00:00 2001 From: boastful-squirrel Date: Fri, 10 May 2019 18:18:21 +0200 Subject: cargo fmt --- tests/auth.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/auth.rs') diff --git a/tests/auth.rs b/tests/auth.rs index 0e5f9b7..128047d 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -3,13 +3,13 @@ mod fixtures; use assert_cmd::prelude::*; use assert_fs::fixture::TempDir; use fixtures::{port, tmpdir, Error, FILES}; +use reqwest::StatusCode; use rstest::rstest_parametrize; use select::document::Document; 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, -- cgit v1.2.3