use rocketmq_error::AuthError;
use crate::authentication::strategy::AuthenticationStrategy;
use crate::authorization::context::authentication_context::AuthenticationContext;
#[derive(Debug, Clone, Default)]
pub struct AllowAllAuthenticationStrategy;
impl AllowAllAuthenticationStrategy {
#[inline]
pub fn new() -> Self {
Self
}
}
impl AuthenticationStrategy for AllowAllAuthenticationStrategy {
fn authenticate(&self, _context: &dyn AuthenticationContext) -> Result<(), AuthError> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::authentication::context::default_authentication_context::DefaultAuthenticationContext;
use cheetah_string::CheetahString;
use std::sync::Arc;
#[test]
fn test_new_creates_strategy() {
let strategy = AllowAllAuthenticationStrategy::new();
let context = DefaultAuthenticationContext::new();
assert!(strategy.authenticate(&context).is_ok());
}
#[test]
fn test_default_creates_strategy() {
let strategy = AllowAllAuthenticationStrategy;
let context = DefaultAuthenticationContext::new();
assert!(strategy.authenticate(&context).is_ok());
}
#[test]
fn test_allows_empty_context() {
let strategy = AllowAllAuthenticationStrategy::new();
let context = DefaultAuthenticationContext::new();
let result = strategy.authenticate(&context);
assert!(result.is_ok());
}
#[test]
fn test_allows_context_with_credentials() {
let strategy = AllowAllAuthenticationStrategy::new();
let mut context = DefaultAuthenticationContext::new();
context.set_username(CheetahString::from("test_user"));
context.set_signature(CheetahString::from("invalid_signature"));
let result = strategy.authenticate(&context);
assert!(result.is_ok());
}
#[test]
fn test_allows_context_with_invalid_data() {
let strategy = AllowAllAuthenticationStrategy::new();
let mut context = DefaultAuthenticationContext::new();
context.set_username(CheetahString::from(""));
context.set_content(vec![]);
let result = strategy.authenticate(&context);
assert!(result.is_ok());
}
#[test]
fn test_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<AllowAllAuthenticationStrategy>();
}
#[test]
fn test_can_be_used_as_trait_object() {
let strategy: Arc<dyn AuthenticationStrategy> = Arc::new(AllowAllAuthenticationStrategy::new());
let context = DefaultAuthenticationContext::new();
let result = strategy.authenticate(&context);
assert!(result.is_ok());
}
#[test]
fn test_clone_works() {
let strategy1 = AllowAllAuthenticationStrategy::new();
let strategy2 = strategy1.clone();
let context = DefaultAuthenticationContext::new();
assert!(strategy1.authenticate(&context).is_ok());
assert!(strategy2.authenticate(&context).is_ok());
}
#[test]
fn test_debug_impl() {
let strategy = AllowAllAuthenticationStrategy::new();
let debug_string = format!("{:?}", strategy);
assert_eq!(debug_string, "AllowAllAuthenticationStrategy");
}
}