mod common;
mod modules;
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::common::*;
#[allow(unused_imports)]
use super::modules::*;
use limiteron::{BanStorage, QuotaStorage};
#[tokio::test]
async fn test_mock_quota_storage() {
let storage = MockQuotaStorage::new();
let result = storage
.consume(
"user1",
"resource1",
100,
1000,
std::time::Duration::from_secs(60),
)
.await
.unwrap();
assert!(result.allowed);
assert_eq!(result.remaining, 900);
let result = storage
.consume(
"user1",
"resource1",
500,
1000,
std::time::Duration::from_secs(60),
)
.await
.unwrap();
assert!(result.allowed);
assert_eq!(result.remaining, 400);
let result = storage
.consume(
"user1",
"resource1",
500,
1000,
std::time::Duration::from_secs(60),
)
.await
.unwrap();
assert!(!result.allowed);
}
#[tokio::test]
async fn test_mock_ban_storage() {
use limiteron::{BanRecord, BanTarget};
use std::time::Duration;
let storage = MockBanStorage::new();
let ban = BanRecord {
target: BanTarget::Ip("192.168.1.1".to_string()),
ban_times: 1,
duration: Duration::from_secs(60),
banned_at: chrono::Utc::now(),
expires_at: chrono::Utc::now() + chrono::Duration::seconds(60),
is_manual: false,
reason: "Test".to_string(),
};
storage.save(&ban).await.unwrap();
let result = storage
.is_banned(&BanTarget::Ip("192.168.1.1".to_string()))
.await
.unwrap();
assert!(result.is_some());
storage
.remove_ban(&BanTarget::Ip("192.168.1.1".to_string()))
.await
.unwrap();
let result = storage
.is_banned(&BanTarget::Ip("192.168.1.1".to_string()))
.await
.unwrap();
assert!(result.is_none());
}
}