Add tests to the service module

This commit is contained in:
Rodolphe Bréard 2023-07-16 11:29:07 +02:00
parent 2841babdcc
commit 01220e6001

View file

@ -59,3 +59,73 @@ fn allow_email(input: &Input, addr_lst: &HashSet<KeyedAddress>) -> bool {
}
true
}
#[cfg(test)]
mod tests {
use super::allow_email;
use crate::input::parse_input;
use crate::service::KeyedAddress;
use std::collections::HashSet;
use std::str::FromStr;
fn run_test_with_addr(address: &str) -> bool {
// Preparing the input
let input_str = format!("filter|0.5|1576146008.006099|smtp-in|rcpt-to|7641df9771b4ed00|1ef1c203cc576e5d|{address}");
let input = parse_input(&input_str);
assert!(input.is_ok());
let input = input.unwrap();
// Preparing the test keyed addresses
let kaddr_lst = [
"a@example.org:11voiefK5PgCX5F1TTcuoQ==",
"b:11voiefK5PgCX5F1TTcuoQ==",
];
let mut addr_set = HashSet::with_capacity(kaddr_lst.len());
for kaddr_str in kaddr_lst {
let keyed_addr = KeyedAddress::from_str(kaddr_str);
assert!(keyed_addr.is_ok());
let keyed_addr = keyed_addr.unwrap();
addr_set.insert(keyed_addr);
}
// Run the test
allow_email(&input, &addr_set)
}
#[test]
#[ignore]
fn test_valid_code_domain() {
assert!(run_test_with_addr("a+test+TODO@example.org"));
}
#[test]
#[ignore]
fn test_valid_code_no_domain() {
assert!(run_test_with_addr("b+test+TODO@example.org"));
}
#[test]
fn test_invalid_code_domain() {
assert!(!run_test_with_addr("a+test+orsxg5a@example.org"));
}
#[test]
fn test_invalid_code_no_domain() {
assert!(!run_test_with_addr("b+test+orsxg5a@example.org"));
}
#[test]
fn test_no_code() {
assert!(!run_test_with_addr("a+test@example.org"));
}
#[test]
fn test_no_sub_addr() {
assert!(!run_test_with_addr("b@example.org"));
}
#[test]
fn test_different_domain() {
assert!(run_test_with_addr("a@example.com"));
}
}