use crate::core::assertion::conditions::{
AudienceRestriction, AudienceRestrictionRef, Conditions, ConditionsRef,
};
pub fn evaluate_audience(conditions: &Conditions, sp_entity_id: &str) -> bool {
evaluate_audience_restrictions(&conditions.audience_restrictions, sp_entity_id)
}
pub fn evaluate_audience_ref(conditions: &ConditionsRef<'_>, sp_entity_id: &str) -> bool {
evaluate_audience_restrictions_ref(&conditions.audience_restrictions, sp_entity_id)
}
pub fn evaluate_audience_restrictions(
restrictions: &[AudienceRestriction],
sp_entity_id: &str,
) -> bool {
if restrictions.is_empty() {
return true;
}
restrictions.iter().all(|r| r.matches(sp_entity_id))
}
pub fn evaluate_audience_restrictions_ref(
restrictions: &[AudienceRestrictionRef<'_>],
sp_entity_id: &str,
) -> bool {
if restrictions.is_empty() {
return true;
}
restrictions.iter().all(|r| r.matches(sp_entity_id))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_empty_restrictions_passes() {
assert!(evaluate_audience_restrictions(
&[],
"https://sp.example.com"
));
}
#[test]
fn test_single_restriction_match() {
let restrictions = vec![AudienceRestriction {
audiences: vec!["https://sp.example.com".to_string()],
}];
assert!(evaluate_audience_restrictions(
&restrictions,
"https://sp.example.com"
));
}
#[test]
fn test_single_restriction_no_match() {
let restrictions = vec![AudienceRestriction {
audiences: vec!["https://other.example.com".to_string()],
}];
assert!(!evaluate_audience_restrictions(
&restrictions,
"https://sp.example.com"
));
}
#[test]
fn test_or_within_restriction() {
let restrictions = vec![AudienceRestriction {
audiences: vec![
"https://sp1.example.com".to_string(),
"https://sp2.example.com".to_string(),
],
}];
assert!(evaluate_audience_restrictions(
&restrictions,
"https://sp1.example.com"
));
assert!(evaluate_audience_restrictions(
&restrictions,
"https://sp2.example.com"
));
assert!(!evaluate_audience_restrictions(
&restrictions,
"https://sp3.example.com"
));
}
#[test]
fn test_and_across_restrictions() {
let restrictions = vec![
AudienceRestriction {
audiences: vec![
"https://sp.example.com".to_string(),
"https://partner.example.com".to_string(),
],
},
AudienceRestriction {
audiences: vec!["https://sp.example.com".to_string()],
},
];
assert!(evaluate_audience_restrictions(
&restrictions,
"https://sp.example.com"
));
assert!(!evaluate_audience_restrictions(
&restrictions,
"https://partner.example.com"
));
}
#[test]
fn test_evaluate_audience_from_conditions() {
let conditions = Conditions {
not_before: None,
not_on_or_after: None,
audience_restrictions: vec![AudienceRestriction {
audiences: vec!["https://sp.example.com".to_string()],
}],
one_time_use: false,
proxy_restriction: None,
};
assert!(evaluate_audience(&conditions, "https://sp.example.com"));
assert!(!evaluate_audience(&conditions, "https://evil.example.com"));
}
#[test]
fn test_evaluate_audience_from_conditions_no_restrictions() {
let conditions = Conditions {
not_before: None,
not_on_or_after: None,
audience_restrictions: vec![],
one_time_use: false,
proxy_restriction: None,
};
assert!(evaluate_audience(&conditions, "https://sp.example.com"));
}
#[test]
fn test_ref_evaluation() {
let restrictions = vec![AudienceRestrictionRef {
audiences: vec!["https://sp.example.com"],
}];
assert!(evaluate_audience_restrictions_ref(
&restrictions,
"https://sp.example.com"
));
assert!(!evaluate_audience_restrictions_ref(
&restrictions,
"https://other.example.com"
));
}
}