Rename handler.rs to repository.rs for clarity, pass the directory name as a new() parameter rather than have the repository aware of the OPTIONS static

This commit is contained in:
Barry Pederson
2024-11-03 10:28:14 -08:00
parent 52f502f7a6
commit 1a66c8ede5
6 changed files with 35 additions and 29 deletions
-94
View File
@@ -1,94 +0,0 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use crate::options::OPTIONS;
use common::Request;
#[derive(Clone)]
pub struct FileHandler;
impl FileHandler {
pub fn new() -> FileHandler {
FileHandler {}
}
pub fn check_auth(&self, request: &Request) -> bool {
// Don't allow empty userids or passwords
//
if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty()
{
return false;
}
let mut file_name = OPTIONS.password_dir.clone();
file_name.push(&request.userid);
if let Ok(file) = File::open(file_name) {
for line in BufReader::new(file).lines().map_while(Result::ok) {
if !line.starts_with('#') && line.trim() == request.password {
return true;
}
}
}
false
}
}
#[cfg(test)]
mod tests {
use crate::handler::FileHandler;
use common::Request;
#[test]
fn it_checks_good_password() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "some-rad-password".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), true);
}
#[test]
fn it_fails_bad_password() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "not-correct".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), false);
}
#[test]
fn it_fails_bad_userid() {
let handler = FileHandler::new();
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(),
};
assert_eq!(handler.check_auth(&request), false);
}
#[test]
fn it_ignores_comments() {
let handler = FileHandler::new();
let request = Request {
userid: "bp".to_string(),
password: "commented-out".to_string(),
service: "ignore-this".to_string(),
realm: "also-ignore-this".to_string(),
};
assert_eq!(handler.check_auth(&request), false);
}
}