Add comprehensive tests for IAM permissions, roles, teams, and users

- Implemented unit tests for PermissionsController and PermissionsService, covering create, read, update, and delete operations.
- Added tests for RolesController and RolesService, including handling of duplicates and retrieval by ID.
- Developed tests for TeamsController, including creation, retrieval, updating, deletion, and search functionality.
- Created tests for UsersController, ensuring user creation and validation of attributes.
- Each test includes setup, execution, and cleanup to maintain database integrity.
This commit is contained in:
MythEclipse
2025-09-25 22:06:21 +07:00
parent 4ecdcd12d2
commit 7056f8c8a6
25 changed files with 4761 additions and 432 deletions
@@ -0,0 +1,82 @@
#[cfg(test)]
mod tests {
use crate::get_meta_request_dto;
use imphnen_cms::{
v1::landing::events::{
events_controller::EventsController,
events_dto::{EventsCreateRequestDto, EventsUpdateRequestDto},
},
};
#[tokio::test]
async fn test_get_event_list_controller() {
let app_state = crate::get_app_state().await;
let response = EventsController::get_event_list(&app_state, get_meta_request_dto(1, 10)).await;
assert_eq!(response.status(), 200);
}
#[tokio::test]
async fn test_get_event_by_id_controller_not_found() {
let app_state = crate::get_app_state().await;
let response = EventsController::get_event_by_id(&app_state, "non-existent-uuid-123456789".to_string()).await;
assert_eq!(response.status(), 404);
}
#[tokio::test]
async fn test_create_event_controller() {
let app_state = crate::get_app_state().await;
let event_request = EventsCreateRequestDto {
name: "Test Event".to_string(),
description: "Test event description".to_string(),
detail_link: Some("https://example.com/event".to_string()),
price: Some(100.0),
is_online: Some(true),
start_date: "2024-01-01T00:00:00Z".to_string(),
end_date: "2024-01-02T00:00:00Z".to_string(),
location: Some("Online".to_string()),
};
let response = EventsController::create_event(&app_state, event_request).await;
assert_eq!(response.status(), 201);
}
#[tokio::test]
async fn test_create_event_controller_invalid_data() {
let app_state = crate::get_app_state().await;
let event_request = EventsCreateRequestDto {
name: "".to_string(),
description: "".to_string(),
detail_link: None,
price: None,
is_online: None,
start_date: "invalid-date".to_string(),
end_date: "2024-01-02T00:00:00Z".to_string(),
location: None,
};
let response = EventsController::create_event(&app_state, event_request).await;
assert_eq!(response.status(), 400);
}
#[tokio::test]
async fn test_update_event_controller_not_found() {
let app_state = crate::get_app_state().await;
let update_request = EventsUpdateRequestDto {
name: Some("Updated Event".to_string()),
description: Some("Updated description".to_string()),
detail_link: Some("https://example.com/updated".to_string()),
price: Some(150.0),
is_online: Some(false),
start_date: Some("2024-02-01T00:00:00Z".to_string()),
end_date: Some("2024-02-02T00:00:00Z".to_string()),
location: Some("Offline".to_string()),
};
let response = EventsController::update_event(&app_state, "non-existent-uuid-123456789".to_string(), update_request).await;
assert_eq!(response.status(), 400);
}
#[tokio::test]
async fn test_delete_event_controller_not_found() {
let app_state = crate::get_app_state().await;
let response = EventsController::delete_event(&app_state, "non-existent-uuid-123456789".to_string()).await;
assert_eq!(response.status(), 400);
}
}
@@ -0,0 +1,78 @@
#[cfg(test)]
mod tests {
use imphnen_cms::{
v1::landing::events::{
events_repository::EventsRepository,
events_schema::EventsSchema,
},
};
use imphnen_utils::make_thing_from_enum;
#[tokio::test]
async fn test_query_event_list() {
let app_state = crate::get_app_state().await;
let repo = EventsRepository::new(&app_state);
let result = repo.query_event_list(crate::get_meta_request_dto(1, 10)).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_query_event_by_id_not_found() {
let app_state = crate::get_app_state().await;
let repo = EventsRepository::new(&app_state);
let result = repo.query_event_by_id("non-existent-uuid-123456789".to_string()).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_query_create_event() {
let app_state = crate::get_app_state().await;
let repo = EventsRepository::new(&app_state);
let event = EventsSchema {
id: make_thing_from_enum("events", &uuid::Uuid::new_v4().to_string()),
name: "Test Event".to_string(),
description: "Test description".to_string(),
detail_link: Some("https://example.com".to_string()),
price: Some(100.0),
is_online: true,
start_date: "2024-01-01T00:00:00Z".to_string(),
end_date: "2024-01-02T00:00:00Z".to_string(),
location: Some("Online".to_string()),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let result = repo.query_create_event(event).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_query_update_event_not_found() {
let app_state = crate::get_app_state().await;
let repo = EventsRepository::new(&app_state);
let event = EventsSchema {
id: make_thing_from_enum("events", &"non-existent-uuid-123456789".to_string()),
name: "Updated Event".to_string(),
description: "Updated description".to_string(),
detail_link: Some("https://example.com/updated".to_string()),
price: Some(150.0),
is_online: false,
start_date: "2024-02-01T00:00:00Z".to_string(),
end_date: "2024-02-02T00:00:00Z".to_string(),
location: Some("Offline".to_string()),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let result = repo.query_update_event(event).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_query_delete_event_not_found() {
let app_state = crate::get_app_state().await;
let repo = EventsRepository::new(&app_state);
let result = repo.query_delete_event("non-existent-uuid-123456789".to_string()).await;
assert!(result.is_err());
}
}
@@ -0,0 +1,82 @@
#[cfg(test)]
mod tests {
use crate::get_meta_request_dto;
use imphnen_cms::{
v1::landing::events::{
events_service::EventsService,
events_dto::{EventsCreateRequestDto, EventsUpdateRequestDto},
},
};
#[tokio::test]
async fn test_get_event_list_service() {
let app_state = crate::get_app_state().await;
let response = EventsService::get_event_list(&app_state, get_meta_request_dto(1, 10)).await;
assert_eq!(response.status(), 200);
}
#[tokio::test]
async fn test_get_event_by_id_service_not_found() {
let app_state = crate::get_app_state().await;
let response = EventsService::get_event_by_id(&app_state, "non-existent-uuid-123456789".to_string()).await;
assert_eq!(response.status(), 404);
}
#[tokio::test]
async fn test_create_event_service() {
let app_state = crate::get_app_state().await;
let event_request = EventsCreateRequestDto {
name: "Test Event".to_string(),
description: "Test event description".to_string(),
detail_link: Some("https://example.com/event".to_string()),
price: Some(100.0),
is_online: Some(true),
start_date: "2024-01-01T00:00:00Z".to_string(),
end_date: "2024-01-02T00:00:00Z".to_string(),
location: Some("Online".to_string()),
};
let response = EventsService::create_event(&app_state, event_request).await;
assert_eq!(response.status(), 201);
}
#[tokio::test]
async fn test_create_event_service_invalid_data() {
let app_state = crate::get_app_state().await;
let event_request = EventsCreateRequestDto {
name: "".to_string(),
description: "".to_string(),
detail_link: None,
price: None,
is_online: None,
start_date: "invalid-date".to_string(),
end_date: "2024-01-02T00:00:00Z".to_string(),
location: None,
};
let response = EventsService::create_event(&app_state, event_request).await;
assert_eq!(response.status(), 400);
}
#[tokio::test]
async fn test_update_event_service_not_found() {
let app_state = crate::get_app_state().await;
let update_request = EventsUpdateRequestDto {
name: Some("Updated Event".to_string()),
description: Some("Updated description".to_string()),
detail_link: Some("https://example.com/updated".to_string()),
price: Some(150.0),
is_online: Some(false),
start_date: Some("2024-02-01T00:00:00Z".to_string()),
end_date: Some("2024-02-02T00:00:00Z".to_string()),
location: Some("Offline".to_string()),
};
let response = EventsService::update_event(&app_state, "non-existent-uuid-123456789".to_string(), update_request).await;
assert_eq!(response.status(), 400);
}
#[tokio::test]
async fn test_delete_event_service_not_found() {
let app_state = crate::get_app_state().await;
let response = EventsService::delete_event(&app_state, "non-existent-uuid-123456789".to_string()).await;
assert_eq!(response.status(), 400);
}
}
@@ -0,0 +1,389 @@
#[cfg(test)]
mod tests {
use crate::{get_meta_request_dto, UsersRepository};
use axum::{http::StatusCode, response::Response};
use imphnen_cms::{
v1::landing::testimonials::{
testimonials_controller::TestimonialsController,
testimonials_dto::{TestimonialsCreateRequestDto, TestimonialsUpdateRequestDto},
testimonials_schema::TestimonialsSchema,
},
};
use imphnen_entities::UsersSchema;
use imphnen_utils::make_thing_from_enum;
#[tokio::test]
async fn test_get_testimonial_list_controller() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test testimonials
let testimonial_names = vec![
"testimonial_list_1".to_string(),
"testimonial_list_2".to_string(),
"testimonial_list_3".to_string(),
];
for name in &testimonial_names {
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: format!("Test User {}", name),
email: format!("test{}@example.com", name),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user).await;
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
role: "Mentor".to_string(),
content: format!("Great testimonial content for {}", name),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let _ = repo.query_create_testimonial(testimonial).await;
}
// Get testimonial list through controller
let response = TestimonialsController::get_testimonial_list(&app_state, get_meta_request_dto(1, 10))
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Clean up
for name in testimonial_names {
let user = UsersRepository::new(&app_state)
.query_user_by_email(format!("test{}@example.com", name))
.await
.unwrap();
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}
#[tokio::test]
async fn test_get_testimonial_by_id_controller() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test testimonial
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "test@example.com".to_string(),
..Default::default()
};
let create_user_result = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
assert!(create_user_result.is_ok());
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Great testimonial content".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Get testimonial by ID through controller
let response = TestimonialsController::get_testimonial_by_id(&app_state, testimonial_id.clone())
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_get_testimonial_by_id_controller_not_found() {
let app_state = crate::get_app_state().await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Get non-existent testimonial by ID through controller
let response = TestimonialsController::get_testimonial_by_id(&app_state, non_existent_id)
.await;
// Verify not found response
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_create_testimonial_controller() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let create_user_result = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
assert!(create_user_result.is_ok());
// Test data
let testimonial_request = TestimonialsCreateRequestDto {
role: "Mentor".to_string(),
content: "This is a test testimonial content for controller test".to_string(),
};
// Create testimonial through controller
let response = TestimonialsController::create_testimonial(
&app_state,
testimonial_request.clone(),
&user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::CREATED);
// Verify testimonial was created in database
let created_testimonials = repo.query_testimonial_list(get_meta_request_dto(1, 10)).await.unwrap();
assert!(created_testimonials.data.iter().any(|t| t.content == testimonial_request.content));
// Clean up
let created_testimonial = repo.query_testimonial_list(get_meta_request_dto(1, 10)).await.unwrap();
for t in created_testimonials.data {
if t.content == testimonial_request.content {
let _ = repo.query_delete_testimonial(t.id.id.to_raw()).await;
}
}
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_create_testimonial_controller_invalid_data() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Test data with empty content (should fail validation)
let testimonial_request = TestimonialsCreateRequestDto {
role: "Mentor".to_string(),
content: "".to_string(), // Empty content should fail validation
};
// Create testimonial through controller
let response = TestimonialsController::create_testimonial(
&app_state,
testimonial_request,
&user,
)
.await;
// Verify bad request response (validation error)
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_update_testimonial_controller() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let original_content = "Original testimonial content for update test".to_string();
let new_content = "Updated testimonial content for update test".to_string();
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: original_content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Prepare update request
let update_request = TestimonialsUpdateRequestDto {
role: Some("Updated Mentor".to_string()),
content: Some(new_content.clone()),
};
// Update testimonial through controller
let response = TestimonialsController::update_testimonial(
&app_state, update_request, testimonial_id.clone(), &user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Verify testimonial was updated in database
let updated_testimonial = repo
.query_testimonial_by_id(testimonial_id.clone())
.await
.unwrap();
assert_eq!(updated_testimonial.content, new_content);
assert_eq!(updated_testimonial.role, "Updated Mentor");
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_update_testimonial_controller_not_found() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Prepare update request
let update_request = TestimonialsUpdateRequestDto {
role: Some("Updated Mentor".to_string()),
content: Some("Updated content".to_string()),
};
// Update non-existent testimonial through controller
let response = TestimonialsController::update_testimonial(
&app_state, update_request, non_existent_id, &user,
)
.await;
// Verify not found response
assert_eq!(response.status(), StatusCode::NOT_FOUND);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_delete_testimonial_controller() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for delete test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Verify testimonial exists before deletion
let exists_before = repo.query_testimonial_by_id(testimonial_id.clone()).await.is_ok();
assert!(exists_before);
// Delete testimonial through controller
let response = TestimonialsController::delete_testimonial(
&app_state, testimonial_id.clone(), &user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Verify testimonial was soft-deleted from database
let deleted_testimonial = repo.query_testimonial_by_id(testimonial_id.clone()).await;
assert!(deleted_testimonial.is_err());
// Clean up - no need since it's already soft-deleted
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_delete_testimonial_controller_not_found() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Delete non-existent testimonial through controller
let response = TestimonialsController::delete_testimonial(
&app_state, non_existent_id, &user,
)
.await;
// Verify not found response
assert_eq!(response.status(), StatusCode::NOT_FOUND);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}
@@ -0,0 +1,493 @@
#[cfg(test)]
mod tests {
use crate::{get_meta_request_dto, UsersRepository};
use imphnen_cms::{
v1::landing::testimonials::{
testimonials_repository::TestimonialsRepository,
testimonials_schema::TestimonialsSchema,
},
};
use imphnen_entities::UsersSchema;
use imphnen_utils::make_thing_from_enum;
#[tokio::test]
async fn test_query_testimonial_list() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test users and testimonials
let num_testimonials = 5;
let testimonial_contents = vec![
"Testimonial content 1".to_string(),
"Testimonial content 2".to_string(),
"Testimonial content 3".to_string(),
"Testimonial content 4".to_string(),
"Testimonial content 5".to_string(),
];
for (i, content) in testimonial_contents.iter().enumerate() {
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: format!("Test User {}", i + 1),
email: format!("testuser{}@example.com", i + 1),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: format!("Role {}", i + 1),
content: content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let _ = repo.query_create_testimonial(testimonial).await;
}
// Test with pagination
let result = repo.query_testimonial_list(get_meta_request_dto(1, 10)).await;
assert!(result.is_ok());
let response = result.unwrap();
assert_eq!(response.data.len(), num_testimonials as usize);
// Test with smaller page size
let result = repo.query_testimonial_list(get_meta_request_dto(1, 2)).await;
assert!(result.is_ok());
let response = result.unwrap();
assert_eq!(response.data.len(), 2);
// Clean up
for content in testimonial_contents {
let user = UsersRepository::new(&app_state)
.query_user_by_email(format!("testuser{}@example.com", content.chars().take(8).collect::<String>()))
.await
.unwrap();
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}
#[tokio::test]
async fn test_query_testimonial_by_id_found() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial_content = "Test testimonial content for by ID test".to_string();
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: testimonial_content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Query testimonial by ID
let result = repo.query_testimonial_by_id(testimonial_id.clone()).await;
assert!(result.is_ok());
let found_testimonial = result.unwrap();
assert_eq!(found_testimonial.content, testimonial_content);
assert_eq!(found_testimonial.role, "Mentor");
assert!(!found_testimonial.is_deleted);
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_testimonial_by_id_not_found() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Query non-existent testimonial by ID
let result = repo.query_testimonial_by_id(non_existent_id).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial not found");
}
#[tokio::test]
async fn test_query_testimonial_by_id_deleted() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for deleted test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Soft delete the testimonial
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
// Try to query deleted testimonial by ID
let result = repo.query_testimonial_by_id(testimonial_id).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial not found");
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_create_testimonial() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial data
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for create test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
// Create testimonial
let result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(result.is_ok());
let created_testimonial = result.unwrap();
assert_eq!(created_testimonial.content, testimonial.content);
assert_eq!(created_testimonial.role, testimonial.role);
assert!(!created_testimonial.is_deleted);
// Verify it was created in database
let found_testimonial = repo.query_testimonial_by_id(created_testimonial.id.id.to_raw()).await;
assert!(found_testimonial.is_ok());
assert_eq!(found_testimonial.unwrap().content, testimonial.content);
// Clean up
let _ = repo.query_delete_testimonial(created_testimonial.id.id.to_raw()).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_update_testimonial() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let original_content = "Original testimonial content for update test".to_string();
let new_content = "Updated testimonial content for update test".to_string();
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: original_content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Prepare updated testimonial
let updated_testimonial = TestimonialsSchema {
id: created_testimonial.id,
user: created_testimonial.user,
role: "Updated Mentor".to_string(),
content: new_content.clone(),
created_at: created_testimonial.created_at,
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
// Update testimonial
let result = repo.query_update_testimonial(updated_testimonial).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Success update testimonial");
// Verify it was updated in database
let found_testimonial = repo.query_testimonial_by_id(testimonial_id).await;
assert!(found_testimonial.is_ok());
let updated = found_testimonial.unwrap();
assert_eq!(updated.content, new_content);
assert_eq!(updated.role, "Updated Mentor");
assert!(!updated.is_deleted);
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_update_testimonial_not_found() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create non-existent testimonial ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Prepare updated testimonial with non-existent ID
let updated_testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &non_existent_id),
user: user.id,
role: "Updated Mentor".to_string(),
content: "Updated content".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
// Try to update non-existent testimonial
let result = repo.query_update_testimonial(updated_testimonial).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial not found");
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_update_testimonial_deleted() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for deleted update test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Soft delete the testimonial
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
// Prepare updated testimonial
let updated_testimonial = TestimonialsSchema {
id: created_testimonial.id,
user: created_testimonial.user,
role: "Updated Mentor".to_string(),
content: "Updated content".to_string(),
created_at: created_testimonial.created_at,
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
// Try to update deleted testimonial
let result = repo.query_update_testimonial(updated_testimonial).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial already deleted");
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_delete_testimonial() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for delete test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Verify testimonial exists before deletion
let exists_before = repo.query_testimonial_by_id(testimonial_id.clone()).await.is_ok();
assert!(exists_before);
// Delete testimonial
let result = repo.query_delete_testimonial(testimonial_id.clone()).await;
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Success delete testimonial");
// Verify testimonial was soft-deleted from database
let deleted_testimonial = repo.query_testimonial_by_id(testimonial_id.clone()).await;
assert!(deleted_testimonial.is_err());
assert_eq!(deleted_testimonial.unwrap_err().to_string(), "Testimonial not found");
// Clean up - no need since it's already soft-deleted
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_query_delete_testimonial_not_found() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Try to delete non-existent testimonial
let result = repo.query_delete_testimonial(non_existent_id).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial not found");
}
#[tokio::test]
async fn test_query_delete_testimonial_already_deleted() {
let app_state = crate::get_app_state().await;
let repo = TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for already deleted test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Soft delete the testimonial twice
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
let result = repo.query_delete_testimonial(testimonial_id).await;
// Verify second deletion fails
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Testimonial not found");
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}
@@ -0,0 +1,544 @@
#[cfg(test)]
mod tests {
use crate::{get_meta_request_dto, UsersRepository};
use axum::{http::StatusCode, response::Response};
use imphnen_cms::{
v1::landing::testimonials::{
testimonials_dto::{TestimonialsCreateRequestDto, TestimonialsUpdateRequestDto},
testimonials_service::TestimonialsService,
testimonials_schema::TestimonialsSchema,
},
};
use imphnen_entities::UsersSchema;
use imphnen_utils::make_thing_from_enum;
#[tokio::test]
async fn test_get_testimonial_list_service() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test testimonials
let testimonial_contents = vec![
"Testimonial content 1".to_string(),
"Testimonial content 2".to_string(),
"Testimonial content 3".to_string(),
];
for content in &testimonial_contents {
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: format!("Test User for {}", content),
email: format!("testuser{}@example.com", content.chars().take(5).collect::<String>()),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let _ = repo.query_create_testimonial(testimonial).await;
}
// Get testimonial list through service
let response = TestimonialsService::get_testimonial_list(&app_state, get_meta_request_dto(1, 10))
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Clean up
for content in testimonial_contents {
let user = UsersRepository::new(&app_state)
.query_user_by_email(format!("testuser{}@example.com", content.chars().take(5).collect::<String>()))
.await
.unwrap();
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}
#[tokio::test]
async fn test_get_testimonial_by_id_service_found() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial_content = "Test testimonial content for get by ID service test".to_string();
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: testimonial_content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Get testimonial by ID through service
let response = TestimonialsService::get_testimonial_by_id(&app_state, testimonial_id.clone())
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Verify response body contains correct data
let body_bytes = response.into_body().collect().await.unwrap().to_bytes();
let response_body: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
assert_eq!(response_body["data"]["content"].as_str().unwrap(), testimonial_content);
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_get_testimonial_by_id_service_not_found() {
let app_state = crate::get_app_state().await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Get non-existent testimonial by ID through service
let response = TestimonialsService::get_testimonial_by_id(&app_state, non_existent_id)
.await;
// Verify not found response
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_get_testimonial_by_id_service_deleted() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test User".to_string(),
email: "testuser@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for deleted test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Soft delete the testimonial
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
// Try to get deleted testimonial through service
let response = TestimonialsService::get_testimonial_by_id(&app_state, testimonial_id)
.await;
// Verify not found response (service should filter out deleted items)
assert_eq!(response.status(), StatusCode::NOT_FOUND);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_create_testimonial_service() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Test data
let testimonial_request = TestimonialsCreateRequestDto {
role: "Mentor".to_string(),
content: "Test testimonial content for service create test".to_string(),
};
// Create testimonial through service
let response = TestimonialsService::create_testimonial(
&app_state, testimonial_request.clone(), &user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::CREATED);
// Verify testimonial was created in database
let created_testimonials = repo.query_testimonial_list(get_meta_request_dto(1, 10)).await.unwrap();
assert!(created_testimonials.data.iter().any(|t| t.content == testimonial_request.content));
// Clean up
let created_testimonial = repo.query_testimonial_list(get_meta_request_dto(1, 10)).await.unwrap();
for t in created_testimonials.data {
if t.content == testimonial_request.content {
let _ = repo.query_delete_testimonial(t.id.id.to_raw()).await;
}
}
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_create_testimonial_service_invalid_data() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Test data with empty content (should fail validation)
let testimonial_request = TestimonialsCreateRequestDto {
role: "Mentor".to_string(),
content: "".to_string(), // Empty content should fail validation
};
// Create testimonial through service
let response = TestimonialsService::create_testimonial(
&app_state, testimonial_request, &user,
)
.await;
// Verify bad request response (validation error)
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_update_testimonial_service() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let original_content = "Original testimonial content for service update test".to_string();
let new_content = "Updated testimonial content for service update test".to_string();
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: original_content.clone(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Prepare update request
let update_request = TestimonialsUpdateRequestDto {
role: Some("Updated Mentor".to_string()),
content: Some(new_content.clone()),
};
// Update testimonial through service
let response = TestimonialsService::update_testimonial(
&app_state, update_request, testimonial_id.clone(), &user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Verify testimonial was updated in database
let updated_testimonial = repo
.query_testimonial_by_id(testimonial_id.clone())
.await
.unwrap();
assert_eq!(updated_testimonial.content, new_content);
assert_eq!(updated_testimonial.role, "Updated Mentor");
// Clean up
let _ = repo.query_delete_testimonial(testimonial_id).await;
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_update_testimonial_service_not_found() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Prepare update request
let update_request = TestimonialsUpdateRequestDto {
role: Some("Updated Mentor".to_string()),
content: Some("Updated content".to_string()),
};
// Update non-existent testimonial through service
let response = TestimonialsService::update_testimonial(
&app_state, update_request, non_existent_id, &user,
)
.await;
// Verify not found response
assert_eq!(response.status(), StatusCode::NOT_FOUND);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_update_testimonial_service_deleted() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for deleted update test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Soft delete the testimonial
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
// Prepare update request
let update_request = TestimonialsUpdateRequestDto {
role: Some("Updated Mentor".to_string()),
content: Some("Updated content".to_string()),
};
// Try to update deleted testimonial through service
let response = TestimonialsService::update_testimonial(
&app_state, update_request, testimonial_id, &user,
)
.await;
// Verify bad request response (should fail because it's deleted)
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_delete_testimonial_service() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for service delete test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Verify testimonial exists before deletion
let exists_before = repo.query_testimonial_by_id(testimonial_id.clone()).await.is_ok();
assert!(exists_before);
// Delete testimonial through service
let response = TestimonialsService::delete_testimonial(
&app_state, testimonial_id.clone(), &user,
)
.await;
// Verify response
assert_eq!(response.status(), StatusCode::OK);
// Verify testimonial was soft-deleted from database
let deleted_testimonial = repo.query_testimonial_by_id(testimonial_id.clone()).await;
assert!(deleted_testimonial.is_err());
// Clean up - no need since it's already soft-deleted
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_delete_testimonial_service_not_found() {
let app_state = crate::get_app_state().await;
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Use non-existent ID
let non_existent_id = "non-existent-uuid-123456789".to_string();
// Delete non-existent testimonial through service
let response = TestimonialsService::delete_testimonial(
&app_state, non_existent_id, &user,
)
.await;
// Verify bad request response
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
#[tokio::test]
async fn test_delete_testimonial_service_deleted_twice() {
let app_state = crate::get_app_state().await;
let repo = imphnen_cms::v1::landing::testimonials::testimonials_repository::TestimonialsRepository::new(&app_state);
// Create test user for authentication
let user = UsersSchema {
id: make_thing_from_enum("users", &uuid::Uuid::new_v4().to_string()),
fullname: "Test Admin".to_string(),
email: "admin@example.com".to_string(),
..Default::default()
};
let _ = UsersRepository::new(&app_state).query_create_user(user.clone()).await;
// Create test testimonial
let testimonial = TestimonialsSchema {
id: make_thing_from_enum("testimonials", &uuid::Uuid::new_v4().to_string()),
user: user.id,
role: "Mentor".to_string(),
content: "Test testimonial content for double delete test".to_string(),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
is_deleted: false,
};
let create_result = repo.query_create_testimonial(testimonial.clone()).await;
assert!(create_result.is_ok());
// Get created testimonial to get ID
let created_testimonial = repo
.query_testimonial_by_id(testimonial.id.id.to_raw())
.await
.unwrap();
let testimonial_id = created_testimonial.id.id.to_raw();
// Delete testimonial once
let _ = repo.query_delete_testimonial(testimonial_id.clone()).await;
// Try to delete again through service
let response = TestimonialsService::delete_testimonial(
&app_state, testimonial_id, &user,
)
.await;
// Verify bad request response (should fail because it's already deleted)
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// Clean up
let _ = UsersRepository::new(&app_state).query_delete_user(user.id.id.to_raw()).await;
}
}