Simplify test code a bit, use Request::new() to simplfy constructing requests

This commit is contained in:
Barry Pederson
2024-11-03 12:49:23 -08:00
parent 4d70463e46
commit 60bc980e8d
4 changed files with 24 additions and 39 deletions
+6 -6
View File
@@ -9,12 +9,12 @@ fn main() -> std::io::Result<()> {
let mut stream = UnixStream::connect(&OPTIONS.socket_name)?; let mut stream = UnixStream::connect(&OPTIONS.socket_name)?;
println!("Connected to server"); println!("Connected to server");
let request = Request { let request = Request::new(
userid: OPTIONS.username.clone(), &OPTIONS.username,
password: OPTIONS.password.clone(), &OPTIONS.password,
service: "ignore-this".to_string(), "ignore-this",
realm: "also-ignore-this".to_string(), "also-ignore-this",
}; );
request.write_to_stream(&mut stream)?; request.write_to_stream(&mut stream)?;
let response = read_string(&mut stream)?; let response = read_string(&mut stream)?;
+1 -1
View File
@@ -45,7 +45,7 @@ mod tests {
const TEST_PATH: &str = "./test_passwords"; const TEST_PATH: &str = "./test_passwords";
#[test] #[test]
fn it_communicates_ok() { fn it_handles_a_request() {
let repository = PasswordDirectory::new(TEST_PATH); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request::new("bp", "some-rad-password", "ignore-this", "also-ignore-this"); let request = Request::new("bp", "some-rad-password", "ignore-this", "also-ignore-this");
+13 -24
View File
@@ -48,12 +48,8 @@ mod tests {
#[test] #[test]
fn it_checks_good_password() { fn it_checks_good_password() {
let repository = PasswordDirectory::new(TEST_PATH); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(), let request = Request::new("bp", "some-rad-password", "ignore-this", "also-ignore-this");
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(repository.check_auth(&request), true); assert_eq!(repository.check_auth(&request), true);
} }
@@ -61,12 +57,8 @@ mod tests {
#[test] #[test]
fn it_fails_bad_password() { fn it_fails_bad_password() {
let repository = PasswordDirectory::new(TEST_PATH); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(), let request = Request::new("bp", "not-correct", "ignore-this", "also-ignore-this");
password: "not-correct".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(repository.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
@@ -74,12 +66,13 @@ mod tests {
#[test] #[test]
fn it_fails_bad_userid() { fn it_fails_bad_userid() {
let repository = PasswordDirectory::new(TEST_PATH); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp-xxx".to_string(), let request = Request::new(
password: "some-rad-password".to_string(), "bp-xxx",
service: "ignore-this".to_string(), "some-rad-password",
realm: "also-ignore-this".to_string(), "ignore-this",
}; "also-ignore-this",
);
assert_eq!(repository.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
@@ -87,12 +80,8 @@ mod tests {
#[test] #[test]
fn it_ignores_comments() { fn it_ignores_comments() {
let repository = PasswordDirectory::new(TEST_PATH); let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(), let request = Request::new("bp-xxx", "commented-out", "ignore-this", "also-ignore-this");
password: "commented-out".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(repository.check_auth(&request), false); assert_eq!(repository.check_auth(&request), false);
} }
+4 -8
View File
@@ -5,10 +5,9 @@ use std::io::Cursor;
fn it_reads_strings() { fn it_reads_strings() {
let buffer = [0x0, 0x3, b'F', b'o', b'o']; let buffer = [0x0, 0x3, b'F', b'o', b'o'];
assert_eq!( let result = read_string(&mut buffer.as_ref()).unwrap();
read_string(&mut buffer.as_ref()).unwrap(),
"Foo".to_string() assert_eq!(result, "Foo");
);
} }
#[test] #[test]
@@ -17,8 +16,5 @@ fn it_writes_strings() {
write_string(&mut buffer, "Foo").unwrap(); write_string(&mut buffer, "Foo").unwrap();
// Should have written exactly 5 bytes assert_eq!(buffer.into_inner(), [0x0, 0x3, b'F', b'o', b'o']);
assert_eq!(buffer.get_ref().len(), 5usize);
assert_eq!(&buffer.get_ref()[0..5], [0x0, 0x3, b'F', b'o', b'o']);
} }