Skip to content

Commit

Permalink
Add member creation test
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb committed Dec 4, 2023
1 parent 9a8dbca commit 41995f6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/cloud/src/auth/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions crates/cloud/src/users/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,49 @@ impl TryFrom<SetPasswordEmail> 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 {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 41995f6

Please sign in to comment.