make the username part of the invalid message.
This commit is contained in:
parent
1ab0adbc06
commit
f84f15b0cd
@ -263,12 +263,18 @@ impl<'a> TryFrom<&'a str> for Username<'a> {
|
|||||||
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
|
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref USERVALIDATION: Regex =
|
static ref USERVALIDATION: Regex =
|
||||||
Regex::new("^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\\$)$").unwrap();
|
Regex::new("^[a-z_]([a-z0-9_\\-]{0,31}|[a-z0-9_\\-]{0,30}\\$)$").unwrap();
|
||||||
}
|
}
|
||||||
if USERVALIDATION.is_match(source) {
|
if USERVALIDATION.is_match(source) {
|
||||||
Ok(Self { username: source })
|
Ok(Self { username: source })
|
||||||
|
} else if source == "Debian-exim" {
|
||||||
|
warn!("username {} is not a valid username. This might cause problems. (It is default in Debian and Ubuntu)", source);
|
||||||
|
Ok(Self { username: source })
|
||||||
} else {
|
} else {
|
||||||
Err(UserLibError::Message("Invalid username".into()))
|
Err(UserLibError::Message(format!(
|
||||||
|
"Invalid username {}",
|
||||||
|
source
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -401,21 +407,26 @@ fn test_username_validation() {
|
|||||||
// Failing tests
|
// Failing tests
|
||||||
let umlauts = Username::try_from("täst"); // umlauts
|
let umlauts = Username::try_from("täst"); // umlauts
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Err(UserLibError::Message("Invalid username".into())),
|
Err(UserLibError::Message("Invalid username täst".into())),
|
||||||
umlauts
|
umlauts
|
||||||
);
|
);
|
||||||
let number_first = Username::try_from("11elf"); // numbers first
|
let number_first = Username::try_from("11elf"); // numbers first
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Err(UserLibError::Message("Invalid username".into())),
|
Err(UserLibError::Message("Invalid username 11elf".into())),
|
||||||
number_first
|
number_first
|
||||||
);
|
);
|
||||||
let slashes = Username::try_from("test/name"); // slashes in the name
|
let slashes = Username::try_from("test/name"); // slashes in the name
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Err(UserLibError::Message("Invalid username".into())),
|
Err(UserLibError::Message("Invalid username test/name".into())),
|
||||||
slashes
|
slashes
|
||||||
);
|
);
|
||||||
let long = Username::try_from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // maximum size 32 letters
|
let long = Username::try_from("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // maximum size 32 letters
|
||||||
assert_eq!(Err(UserLibError::Message("Invalid username".into())), long);
|
assert_eq!(
|
||||||
|
Err(UserLibError::Message(
|
||||||
|
"Invalid username aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into()
|
||||||
|
)),
|
||||||
|
long
|
||||||
|
);
|
||||||
// Working tests
|
// Working tests
|
||||||
let single = Username::try_from("t"); // single characters are ok
|
let single = Username::try_from("t"); // single characters are ok
|
||||||
assert_eq!(single.unwrap().username, "t");
|
assert_eq!(single.unwrap().username, "t");
|
||||||
|
Loading…
Reference in New Issue
Block a user