just unit test the creational struct for validating
This commit is contained in:
parent
8881e4d7eb
commit
0b06805963
2 changed files with 129 additions and 7 deletions
|
@ -13,14 +13,15 @@ pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
|
|||
|
||||
#[derive(Deserialize, Validate, ToSchema)]
|
||||
pub struct CreateUser {
|
||||
#[validate(length(min = 4))]
|
||||
/// Username (minimum 4 characters)
|
||||
#[validate(length(min = 4, max = 255))]
|
||||
/// Username (minimum 4 characters, maximum 255 characters)
|
||||
/// TODO: Don't allow spaces, only alphanumeric characters and underscores
|
||||
username: String,
|
||||
/// Full name of the user
|
||||
#[validate(length(min = 3))]
|
||||
/// Full name of the user (minimum 3 characters)
|
||||
name: String,
|
||||
#[validate(length(min = 8))]
|
||||
/// Password (minimum 8 characters)
|
||||
#[validate(length(min = 8, max = 255))]
|
||||
/// Password (minimum 8 characters, maximum 255 characters)
|
||||
password: String,
|
||||
}
|
||||
|
||||
|
@ -135,3 +136,74 @@ async fn delete_user(
|
|||
db.delete_user(id).await?;
|
||||
Ok(web::Json(format!("User {} deleted", id)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_valid() {
|
||||
let user = CreateUser {
|
||||
username: "testuser".to_string(),
|
||||
name: "Test User".to_string(),
|
||||
password: "password123".to_string(),
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_ok());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_username_invalid() {
|
||||
let user = CreateUser {
|
||||
username: "usr".to_string(), // too short
|
||||
name: "Test User".to_string(),
|
||||
password: "password".to_string(),
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_username_too_long() {
|
||||
let user = CreateUser {
|
||||
username: "a".repeat(256), // too long
|
||||
name: "Test User".to_string(),
|
||||
password: "password123".to_string(),
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_name_invalid() {
|
||||
let user = CreateUser {
|
||||
username: "testuser".to_string(),
|
||||
name: "".to_string(), // empty name
|
||||
password: "password123".to_string(),
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_password_invalid() {
|
||||
let user = CreateUser {
|
||||
username: "testuser".to_string(),
|
||||
name: "Test User".to_string(),
|
||||
password: "pass".to_string(), // too short
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_user_struct_password_too_long() {
|
||||
let user = CreateUser {
|
||||
username: "testuser".to_string(),
|
||||
name: "Test User".to_string(),
|
||||
password: "a".repeat(256), // too long
|
||||
};
|
||||
let validation_result = user.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ use validator::Validate;
|
|||
|
||||
#[derive(Deserialize, Validate, ToSchema)]
|
||||
pub struct CreateProject {
|
||||
#[validate(length(min = 3))]
|
||||
/// Project name (minimum 3 characters)
|
||||
#[validate(length(min = 3, max = 255))]
|
||||
/// Project name (minimum 3 characters and maximum 255 characters)
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
|
@ -84,3 +84,53 @@ impl Database {
|
|||
Ok(project)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_project_struct_valid() {
|
||||
let project = CreateProject {
|
||||
name: "Test Project".to_string(),
|
||||
};
|
||||
let validation_result = project.validate();
|
||||
assert!(validation_result.is_ok());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_project_struct_invalid_too_short() {
|
||||
let project = CreateProject {
|
||||
name: "TP".to_string(), // too short
|
||||
};
|
||||
let validation_result = project.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_project_struct_empty() {
|
||||
let project = CreateProject {
|
||||
name: "".to_string(), // empty string
|
||||
};
|
||||
let validation_result = project.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_project_struct_min_length() {
|
||||
let project = CreateProject {
|
||||
name: "abc".to_string(), // exactly at min length
|
||||
};
|
||||
let validation_result = project.validate();
|
||||
assert!(validation_result.is_ok());
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn test_validation_create_project_struct_long_name() {
|
||||
// 256 characters long should be invalid because of max length
|
||||
let long_name = "a".repeat(256);
|
||||
let project = CreateProject { name: long_name };
|
||||
let validation_result = project.validate();
|
||||
assert!(validation_result.is_err());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue