Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CEL Interperter for Conditions #354

Closed
wants to merge 12 commits into from
28 changes: 18 additions & 10 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,14 @@ mod conditions {
mod tests {
use super::*;

macro_rules! assert_false {
($cond:expr $(,)?) => {
paste::item! {
assert!(!$cond)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below, so I'd change this to:

-               assert!(!$cond)
+               assert!(!$cond, "assertion failed: assert_false!({}) was true!", stringify!($cond))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me. updated.

}
};
}

#[test]
fn limit_can_have_an_optional_name() {
let mut limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]);
Expand Down Expand Up @@ -915,7 +923,7 @@ mod tests {
values.insert("x".into(), "1".into());
values.insert("y".into(), "1".into());

assert!(!limit.applies(&values))
assert_false!(limit.applies(&values))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 ...
But so you know this will report in a slightly confusing way:

assertion failed: !limit.applies(&values)

with the change above it'd be, I think clearer:

assertion failed: assert_false!(limit.applies(&values)) was true!

where now assert_false!(limit.applies(&values)) matches code the user wrote

}

#[test]
Expand All @@ -927,9 +935,9 @@ mod tests {
values.insert("x".into(), "1".into());
values.insert("y".into(), "1".into());

assert!(!limit.applies(&values));
assert_false!(limit.applies(&values));
assert!(check_deprecated_syntax_usages_and_reset());
assert!(!check_deprecated_syntax_usages_and_reset());
assert_false!(check_deprecated_syntax_usages_and_reset());

let limit = Limit::new("test_namespace", 10, 60, vec!["x == foobar"], vec!["y"]);

Expand All @@ -939,7 +947,7 @@ mod tests {

assert!(limit.applies(&values));
assert!(check_deprecated_syntax_usages_and_reset());
assert!(!check_deprecated_syntax_usages_and_reset());
assert_false!(check_deprecated_syntax_usages_and_reset());
}

#[test]
Expand All @@ -951,7 +959,7 @@ mod tests {
values.insert("a".into(), "1".into());
values.insert("y".into(), "1".into());

assert!(!limit.applies(&values))
assert_false!(limit.applies(&values))
}

#[test]
Expand All @@ -962,7 +970,7 @@ mod tests {
let mut values: HashMap<String, String> = HashMap::new();
values.insert("x".into(), "5".into());

assert!(!limit.applies(&values))
assert_false!(limit.applies(&values))
}

#[test]
Expand Down Expand Up @@ -998,7 +1006,7 @@ mod tests {
values.insert("y".into(), "2".into());
values.insert("z".into(), "1".into());

assert!(!limit.applies(&values))
assert_false!(limit.applies(&values))
}

#[test]
Expand Down Expand Up @@ -1091,7 +1099,7 @@ mod tests {

// we can't access complex variables via simple names....
let limit = limit_with_condition(vec![r#"cel: req.method == "GET" "#]);
assert!(!limit.applies(&values));
assert_false!(limit.applies(&values));

// But we can access it via the vars map.
let limit = limit_with_condition(vec![r#"cel: vars["req.method"] == "GET" "#]);
Expand All @@ -1117,7 +1125,7 @@ mod tests {

// we can't access simple variables that conflict with built-ins
let limit = limit_with_condition(vec![r#"cel: size == "50" "#]);
assert!(!limit.applies(&values));
assert_false!(limit.applies(&values));

// But we can access it via the vars map.
let limit = limit_with_condition(vec![r#"cel: vars["size"] == "50" "#]);
Expand All @@ -1130,7 +1138,7 @@ mod tests {

// we can't access simple variables that conflict with built-ins (the vars map)
let limit = limit_with_condition(vec![r#"cel: vars == "hello" "#]);
assert!(!limit.applies(&values));
assert_false!(limit.applies(&values));

// But we can access it via the vars map.
let limit = limit_with_condition(vec![r#"cel: vars["vars"] == "hello" "#]);
Expand Down
Loading