Compare commits
9 commits
1aab654d39
...
c98eb9cdbb
Author | SHA1 | Date | |
---|---|---|---|
c98eb9cdbb | |||
7033ad7013 | |||
42dae815a6 | |||
8fb44d0ad9 | |||
64d9e9d51d | |||
d41370cbb2 | |||
9dbfeef94f | |||
cb84d40a48 | |||
2d02e810df |
2 changed files with 127 additions and 5 deletions
|
@ -17,10 +17,11 @@ pub struct CreateUser {
|
|||
/// 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