diff --git a/crates/cloud/src/auth/users.rs b/crates/cloud/src/auth/users.rs index fc62a78f..920f05b7 100644 --- a/crates/cloud/src/auth/users.rs +++ b/crates/cloud/src/auth/users.rs @@ -75,6 +75,13 @@ impl ViewUser { } } +#[cfg(test)] +impl CreateUser { + pub(crate) fn test(data: api::NewUser) -> Self { + Self { data, _private: () } + } +} + /// Try to get privileges to create the given user. Must be able /// to edit the target group (if user is in a group). Moderators /// or admins can only be created by others with their role (or diff --git a/crates/cloud/src/users/actions.rs b/crates/cloud/src/users/actions.rs index e02eb5d1..3ddc45bb 100644 --- a/crates/cloud/src/users/actions.rs +++ b/crates/cloud/src/users/actions.rs @@ -563,10 +563,49 @@ impl TryFrom for lettre::Message { #[cfg(test)] mod tests { + use netsblox_cloud_common::Group; + use crate::test_utils; use super::*; + #[actix_web::test] + async fn test_create_member() { + let owner: User = api::NewUser { + username: "owner".into(), + email: "owner@netsblox.org".into(), + password: None, + group_id: None, + role: None, + } + .into(); + let group = Group::new(owner.username.to_owned(), "Some name".into()); + + test_utils::setup() + .with_users(&[owner.clone()]) + .with_groups(&[group.clone()]) + .run(|app_data| async move { + let actions = app_data.as_user_actions(); + + let new_user = api::NewUser { + username: "member".into(), + email: "member@netsblox.org".into(), + password: None, + group_id: Some(group.id.to_owned()), + role: None, + }; + let auth_cu = auth::CreateUser::test(new_user); + let user = actions.create_user(auth_cu).await.unwrap(); + assert!(user.group_id.is_some(), "User is not assigned to a group."); + assert_eq!( + user.group_id.unwrap(), + group.id, + "User assigned to incorrect group" + ); + }) + .await; + } + #[actix_web::test] async fn test_ban_idempotent() { let user: User = api::NewUser { @@ -632,8 +671,21 @@ mod tests { assert!(!is_valid_username("hello@gmail.com")); } + #[actix_web::test] + async fn test_is_valid_username_length() { + assert!(!is_valid_username( + "testCreateUser1701709207213testCreateUser1701709207213" + )); + } + #[actix_web::test] async fn test_is_valid_username_vulgar() { assert!(!is_valid_username("shit")); } + + #[actix_web::test] + async fn test_ensure_valid_email() { + let result = ensure_valid_email("noreply@netsblox.org"); + assert!(result.is_ok()); + } }