aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorProudmuslim <proudmuslim-dev@protonmail.com>2023-08-08 19:12:46 +0000
committerProudmuslim <proudmuslim-dev@protonmail.com>2023-08-08 19:12:46 +0000
commit3dbebb2493f4a29628529adf1539d3b841ec007a (patch)
treec7a51933d6cbe011f2e06e5bce9cd3fc7f00cde1 /tests
parentAdd `--auth-file` argument to README.md (diff)
downloadminiserve-3dbebb2493f4a29628529adf1539d3b841ec007a.tar.gz
miniserve-3dbebb2493f4a29628529adf1539d3b841ec007a.zip
Add tests for `--auth-file` flag
Diffstat (limited to 'tests')
-rw-r--r--tests/auth_file.rs62
-rw-r--r--tests/data/auth1.txt3
2 files changed, 65 insertions, 0 deletions
diff --git a/tests/auth_file.rs b/tests/auth_file.rs
new file mode 100644
index 0000000..e022a3d
--- /dev/null
+++ b/tests/auth_file.rs
@@ -0,0 +1,62 @@
+mod fixtures;
+
+use fixtures::{server, server_no_stderr, Error, FILES};
+use http::StatusCode;
+use reqwest::blocking::Client;
+use rstest::rstest;
+use select::document::Document;
+use select::predicate::Text;
+
+#[rstest(
+ cli_auth_file_arg, client_username, client_password,
+ case("tests/data/auth1.txt", "joe", "123"),
+ case("tests/data/auth1.txt", "bob", "123"),
+ case("tests/data/auth1.txt", "bill", ""),
+)]
+fn auth_file_accepts(
+ cli_auth_file_arg: &str,
+ client_username: &str,
+ client_password: &str
+) -> Result<(), Error> {
+ let server = server(&["--auth-file", cli_auth_file_arg]);
+ let client = Client::new();
+ let response = client
+ .get(server.url())
+ .basic_auth(client_username, Some(client_password))
+ .send()?;
+
+ let status_code = response.status();
+ assert_eq!(status_code, StatusCode::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));
+ }
+
+ Ok(())
+}
+
+#[rstest(
+ cli_auth_file_arg, client_username, client_password,
+ case("tests/data/auth1.txt", "joe", "wrongpassword"),
+ case("tests/data/auth1.txt", "bob", ""),
+ case("tests/data/auth1.txt", "nonexistentuser", "wrongpassword"),
+)]
+fn auth_file_rejects(
+ cli_auth_file_arg: &str,
+ client_username: &str,
+ client_password: &str,
+) -> Result<(), Error> {
+ let server = server_no_stderr(&["--auth-file", cli_auth_file_arg]);
+ let client = Client::new();
+ let status = client
+ .get(server.url())
+ .basic_auth(client_username, Some(client_password))
+ .send()?
+ .status();
+
+ assert_eq!(status, StatusCode::UNAUTHORIZED);
+
+ Ok(())
+}
diff --git a/tests/data/auth1.txt b/tests/data/auth1.txt
new file mode 100644
index 0000000..3744d61
--- /dev/null
+++ b/tests/data/auth1.txt
@@ -0,0 +1,3 @@
+joe:123
+bob:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
+bill: