garrison 0.9.0

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

//! 权限校验层测试 mock 实现(`#[cfg(test)]` 编译):提供 `MockInterface`(`HashMap` 模拟
//! `GarrisonInterface`),供 `core::permission::tests` 权限 / 角色校验测试复用。

use crate::error::GarrisonResult;
use crate::stp::GarrisonInterface;
use async_trait::async_trait;
use std::collections::HashMap;

/// 测试用 mock GarrisonInterface。
pub struct MockInterface {
    permissions: HashMap<String, Vec<String>>,
    roles: HashMap<String, Vec<String>>,
}

impl MockInterface {
    pub fn new() -> Self {
        Self {
            permissions: HashMap::new(),
            roles: HashMap::new(),
        }
    }

    /// 链式注入指定 login_id 的权限列表。
    pub fn with_perms(mut self, login_id: &str, perms: Vec<&str>) -> Self {
        self.permissions.insert(
            login_id.to_string(),
            perms.iter().map(|s| s.to_string()).collect(),
        );
        self
    }

    /// 链式注入指定 login_id 的角色列表。
    pub fn with_roles(mut self, login_id: &str, roles: Vec<&str>) -> Self {
        self.roles.insert(
            login_id.to_string(),
            roles.iter().map(|s| s.to_string()).collect(),
        );
        self
    }
}

#[async_trait]
impl GarrisonInterface for MockInterface {
    async fn get_permission_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
        Ok(self.permissions.get(login_id).cloned().unwrap_or_default())
    }

    async fn get_role_list(&self, login_id: &str) -> GarrisonResult<Vec<String>> {
        Ok(self.roles.get(login_id).cloned().unwrap_or_default())
    }
}