Permalink
Browse files

fix: truncate username if created from email

  • Loading branch information...
Markus Kohlhase
Markus Kohlhase committed Jan 19, 2019
1 parent 7b88610 commit 26daae1d627315d0c79d048795817ac5be80a7ba
Showing with 268 additions and 209 deletions.
  1. +20 −1 src/core/usecases/create_new_user.rs
  2. +1 −1 src/core/usecases/mod.rs
  3. +224 −207 src/core/util/validate.rs
  4. +23 −0 src/ports/web/api/events.rs
@@ -44,12 +44,15 @@ const PW_GEN: PasswordGenerator = PasswordGenerator {
strict: false,
};

pub const MAX_USERNAME_LEN: usize = 40;

pub fn create_user_from_email<D: Db>(db: &mut D, email: &str) -> Result<String> {
let users: Vec<_> = db.all_users()?;
let username = match users.iter().find(|u| u.email == email) {
Some(u) => u.username.clone(),
None => {
let generated_username = slugify(&email).replace("-", "");
let mut generated_username = slugify(&email).replace("-", "");
generated_username.truncate(MAX_USERNAME_LEN);
let username = generated_username.clone();
let password = PW_GEN.generate_one().map_err(|e| e.to_string())?;
let u = NewUser {
@@ -216,4 +219,20 @@ mod tests {
assert!(bcrypt::verify("pass", &db.users[0].password));
}

#[test]
fn test_create_user_from_email() {
let mut db = MockDb::new();
assert_eq!(
create_user_from_email(&mut db, "mail@tld.com").unwrap(),
"mailtldcom"
);
assert_eq!(
create_user_from_email(
&mut db,
"a-very-very-long-email@with-a-very-very-long-domain.com"
)
.unwrap(),
"averyverylongemailwithaveryverylongdomai"
);
}
}
@@ -10,7 +10,7 @@ use uuid::Uuid;
mod confirm_email;
mod create_new_entry;
mod create_new_event;
mod create_new_user;
pub mod create_new_user;
mod delete_event;
mod find_duplicates;
mod login;
Oops, something went wrong.

0 comments on commit 26daae1

Please sign in to comment.