Skip to content

Commit 7c4630f

Browse files
VibeSWEclaude
andcommitted
test: cover unprotected holders and tenant scoping in any_role_held_by_protected_user
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ad4b09d commit 7c4630f

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

‎src/rbac/mod.rs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,3 +1433,82 @@ mod lock_order_tests {
14331433
}
14341434
}
14351435
}
1436+
1437+
#[cfg(test)]
1438+
mod protected_holder_tests {
1439+
use parking_lot::RwLock;
1440+
use ulid::Ulid;
1441+
1442+
use super::*;
1443+
use crate::rbac::map::{TEST_SERIAL, USERS};
1444+
use crate::rbac::user::Basic;
1445+
1446+
fn insert_user(tenant: Option<String>, roles: &[&str], protected: bool) {
1447+
mut_users().insert(User {
1448+
ty: UserType::Native(Basic {
1449+
username: format!("user-{}", Ulid::new()),
1450+
password_hash: String::new(),
1451+
email: None,
1452+
}),
1453+
roles: roles.iter().map(|role| role.to_string()).collect(),
1454+
user_groups: HashSet::new(),
1455+
tenant,
1456+
protected,
1457+
});
1458+
}
1459+
1460+
fn names(roles: &[&str]) -> HashSet<String> {
1461+
roles.iter().map(|role| role.to_string()).collect()
1462+
}
1463+
1464+
/// Unique role names keep these tests apart from others sharing the default tenant.
1465+
fn role() -> String {
1466+
format!("role-{}", Ulid::new())
1467+
}
1468+
1469+
#[test]
1470+
fn only_protected_holders_of_a_requested_role_count() {
1471+
let _serial = TEST_SERIAL.blocking_lock();
1472+
USERS.get_or_init(|| RwLock::new(map::Users::default()));
1473+
let tenant = Some(format!("protected-holder-{}", Ulid::new()));
1474+
let (held, unprotected, other) = (role(), role(), role());
1475+
insert_user(tenant.clone(), &[&unprotected], false);
1476+
insert_user(tenant.clone(), &[&held], true);
1477+
1478+
assert!(!Users.any_role_held_by_protected_user(&names(&[&unprotected]), &tenant));
1479+
assert!(!Users.any_role_held_by_protected_user(&names(&[&other]), &tenant));
1480+
assert!(Users.any_role_held_by_protected_user(&names(&[&other, &held]), &tenant));
1481+
}
1482+
1483+
#[test]
1484+
fn protected_holders_in_other_tenants_do_not_count() {
1485+
let _serial = TEST_SERIAL.blocking_lock();
1486+
USERS.get_or_init(|| RwLock::new(map::Users::default()));
1487+
let held = role();
1488+
insert_user(
1489+
Some(format!("protected-holder-{}", Ulid::new())),
1490+
&[&held],
1491+
true,
1492+
);
1493+
1494+
let other_tenant = Some(format!("protected-holder-{}", Ulid::new()));
1495+
assert!(!Users.any_role_held_by_protected_user(&names(&[&held]), &other_tenant));
1496+
assert!(!Users.any_role_held_by_protected_user(&names(&[&held]), &None));
1497+
}
1498+
1499+
#[test]
1500+
fn no_tenant_means_the_default_tenant() {
1501+
let _serial = TEST_SERIAL.blocking_lock();
1502+
USERS.get_or_init(|| RwLock::new(map::Users::default()));
1503+
let held = role();
1504+
insert_user(None, &[&held], true);
1505+
1506+
assert!(Users.any_role_held_by_protected_user(&names(&[&held]), &None));
1507+
assert!(
1508+
Users.any_role_held_by_protected_user(
1509+
&names(&[&held]),
1510+
&Some(DEFAULT_TENANT.to_owned())
1511+
)
1512+
);
1513+
}
1514+
}

0 commit comments

Comments
 (0)