Added some tests for Filestore

This commit is contained in:
Nathan Fisher 2023-06-21 02:17:09 -04:00
parent 12b10e382e
commit 8af4a5bc39
3 changed files with 87 additions and 2 deletions

View file

@ -2,10 +2,11 @@ use {
super::*,
crate::{message::Parser as MessageParser, prelude::ParseMailboxError},
std::{
ffi::OsString,
fs::{self, File},
io::{self, BufWriter, Write},
os::unix::fs::DirBuilderExt,
path::PathBuf,
path::{Path, PathBuf},
},
};
@ -25,6 +26,36 @@ pub struct Filesystem {
pub path: PathBuf,
}
impl From<String> for Filesystem {
fn from(value: String) -> Self {
Self { path: value.into() }
}
}
impl From<&str> for Filesystem {
fn from(value: &str) -> Self {
Self { path: value.into() }
}
}
impl From<&Path> for Filesystem {
fn from(value: &Path) -> Self {
Self { path: value.into() }
}
}
impl From<PathBuf> for Filesystem {
fn from(value: PathBuf) -> Self {
Self { path: value }
}
}
impl From<OsString> for Filesystem {
fn from(value: OsString) -> Self {
Self { path: value.into() }
}
}
impl MailStore for Filesystem {
type Error = io::Error;
@ -98,7 +129,7 @@ impl MailStore for Filesystem {
};
dir.filter_map(Result::ok).for_each(|e| {
if let Ok(contents) = fs::read_to_string(e.path()) {
if let Some(p) = e.path().to_str() {
if let Some(Some(p)) = e.path().file_stem().map(|s| s.to_str()) {
let parser = MessageParser::new(p);
if let Ok(message) = parser.parse(&contents) {
folder.messages.insert(message.id.clone(), message);
@ -253,3 +284,50 @@ impl MultiDomain for Filesystem {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn store() -> Filesystem {
"test/mailstore".into()
}
#[test]
fn users() {
let users = store().users();
assert!(users.iter().any(|u| u.host.to_string() == "mail.gmi.org" && u.username == "jane" ));
assert!(users.iter().any(|u| u.host.to_string() == "mail.gmi.org" && u.username == "dick" ));
assert!(users.iter().any(|u| u.host.to_string() == "misfin.example.org" && u.username == "jane" ));
assert!(users.iter().any(|u| u.host.to_string() == "misfin.example.org" && u.username == "dick" ));
assert_eq!(users.len(), 4);
}
#[test]
fn serves_domain() {
let store = store();
assert!(store.serves_domain("mail.gmi.org"));
assert!(store.serves_domain("misfin.example.org"));
assert!(!store.serves_domain("iron.maiden.org"));
}
#[test]
fn has_user() {
let store = store();
assert!(store.has_mailuser("jane@mail.gmi.org"));
assert!(store.has_mailuser("dick@mail.gmi.org"));
assert!(store.has_mailuser("jane@misfin.example.org"));
assert!(store.has_mailuser("dick@misfin.example.org"));
assert!(!store.has_mailuser("fred@mail.gmi.org"));
}
#[test]
fn get_folder() {
let store = store();
let folder = store.get_folder("jane@mail.gmi.org", "Inbox").unwrap();
let msg = folder.messages.get("867017");
assert!(msg.is_some());
let msg = folder.messages.get("488571");
assert!(msg.is_some());
}
}

View file

@ -0,0 +1,4 @@
< joe@bonamassa.sucks Joe Bonamassa
# Does anyone like my music?
Or should I just stop now?

View file

@ -0,0 +1,3 @@
< april@we.wear.shortshorts.biz
This message is spam that probably never would have actually been delivered