just unit test the creational struct for validating
This commit is contained in:
parent
09f4ddc3a4
commit
2d02e810df
2 changed files with 130 additions and 8 deletions
|
@ -13,13 +13,14 @@ pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
|
||||||
|
|
||||||
#[derive(Deserialize, Validate, ToSchema)]
|
#[derive(Deserialize, Validate, ToSchema)]
|
||||||
pub struct CreateUser {
|
pub struct CreateUser {
|
||||||
#[validate(length(min = 4))]
|
#[validate(length(min = 4, max = 255))]
|
||||||
/// Username (minimum 4 characters)
|
/// Username (minimum 4 characters, maximum 255 characters)
|
||||||
username: String,
|
username: String,
|
||||||
/// Full name of the user
|
#[validate(length(min = 3))]
|
||||||
|
/// Full name of the user (minimum 3 characters)
|
||||||
name: String,
|
name: String,
|
||||||
#[validate(length(min = 8))]
|
#[validate(length(min = 8, max = 255))]
|
||||||
/// Password (minimum 8 characters)
|
/// Password (minimum 8 characters, maximum 255 characters)
|
||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,3 +133,74 @@ async fn delete_user(
|
||||||
db.delete_user(id).await?;
|
db.delete_user(id).await?;
|
||||||
Ok(web::Json(format!("User {} deleted", id)))
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ use crate::entity::project;
|
||||||
use sea_orm::ActiveValue::{NotSet, Set, Unchanged};
|
use sea_orm::ActiveValue::{NotSet, Set, Unchanged};
|
||||||
use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait};
|
use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use utoipa::ToSchema;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use utoipa::ToSchema;
|
|
||||||
|
|
||||||
#[derive(Deserialize, Validate, ToSchema)]
|
#[derive(Deserialize, Validate, ToSchema)]
|
||||||
pub struct CreateProject {
|
pub struct CreateProject {
|
||||||
#[validate(length(min = 3))]
|
#[validate(length(min = 3, max = 255))]
|
||||||
/// Project name (minimum 3 characters)
|
/// Project name (minimum 3 characters and maximum 255 characters)
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,3 +84,53 @@ impl Database {
|
||||||
Ok(project)
|
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