aboutsummaryrefslogtreecommitdiffstats
path: root/src/auth.rs
diff options
context:
space:
mode:
authorkhai96_ <hvksmr1996@gmail.com>2019-04-25 12:13:18 +0000
committerkhai96_ <hvksmr1996@gmail.com>2019-04-25 12:13:18 +0000
commit0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb (patch)
tree5826f8710efc79376a92c0f50e284e3dea80fafc /src/auth.rs
parentUpdate Cargo.lock (diff)
downloadminiserve-0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb.tar.gz
miniserve-0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb.zip
Use rstest_parametrize for unit tests
Diffstat (limited to '')
-rw-r--r--src/auth.rs112
1 files changed, 44 insertions, 68 deletions
diff --git a/src/auth.rs b/src/auth.rs
index a4b3555..2db422d 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -115,31 +115,26 @@ impl Middleware<crate::MiniserveConfig> for Auth {
#[cfg(test)]
mod tests {
use super::*;
+ use rstest::rstest_parametrize;
- fn assert_hex_eq(expectation: &str, received: Vec<u8>) {
- let bin = hex::decode(expectation).expect("Provided string is not a valid hex code");
- assert_eq!(bin, received);
- }
-
- #[test]
- fn get_hash_hex_sha256() {
- let expectation = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
- let received = get_hash::<Sha256>("abc".to_owned());
- assert_hex_eq(expectation, received);
+ fn get_hash_func(name: &str) -> impl FnOnce(String) -> Vec<u8> {
+ match name {
+ "sha256" => get_hash::<Sha256>,
+ "sha512" => get_hash::<Sha512>,
+ _ => panic!("Invalid hash method"),
+ }
}
- #[test]
- fn get_hash_hex_sha512() {
- let expectation = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
- let received = get_hash::<Sha512>("abc".to_owned());
- assert_hex_eq(expectation, received);
- }
-
- fn create_auth_params(username: &str, password: &str) -> BasicAuthParams {
- BasicAuthParams {
- username: username.to_owned(),
- password: password.to_owned(),
- }
+ #[rstest_parametrize(
+ password, hash_method, hash,
+ case("abc", "sha256", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"),
+ case("abc", "sha512", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"),
+ )]
+ fn test_get_hash(password: &str, hash_method: &str, hash: &str) {
+ let hash_func = get_hash_func(hash_method);
+ let expected = hex::decode(hash).expect("Provided hash is not a valid hex code");
+ let received = hash_func(password.to_owned());
+ assert_eq!(received, expected);
}
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> RequiredAuth {
@@ -156,51 +151,32 @@ mod tests {
}
}
- #[test]
- fn match_auth_plain_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "plain"),
- ));
- }
-
- #[test]
- fn match_auth_plain_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "plain"),
- ));
- }
-
- #[test]
- fn match_auth_sha256_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "sha256"),
- ));
- }
-
- #[test]
- fn match_auth_sha256_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "sha256"),
- ));
- }
-
- #[test]
- fn match_auth_sha512_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "sha512"),
- ));
- }
-
- #[test]
- fn match_auth_sha512_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "sha512"),
- ));
+ #[rstest_parametrize(
+ should_pass, param_username, param_password, required_username, required_password, encrypt,
+ case(true, "obi", "hello there", "obi", "hello there", "plain"),
+ case(false, "obi", "hello there", "obi", "hi!", "plain"),
+ case(true, "obi", "hello there", "obi", "hello there", "sha256"),
+ case(false, "obi", "hello there", "obi", "hi!", "sha256"),
+ case(true, "obi", "hello there", "obi", "hello there", "sha512"),
+ case(false, "obi", "hello there", "obi", "hi!", "sha512"),
+ )]
+ fn test_auth(
+ should_pass: bool,
+ param_username: &str,
+ param_password: &str,
+ required_username: &str,
+ required_password: &str,
+ encrypt: &str,
+ ) {
+ assert_eq!(
+ match_auth(
+ BasicAuthParams {
+ username: param_username.to_owned(),
+ password: param_password.to_owned(),
+ },
+ &create_required_auth(required_username, required_password, encrypt),
+ ),
+ should_pass,
+ )
}
}