Now with more better tests

This commit is contained in:
Gregor Reitzenstein 2020-11-19 15:10:42 +01:00
parent 3b63e654e5
commit 4b9070fd17
2 changed files with 67 additions and 29 deletions

View File

@ -187,8 +187,10 @@ impl TryFrom<String> for RoleIdentifier {
fn try_from(s: String) -> std::result::Result<Self, Self::Error> { fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
if let Some((name, location)) = split_once(&s, '@') { if let Some((name, location)) = split_once(&s, '@') {
let location = &location[1..];
Ok(RoleIdentifier::Remote { name: name.to_string(), location: location.to_string() }) Ok(RoleIdentifier::Remote { name: name.to_string(), location: location.to_string() })
} else if let Some((name, source)) = split_once(&s, '%') { } else if let Some((name, source)) = split_once(&s, '%') {
let source = &source[1..];
Ok(RoleIdentifier::Local { name: name.to_string(), source: source.to_string() }) Ok(RoleIdentifier::Local { name: name.to_string(), source: source.to_string() })
} else { } else {
Err(RoleFromStrError::Invalid) Err(RoleFromStrError::Invalid)
@ -500,11 +502,45 @@ mod tests {
#[test] #[test]
fn load_examples_roles_test() { fn load_examples_roles_test() {
let roles = Role::load_file("examples/roles.toml") let mut roles = Role::load_file("examples/roles.toml")
.expect("Couldn't load the example role defs. Does `examples/roles.toml` exist?"); .expect("Couldn't load the example role defs. Does `examples/roles.toml` exist?");
let expected = vec![
(RoleIdentifier::Local { name: "testrole".to_string(), source: "lmdb".to_string() },
Role {
name: "Testrole".to_string(),
parents: vec![],
permissions: vec![
PermRule::Subtree(PermissionBuf::from_string("lab.test".to_string()))
],
}),
(RoleIdentifier::Local { name: "somerole".to_string(), source: "lmdb".to_string() },
Role {
name: "Somerole".to_string(),
parents: vec![
RoleIdentifier::local_from_str("lmdb".to_string(), "testparent".to_string()),
],
permissions: vec![
PermRule::Base(PermissionBuf::from_string("lab.some.admin".to_string()))
],
}),
(RoleIdentifier::Local { name: "testparent".to_string(), source: "lmdb".to_string() },
Role {
name: "Testparent".to_string(),
parents: vec![],
permissions: vec![
PermRule::Base(PermissionBuf::from_string("lab.some.write".to_string())),
PermRule::Base(PermissionBuf::from_string("lab.some.read".to_string())),
PermRule::Base(PermissionBuf::from_string("lab.some.disclose".to_string())),
],
}),
];
assert!(true) for (id, role) in expected {
assert_eq!(roles.remove(&id).unwrap(), role);
}
assert!(roles.is_empty())
} }
#[test] #[test]

View File

@ -112,38 +112,40 @@ mod tests {
#[test] #[test]
fn load_examples_descriptions_test() { fn load_examples_descriptions_test() {
let machines = MachineDescription::load_file("examples/machines.toml") let mut machines = MachineDescription::load_file("examples/machines.toml")
.expect("Couldn't load the example machine defs. Does `examples/machines.toml` exist?"); .expect("Couldn't load the example machine defs. Does `examples/machines.toml` exist?");
let expected: HashMap<MachineIdentifier, MachineDescription> let expected =
= HashMap::from_iter(vec![ vec![
(Uuid::parse_str("e5408099-d3e5-440b-a92b-3aabf7683d6b").unwrap(), (Uuid::parse_str("e5408099-d3e5-440b-a92b-3aabf7683d6b").unwrap(),
MachineDescription { MachineDescription {
name: "Somemachine".to_string(), name: "Somemachine".to_string(),
description: None, description: None,
privs: PrivilegesBuf { privs: PrivilegesBuf {
disclose: PermissionBuf::from_string("lab.some.disclose".to_string()), disclose: PermissionBuf::from_string("lab.some.disclose".to_string()),
read: PermissionBuf::from_string("lab.some.read".to_string()), read: PermissionBuf::from_string("lab.some.read".to_string()),
write: PermissionBuf::from_string("lab.some.write".to_string()), write: PermissionBuf::from_string("lab.some.write".to_string()),
manage: PermissionBuf::from_string("lab.some.admin".to_string()), manage: PermissionBuf::from_string("lab.some.admin".to_string()),
}, },
}), }),
(Uuid::parse_str("eaabebae-34d1-4a3a-912a-967b495d3d6e").unwrap(), (Uuid::parse_str("eaabebae-34d1-4a3a-912a-967b495d3d6e").unwrap(),
MachineDescription { MachineDescription {
name: "Testmachine".to_string(), name: "Testmachine".to_string(),
description: Some("An optional description".to_string()), description: Some("An optional description".to_string()),
privs: PrivilegesBuf { privs: PrivilegesBuf {
disclose: PermissionBuf::from_string("lab.test.read".to_string()), disclose: PermissionBuf::from_string("lab.test.read".to_string()),
read: PermissionBuf::from_string("lab.test.read".to_string()), read: PermissionBuf::from_string("lab.test.read".to_string()),
write: PermissionBuf::from_string("lab.test.write".to_string()), write: PermissionBuf::from_string("lab.test.write".to_string()),
manage: PermissionBuf::from_string("lab.test.admin".to_string()), manage: PermissionBuf::from_string("lab.test.admin".to_string()),
}, },
}), }),
].into_iter()); ];
for u in ["e5408099-d3e5-440b-a92b-3aabf7683d6b", "eaabebae-34d1-4a3a-912a-967b495d3d6e"].iter() { for (id, machine) in expected.into_iter() {
let uuid = Uuid::parse_str(u).unwrap();
assert_eq!(machines[&uuid], expected[&uuid]); assert_eq!(machines.remove(&id).unwrap(), machine);
} }
assert!(machines.is_empty());
} }
} }