garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! `PasswordPolicyEngine` 单元测试。

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(())
    }
}

/// 始终失败的规则(message 可定制,用于区分多个失败规则)。
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))
    }
}

/// `PasswordPolicyRule` trait 可作 `Box<dyn PasswordPolicyRule>` 使用(对象安全)。
#[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);
}

/// `Vec<Box<dyn PasswordPolicyRule>>` 可构造(引擎 rules 字段类型验证)。
#[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");
}

/// `PolicyContext` 6 字段构造。
#[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));
}

/// `PolicyContext` 可选字段为 `None` 时构造正常。
#[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());
}

/// 空规则集 + 任意密码 → `Ok(())`。
#[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());
}

/// 全部规则通过 → `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());
}

/// `FirstError` 模式 — 首条失败即返回单元素 Vec 并短路。
#[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", "应短路在第一条失败规则");
}

/// `AllErrors` 模式 — 收集全部失败规则错误。
#[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");
}

/// `FirstError` 模式 — 首条通过、第二条失败时返回第二条错误。
#[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");
}