use super::*;
struct AlwaysPassRule;
impl PasswordPolicyRule for AlwaysPassRule {
fn name(&self) -> &'static str {
"always_pass"
}
fn validate(&self, _ctx: &PolicyContext, _password: &str) -> Result<(), PolicyError> {
Ok(())
}
}
struct AlwaysFailRule {
msg: &'static str,
}
impl PasswordPolicyRule for AlwaysFailRule {
fn name(&self) -> &'static str {
"always_fail"
}
fn validate(&self, _ctx: &PolicyContext, _password: &str) -> Result<(), PolicyError> {
Err(PolicyError::new("always_fail", self.msg))
}
}
#[test]
fn password_policy_rule_is_object_safe() {
fn _assert_object_safe(_rule: Box<dyn PasswordPolicyRule>) {}
let rule: Box<dyn PasswordPolicyRule> = Box::new(AlwaysPassRule);
_assert_object_safe(rule);
}
#[test]
fn password_policy_rule_vec_of_boxed_dyn() {
let rules: Vec<Box<dyn PasswordPolicyRule>> = vec![
Box::new(AlwaysPassRule),
Box::new(AlwaysFailRule { msg: "fail1" }),
];
assert_eq!(rules.len(), 2);
assert_eq!(rules[0].name(), "always_pass");
assert_eq!(rules[1].name(), "always_fail");
}
#[test]
fn policy_context_constructs_with_6_fields() {
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: Some("tenant-001".to_string()),
username: Some("alice".to_string()),
email: Some("alice@example.com".to_string()),
password_history: vec!["hash1".to_string(), "hash2".to_string()],
password_created_at: Some(1700000000),
};
assert_eq!(ctx.user_id, "alice");
assert_eq!(ctx.tenant_id, Some("tenant-001".to_string()));
assert_eq!(ctx.username, Some("alice".to_string()));
assert_eq!(ctx.email, Some("alice@example.com".to_string()));
assert_eq!(ctx.password_history.len(), 2);
assert_eq!(ctx.password_created_at, Some(1700000000));
}
#[test]
fn policy_context_optional_fields_none() {
let ctx = PolicyContext {
user_id: "bob".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
assert!(ctx.tenant_id.is_none());
assert!(ctx.username.is_none());
assert!(ctx.email.is_none());
assert!(ctx.password_history.is_empty());
assert!(ctx.password_created_at.is_none());
}
#[test]
fn engine_empty_rules_returns_ok() {
let engine = PasswordPolicyEngine::new(Vec::new(), ErrorMode::FirstError);
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
assert!(engine.validate(&ctx, "any").is_ok());
}
#[test]
fn engine_all_pass_returns_ok() {
let engine = PasswordPolicyEngine::new(
vec![Box::new(AlwaysPassRule), Box::new(AlwaysPassRule)],
ErrorMode::AllErrors,
);
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
assert!(engine.validate(&ctx, "password").is_ok());
}
#[test]
fn engine_first_error_mode_short_circuits() {
let engine = PasswordPolicyEngine::new(
vec![
Box::new(AlwaysFailRule { msg: "fail1" }),
Box::new(AlwaysFailRule { msg: "fail2" }),
],
ErrorMode::FirstError,
);
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
let result = engine.validate(&ctx, "password");
assert!(result.is_err());
let errors = result.unwrap_err();
assert_eq!(errors.len(), 1, "FirstError 模式应只返回 1 个错误");
assert_eq!(errors[0].message, "fail1", "应短路在第一条失败规则");
}
#[test]
fn engine_all_errors_mode_collects_all() {
let engine = PasswordPolicyEngine::new(
vec![
Box::new(AlwaysPassRule),
Box::new(AlwaysFailRule { msg: "fail1" }),
Box::new(AlwaysFailRule { msg: "fail2" }),
],
ErrorMode::AllErrors,
);
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
let result = engine.validate(&ctx, "password");
assert!(result.is_err());
let errors = result.unwrap_err();
assert_eq!(errors.len(), 2, "AllErrors 模式应收集全部 2 个失败");
assert_eq!(errors[0].message, "fail1");
assert_eq!(errors[1].message, "fail2");
}
#[test]
fn engine_first_error_mode_skips_passing_rules() {
let engine = PasswordPolicyEngine::new(
vec![
Box::new(AlwaysPassRule),
Box::new(AlwaysFailRule { msg: "fail1" }),
],
ErrorMode::FirstError,
);
let ctx = PolicyContext {
user_id: "alice".to_string(),
tenant_id: None,
username: None,
email: None,
password_history: Vec::new(),
password_created_at: None,
};
let result = engine.validate(&ctx, "password");
assert!(result.is_err());
let errors = result.unwrap_err();
assert_eq!(errors.len(), 1);
assert_eq!(errors[0].message, "fail1");
}