utils (database user struct): add more tests and change username to lowercase

This commit is contained in:
MedzikUser 2022-04-21 20:53:30 +02:00
parent 3428edfc92
commit e0e487fbf4
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
1 changed files with 23 additions and 2 deletions

View File

@ -3,7 +3,7 @@ use uuid::Uuid;
use crate::crypto::CryptographicHash; use crate::crypto::CryptographicHash;
#[derive(Debug)] #[derive(Debug, sqlx::FromRow)]
pub struct User { pub struct User {
pub id: String, pub id: String,
pub username: String, pub username: String,
@ -24,12 +24,33 @@ impl User {
let sha1_name = CryptographicHash::hash("SHA-1", username.as_bytes()).unwrap(); let sha1_name = CryptographicHash::hash("SHA-1", username.as_bytes()).unwrap();
let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string(); let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string();
let username = username.to_lowercase();
let password = encode(CryptographicHash::hash("SHA-512", password.as_bytes()).unwrap()); let password = encode(CryptographicHash::hash("SHA-512", password.as_bytes()).unwrap());
Self { Self {
id, id,
username: username.to_string(), username,
password, password,
} }
} }
} }
#[cfg(test)]
mod tests {
use super::User;
#[test]
fn check_username_is_in_lowercase() {
let user = User::new("MEdzIk", "SuperSecretPassword123!");
assert_eq!(user.username, "medzik")
}
#[test]
fn check_password_is_hashed() {
let password = "Password";
let user = User::new("test", password);
assert!(user.password != password)
}
}