use crate::domain::repositories::geo_restriction_repository::{
GeoRestrictionRepository, GeoRestrictionRepositoryError,
};
use crate::domain::services::geo_location::GeoLocation;
use crate::domain::services::geo_location::GeoLocationService;
use crate::domain::services::team_service::{
GeoRestrictionResult, TeamGeoRestrictions, TeamService,
};
use async_trait::async_trait;
use std::net::IpAddr;
use std::sync::Arc;
fn mock_geo_location(country_code: String) -> GeoLocation {
GeoLocation {
ip: "0.0.0.0".to_string(),
country_code: country_code.clone(),
country_name: country_code.to_string(),
..Default::default()
}
}
#[derive(Debug, Clone)]
struct MockGeoService {
country_code: String,
}
impl MockGeoService {
fn new(country_code: String) -> Self {
Self { country_code }
}
}
#[async_trait]
impl GeoLocationService for MockGeoService {
async fn get_location(&self, _ip: &IpAddr) -> Result<GeoLocation, anyhow::Error> {
Ok(mock_geo_location(self.country_code.clone()))
}
}
#[derive(Debug, Clone)]
struct MockGeoRestrictionRepository;
#[async_trait]
impl GeoRestrictionRepository for MockGeoRestrictionRepository {
async fn get_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
) -> Result<TeamGeoRestrictions, GeoRestrictionRepositoryError> {
Ok(TeamGeoRestrictions::default())
}
async fn update_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
_restrictions: &TeamGeoRestrictions,
) -> Result<(), GeoRestrictionRepositoryError> {
Ok(())
}
async fn log_geo_restriction_action(
&self,
_team_id: crate::domain::id::Id,
_ip_address: &str,
_country_code: &str,
_action: &str,
_reason: &str,
) -> Result<(), GeoRestrictionRepositoryError> {
Ok(())
}
}
#[tokio::test]
async fn test_geo_restrictions_disabled_returns_allowed() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: false,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_invalid_ip_format_returns_denied() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"not-an-ip",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(msg.contains("Invalid IP address format"));
}
_ => panic!("Expected Denied result"),
}
}
#[tokio::test]
async fn test_ip_whitelist_cidr_allows() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["192.168.1.0/24".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"192.168.1.50",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_non_whitelisted_ip_blocks() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["10.0.0.0/8".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"192.168.1.1",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_allowed_countries_allows() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string(), "CN".to_string()]),
ip_whitelist: None,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_blocked_countries_denies() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
blocked_countries: Some(vec!["RU".to_string()]),
ip_whitelist: None,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(_)) => {}
_ => panic!("Expected Denied result"),
}
}
#[tokio::test]
async fn test_blocked_takes_precedence_over_allowed() {
let geo_service = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string()]),
blocked_countries: Some(vec!["US".to_string()]),
ip_whitelist: None,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(
msg.contains("US"),
"expected country-specific denial, got: {}",
msg
);
}
_ => panic!("Expected Denied result when country is in both allowed and blocked lists"),
}
}
#[tokio::test]
async fn test_case_insensitive_country_matching() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["us".to_string()]),
ip_whitelist: None,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_domain_blacklist_allows_safe_domain() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["malicious.com".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("www.google.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_domain_blacklist_blocks_matched_domain() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["malicious.com".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("www.malicious.com", &restrictions);
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(msg.contains("malicious.com"));
}
_ => panic!("Expected Denied result"),
}
}
#[tokio::test]
async fn test_domain_blacklist_disabled_returns_allowed() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(MockGeoService::new("US".to_string()));
let geo_repo = Arc::new(MockGeoRestrictionRepository);
let service = TeamService::new(geo_service, geo_repo);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: false,
domain_blacklist: Some(vec!["malicious.com".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("www.malicious.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[test]
fn test_domain_in_denylist_normalizes_url_forms() {
use super::domain_in_denylist;
let blacklist = vec!["blocked.com".to_string()];
for seed in [
"https://blocked.com/page",
"https://sub.blocked.com/page",
"https://BLOCKED.COM/page",
"https://blocked.com./page",
"https://good.com@blocked.com/page",
"https://blocked.com/?next=https://safe.com",
"https://blocked.com/#https://safe.com",
] {
assert!(
domain_in_denylist(seed, &blacklist).is_some(),
"seed must be denied: {seed}"
);
}
assert!(domain_in_denylist("https://safe.com/page", &blacklist).is_none());
assert!(
domain_in_denylist("https://notblocked.com/page", &blacklist).is_none(),
"substring must not match"
);
let idn = vec!["bücher.de".to_string()];
assert!(
domain_in_denylist("https://bücher.de/page", &idn).is_some(),
"IDN seed must match IDN blacklist entry"
);
}
#[derive(Debug, Clone)]
struct FailingGeoService;
#[async_trait]
impl GeoLocationService for FailingGeoService {
async fn get_location(&self, _ip: &IpAddr) -> Result<GeoLocation, anyhow::Error> {
Err(anyhow::anyhow!("geolocation service unavailable"))
}
}
#[derive(Debug, Clone)]
struct FailingGeoRestrictionRepository;
#[async_trait]
impl GeoRestrictionRepository for FailingGeoRestrictionRepository {
async fn get_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
) -> Result<TeamGeoRestrictions, GeoRestrictionRepositoryError> {
Err(GeoRestrictionRepositoryError::Database(
"db down".to_string(),
))
}
async fn update_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
_restrictions: &TeamGeoRestrictions,
) -> Result<(), GeoRestrictionRepositoryError> {
Err(GeoRestrictionRepositoryError::Database(
"db down".to_string(),
))
}
async fn log_geo_restriction_action(
&self,
_team_id: crate::domain::id::Id,
_ip_address: &str,
_country_code: &str,
_action: &str,
_reason: &str,
) -> Result<(), GeoRestrictionRepositoryError> {
Err(GeoRestrictionRepositoryError::Database(
"db down".to_string(),
))
}
}
#[derive(Debug, Clone)]
struct ConfigurableGeoRestrictionRepository {
restrictions: TeamGeoRestrictions,
}
impl ConfigurableGeoRestrictionRepository {
fn new(restrictions: TeamGeoRestrictions) -> Self {
Self { restrictions }
}
}
#[async_trait]
impl GeoRestrictionRepository for ConfigurableGeoRestrictionRepository {
async fn get_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
) -> Result<TeamGeoRestrictions, GeoRestrictionRepositoryError> {
Ok(self.restrictions.clone())
}
async fn update_team_restrictions(
&self,
_team_id: crate::domain::id::Id,
_restrictions: &TeamGeoRestrictions,
) -> Result<(), GeoRestrictionRepositoryError> {
Ok(())
}
async fn log_geo_restriction_action(
&self,
_team_id: crate::domain::id::Id,
_ip_address: &str,
_country_code: &str,
_action: &str,
_reason: &str,
) -> Result<(), GeoRestrictionRepositoryError> {
Ok(())
}
}
fn make_service(
geo: Arc<dyn GeoLocationService>,
repo: Arc<dyn GeoRestrictionRepository>,
) -> TeamService {
TeamService::new(geo, repo)
}
#[tokio::test]
async fn test_get_team_geo_restrictions_success_returns_config() {
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string()]),
..Default::default()
};
let geo_repo = Arc::new(ConfigurableGeoRestrictionRepository::new(
restrictions.clone(),
));
let service = make_service(Arc::new(MockGeoService::new("US".to_string())), geo_repo);
let result = service
.get_team_geo_restrictions(crate::domain::id::Id::generate())
.await;
assert!(result.enable_geo_restrictions);
assert_eq!(result.allowed_countries, Some(vec!["US".to_string()]));
}
#[tokio::test]
async fn test_get_team_geo_restrictions_failure_falls_back_to_deny_all() {
let geo_repo: Arc<dyn GeoRestrictionRepository> = Arc::new(FailingGeoRestrictionRepository);
let service = make_service(Arc::new(MockGeoService::new("US".to_string())), geo_repo);
let result = service
.get_team_geo_restrictions(crate::domain::id::Id::generate())
.await;
assert!(result.enable_geo_restrictions);
assert_eq!(result.allowed_countries, Some(Vec::new()));
assert!(result.blocked_countries.is_none());
assert!(result.ip_whitelist.is_none());
assert!(result.domain_blacklist.is_none());
let verdict = service
.validate_geographic_restriction(crate::domain::id::Id::generate(), "8.8.8.8", &result)
.await;
assert!(matches!(verdict, Ok(GeoRestrictionResult::Denied(_))));
}
#[tokio::test]
async fn test_validate_geographic_restriction_country_not_in_allowed_denies() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string(), "CA".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(
msg.contains("RU"),
"denial message should mention country code, got: {}",
msg
);
assert!(msg.contains("not allowed"));
}
_ => panic!("Expected Denied result for country not in allowed list"),
}
}
#[tokio::test]
async fn test_validate_geographic_restriction_geo_service_error_propagates() {
let geo_service: Arc<dyn GeoLocationService> = Arc::new(FailingGeoService);
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(result.is_err(), "geo service error should propagate");
let err = result.unwrap_err().to_string();
assert!(
err.contains("Unable to determine geographic location"),
"error should mention geolocation failure, got: {}",
err
);
}
#[tokio::test]
async fn test_ip_whitelist_no_match_then_country_allows() {
let geo_service = Arc::new(MockGeoService::new("US".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["10.0.0.0/8".to_string()]),
allowed_countries: Some(vec!["US".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"192.168.1.1",
&restrictions,
)
.await;
assert!(
matches!(result, Ok(GeoRestrictionResult::Allowed)),
"should be allowed by country when not in whitelist"
);
}
#[tokio::test]
async fn test_ip_whitelist_no_match_then_country_blocks() {
let geo_service = Arc::new(MockGeoService::new("CN".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["10.0.0.0/8".to_string()]),
blocked_countries: Some(vec!["CN".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"192.168.1.1",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(msg.contains("CN"));
}
_ => panic!("Expected Denied when country is blocked and IP not in whitelist"),
}
}
#[tokio::test]
async fn test_both_allowed_and_blocked_countries_allowed_wins_for_unblocked() {
let geo_service = Arc::new(MockGeoService::new("US".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string(), "CN".to_string()]),
blocked_countries: Some(vec!["RU".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_both_allowed_and_blocked_blocked_wins_for_blocked_country() {
let geo_service = Arc::new(MockGeoService::new("CN".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string(), "CN".to_string()]),
blocked_countries: Some(vec!["CN".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(msg.contains("CN"));
}
_ => panic!("Expected Denied when country is in blocked list even if also in allowed"),
}
}
#[tokio::test]
async fn test_ipv6_address_allowed_by_country() {
let geo_service = Arc::new(MockGeoService::new("US".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
allowed_countries: Some(vec!["US".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"2001:db8::1",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_ipv6_address_allowed_by_whitelist() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["2001:db8::/32".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"2001:db8::1",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_ipv6_address_not_in_whitelist_blocked_by_country() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["2001:db9::/32".to_string()]), blocked_countries: Some(vec!["RU".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"2001:db8::1",
&restrictions,
)
.await;
match result {
Ok(GeoRestrictionResult::Denied(msg)) => {
assert!(msg.contains("RU"));
}
_ => panic!("Expected Denied for IPv6 not in whitelist and country blocked"),
}
}
#[tokio::test]
async fn test_validate_domain_blacklist_no_blacklist_returns_allowed() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: None,
..Default::default()
};
let result = service.validate_domain_blacklist("www.anything.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_validate_domain_blacklist_multiple_entries() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec![
"malicious.com".to_string(),
"spam.org".to_string(),
"bad-actor.net".to_string(),
]),
..Default::default()
};
assert!(matches!(
service.validate_domain_blacklist("www.malicious.com", &restrictions),
Ok(GeoRestrictionResult::Denied(_))
));
assert!(matches!(
service.validate_domain_blacklist("sub.spam.org", &restrictions),
Ok(GeoRestrictionResult::Denied(_))
));
assert!(matches!(
service.validate_domain_blacklist("bad-actor.net/path", &restrictions),
Ok(GeoRestrictionResult::Denied(_))
));
assert!(matches!(
service.validate_domain_blacklist("www.safe.com", &restrictions),
Ok(GeoRestrictionResult::Allowed)
));
}
#[tokio::test]
async fn test_validate_domain_blacklist_empty_blacklist_returns_allowed() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec![]),
..Default::default()
};
let result = service.validate_domain_blacklist("www.anything.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_validate_domain_blacklist_no_substring_false_positive() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["evil".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("www.evil-domain.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_validate_domain_blacklist_security_bypass_regression() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["example.com".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("evil-example.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
let result = service.validate_domain_blacklist("example.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Denied(_))));
let result = service.validate_domain_blacklist("sub.example.com", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Denied(_))));
}
#[tokio::test]
async fn test_validate_domain_blacklist_url_input_extracts_host() {
let service = make_service(
Arc::new(MockGeoService::new("US".to_string())),
Arc::new(MockGeoRestrictionRepository),
);
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["malicious.com".to_string()]),
..Default::default()
};
let result = service.validate_domain_blacklist("https://www.malicious.com/path", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Denied(_))));
let result = service.validate_domain_blacklist("malicious.com:8080", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Denied(_))));
let result = service.validate_domain_blacklist("malicious.com/admin", &restrictions);
assert!(matches!(result, Ok(GeoRestrictionResult::Denied(_))));
}
#[tokio::test]
async fn test_team_geo_restrictictions_default_all_none() {
let default = TeamGeoRestrictions::default();
assert!(!default.enable_geo_restrictions);
assert!(default.allowed_countries.is_none());
assert!(default.blocked_countries.is_none());
assert!(default.ip_whitelist.is_none());
assert!(default.domain_blacklist.is_none());
}
#[tokio::test]
async fn test_invalid_cidr_in_whitelist_falls_through_to_country_check() {
let geo_service = Arc::new(MockGeoService::new("US".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
ip_whitelist: Some(vec!["invalid-cidr".to_string()]),
allowed_countries: Some(vec!["US".to_string()]),
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[tokio::test]
async fn test_no_restrictions_configured_allows_all() {
let geo_service = Arc::new(MockGeoService::new("RU".to_string()));
let service = make_service(geo_service, Arc::new(MockGeoRestrictionRepository));
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
..Default::default()
};
let result = service
.validate_geographic_restriction(
crate::domain::id::Id::generate(),
"8.8.8.8",
&restrictions,
)
.await;
assert!(matches!(result, Ok(GeoRestrictionResult::Allowed)));
}
#[test]
fn test_effective_domain_blacklist_validates_caps_even_when_disabled() {
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: false,
domain_blacklist: Some(vec!["x".repeat(300)]),
..Default::default()
};
assert!(
restrictions.effective_domain_blacklist().is_err(),
"oversized entry must fail even when restrictions are disabled"
);
let too_many = TeamGeoRestrictions {
enable_geo_restrictions: false,
domain_blacklist: Some(
(0..=crate::domain::services::team_service::MAX_DOMAIN_BLACKLIST_ENTRIES)
.map(|i| format!("d{i}.example"))
.collect(),
),
..Default::default()
};
assert!(
too_many.effective_domain_blacklist().is_err(),
"entry-count cap must fail even when restrictions are disabled"
);
}
#[test]
fn test_effective_domain_blacklist_disabled_valid_list_returns_empty() {
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: false,
domain_blacklist: Some(vec!["blocked.com".to_string()]),
..Default::default()
};
assert_eq!(
restrictions.effective_domain_blacklist().unwrap(),
Vec::<String>::new(),
"disabled restrictions must not enforce historical entries"
);
}
#[test]
fn test_effective_domain_blacklist_rejects_unnormalizable_entry() {
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["::1".to_string()]),
..Default::default()
};
assert!(
restrictions.effective_domain_blacklist().is_err(),
"unbracketed IPv6 cannot be normalized and must be rejected"
);
let bracketed = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["[::1]".to_string()]),
..Default::default()
};
assert!(
bracketed.effective_domain_blacklist().is_ok(),
"bracketed IPv6 is a valid host form"
);
}
#[test]
fn test_effective_domain_blacklist_rejects_wildcard_entry() {
let restrictions = TeamGeoRestrictions {
enable_geo_restrictions: true,
domain_blacklist: Some(vec!["*.evil.com".to_string()]),
..Default::default()
};
assert!(
restrictions.effective_domain_blacklist().is_err(),
"wildcard entries never match a host and must be rejected"
);
}