umanux/tests/create_user_test.rs

124 lines
3.3 KiB
Rust
Raw Normal View History

extern crate test_bin;
2020-11-16 10:37:18 +01:00
extern crate umanux;
2020-11-10 13:45:21 +01:00
mod testfiles;
#[test]
fn test_create_user_function() {
use testfiles::Fixture;
2020-11-11 06:57:46 +01:00
use std::fs;
2020-11-16 10:37:18 +01:00
use umanux::api::UserDBWrite;
use umanux::api::UserRead;
2020-11-11 06:57:46 +01:00
let p = Fixture::copy("passwd");
let s = Fixture::copy("shadow");
let g = Fixture::copy("group");
let pf = fs::read_to_string(&p.path).unwrap();
2020-11-16 10:37:18 +01:00
let mf = umanux::Files {
2020-11-11 06:57:46 +01:00
passwd: Some(p.path.clone()),
shadow: Some(s.path),
group: Some(g.path),
};
2020-11-16 10:37:18 +01:00
let mut db = umanux::UserDBLocal::load_files(mf).unwrap();
2020-11-11 06:57:46 +01:00
2020-11-16 10:37:18 +01:00
let user_res: Result<&umanux::User, umanux::UserLibError> = db.new_user(
umanux::api::CreateUserArgs::builder()
2020-11-11 06:57:46 +01:00
.username("test2")
2020-11-16 10:37:18 +01:00
// .delete_home(umanux::api::DeleteHome::Delete)
2020-11-11 06:57:46 +01:00
.build()
.unwrap(),
);
let password_file_string = fs::read_to_string(&p.path).unwrap();
let shadow_file_string = fs::read_to_string(&p.path).unwrap();
2020-11-11 06:57:46 +01:00
assert_eq!(user_res.unwrap().get_username().unwrap(), "test2");
let pflines = pf.lines();
let pflines2 = password_file_string.lines();
2020-11-11 06:57:46 +01:00
for (l1, l2) in pflines.zip(pflines2) {
dbg!(l1, l2);
assert!(l1 == l2);
}
assert!(password_file_string
.lines()
.last()
.unwrap()
.starts_with("test2"));
assert!(shadow_file_string
.lines()
.last()
.unwrap()
.starts_with("test2"));
2020-11-11 06:57:46 +01:00
}
#[test]
fn test_create_user_binary() {
use testfiles::Fixture;
2020-11-10 13:45:21 +01:00
use std::fs;
let p = Fixture::copy("passwd");
let s = Fixture::copy("shadow");
let g = Fixture::copy("group");
//dbg!(&p, &s, &g);
2020-11-11 08:20:09 +01:00
let passwd_string = fs::read_to_string(&p.path).unwrap();
let passwd_lines = passwd_string.lines();
let shadow_string = fs::read_to_string(&s.path).unwrap();
let shadow_lines = shadow_string.lines();
2020-11-10 13:45:21 +01:00
let out = test_bin::get_test_bin("create_user")
.args(&[
"testuser3",
"-p",
p.path.to_str().unwrap(),
"-s",
s.path.to_str().unwrap(),
"-g",
g.path.to_str().unwrap(),
])
.output()
.expect("Failed to run the command");
println!(
"The output after running: {}",
String::from_utf8_lossy(&out.stdout)
);
println!(
"The error after running: {}",
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(String::from_utf8_lossy(&out.stdout), "");
assert_eq!(String::from_utf8_lossy(&out.stderr), "");
2020-11-10 13:45:21 +01:00
let passwd_string_after = fs::read_to_string(&p.path).unwrap();
let passwd_lines_after = passwd_string_after.lines();
let shadow_string_after = fs::read_to_string(&s.path).unwrap();
let shadow_lines_after = shadow_string_after.lines();
for (l1, l2) in passwd_lines.zip(passwd_lines_after) {
//dbg!(l1, l2);
assert!(l1 == l2);
}
assert_eq!(
passwd_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
2020-11-10 13:45:21 +01:00
);
//dbg!(&shadow_string_after);
for (l1, l2) in shadow_lines.zip(shadow_lines_after) {
//dbg!(l1, l2);
2020-11-10 13:45:21 +01:00
assert!(l1 == l2);
}
assert_eq!(
shadow_string_after
.lines()
.last()
.unwrap()
.starts_with("testuser3"),
true
);
2020-11-10 13:45:21 +01:00
}