rocketmq_auth/authentication/strategy/
allow_all.rs1use crate::authentication::strategy::AuthenticationFuture;
26use crate::authentication::strategy::AuthenticationStrategy;
27use crate::authorization::context::authentication_context::AuthenticationContext;
28
29#[derive(Debug, Clone, Default)]
59pub struct AllowAllAuthenticationStrategy;
60
61impl AllowAllAuthenticationStrategy {
62 #[inline]
72 pub fn new() -> Self {
73 Self
74 }
75}
76
77impl AuthenticationStrategy for AllowAllAuthenticationStrategy {
78 fn authenticate<'a>(&'a self, _context: &'a dyn AuthenticationContext) -> AuthenticationFuture<'a> {
90 Box::pin(async { Ok(()) })
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::authentication::context::default_authentication_context::DefaultAuthenticationContext;
98 use cheetah_string::CheetahString;
99 use std::sync::Arc;
100
101 #[tokio::test]
102 async fn test_new_creates_strategy() {
103 let strategy = AllowAllAuthenticationStrategy::new();
104 let context = DefaultAuthenticationContext::new();
105 assert!(strategy.authenticate(&context).await.is_ok());
106 }
107
108 #[tokio::test]
109 async fn test_default_creates_strategy() {
110 let strategy = AllowAllAuthenticationStrategy;
111 let context = DefaultAuthenticationContext::new();
112 assert!(strategy.authenticate(&context).await.is_ok());
113 }
114
115 #[tokio::test]
116 async fn test_allows_empty_context() {
117 let strategy = AllowAllAuthenticationStrategy::new();
118 let context = DefaultAuthenticationContext::new();
119
120 let result = strategy.authenticate(&context).await;
121 assert!(result.is_ok());
122 }
123
124 #[tokio::test]
125 async fn test_allows_context_with_credentials() {
126 let strategy = AllowAllAuthenticationStrategy::new();
127 let mut context = DefaultAuthenticationContext::new();
128 context.set_username(CheetahString::from("test_user"));
129 context.set_signature(CheetahString::from("invalid_signature"));
130
131 let result = strategy.authenticate(&context).await;
132 assert!(result.is_ok());
133 }
134
135 #[tokio::test]
136 async fn test_allows_context_with_invalid_data() {
137 let strategy = AllowAllAuthenticationStrategy::new();
138 let mut context = DefaultAuthenticationContext::new();
139 context.set_username(CheetahString::from(""));
140 context.set_content(vec![]);
141
142 let result = strategy.authenticate(&context).await;
143 assert!(result.is_ok());
144 }
145
146 #[test]
147 fn test_is_send_sync() {
148 fn assert_send_sync<T: Send + Sync>() {}
149 assert_send_sync::<AllowAllAuthenticationStrategy>();
150 }
151
152 #[tokio::test]
153 async fn test_can_be_used_as_trait_object() {
154 let strategy: Arc<dyn AuthenticationStrategy> = Arc::new(AllowAllAuthenticationStrategy::new());
155 let context = DefaultAuthenticationContext::new();
156
157 let result = strategy.authenticate(&context).await;
158 assert!(result.is_ok());
159 }
160
161 #[tokio::test]
162 async fn test_clone_works() {
163 let strategy1 = AllowAllAuthenticationStrategy::new();
164 let strategy2 = strategy1.clone();
165
166 let context = DefaultAuthenticationContext::new();
167 assert!(strategy1.authenticate(&context).await.is_ok());
168 assert!(strategy2.authenticate(&context).await.is_ok());
169 }
170
171 #[test]
172 fn test_debug_impl() {
173 let strategy = AllowAllAuthenticationStrategy::new();
174 let debug_string = format!("{:?}", strategy);
175 assert_eq!(debug_string, "AllowAllAuthenticationStrategy");
176 }
177}