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)?;
println!("Connected to server");
let request = Request {
userid: OPTIONS.username.clone(),
password: OPTIONS.password.clone(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
let request = Request::new(
&OPTIONS.username,
&OPTIONS.password,
"ignore-this",
"also-ignore-this",
);
request.write_to_stream(&mut stream)?;
let response = read_string(&mut stream)?;
+1 -1
View File
@@ -45,7 +45,7 @@ mod tests {
const TEST_PATH: &str = "./test_passwords";
#[test]
fn it_communicates_ok() {
fn it_handles_a_request() {
let repository = PasswordDirectory::new(TEST_PATH);
let request = Request::new("bp", "some-rad-password", "ignore-this", "also-ignore-this");
+13 -24
View File
@@ -48,12 +48,8 @@ mod tests {
#[test]
fn it_checks_good_password() {
let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(),
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
let request = Request::new("bp", "some-rad-password", "ignore-this", "also-ignore-this");
assert_eq!(repository.check_auth(&request), true);
}
@@ -61,12 +57,8 @@ mod tests {
#[test]
fn it_fails_bad_password() {
let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(),
password: "not-correct".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
let request = Request::new("bp", "not-correct", "ignore-this", "also-ignore-this");
assert_eq!(repository.check_auth(&request), false);
}
@@ -74,12 +66,13 @@ mod tests {
#[test]
fn it_fails_bad_userid() {
let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp-xxx".to_string(),
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
let request = Request::new(
"bp-xxx",
"some-rad-password",
"ignore-this",
"also-ignore-this",
);
assert_eq!(repository.check_auth(&request), false);
}
@@ -87,12 +80,8 @@ mod tests {
#[test]
fn it_ignores_comments() {
let repository = PasswordDirectory::new(TEST_PATH);
let request = Request {
userid: "bp".to_string(),
password: "commented-out".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
let request = Request::new("bp-xxx", "commented-out", "ignore-this", "also-ignore-this");
assert_eq!(repository.check_auth(&request), false);
}
+4 -8
View File
@@ -5,10 +5,9 @@ use std::io::Cursor;
fn it_reads_strings() {
let buffer = [0x0, 0x3, b'F', b'o', b'o'];
assert_eq!(
read_string(&mut buffer.as_ref()).unwrap(),
"Foo".to_string()
);
let result = read_string(&mut buffer.as_ref()).unwrap();
assert_eq!(result, "Foo");
}
#[test]
@@ -17,8 +16,5 @@ fn it_writes_strings() {
write_string(&mut buffer, "Foo").unwrap();
// Should have written exactly 5 bytes
assert_eq!(buffer.get_ref().len(), 5usize);
assert_eq!(&buffer.get_ref()[0..5], [0x0, 0x3, b'F', b'o', b'o']);
assert_eq!(buffer.into_inner(), [0x0, 0x3, b'F', b'o', b'o']);
}