format integration_tests

This commit is contained in:
Mika 2025-06-17 08:53:23 +02:00
parent 49d27fd8fa
commit 9828ebf86a

View file

@ -1,9 +1,9 @@
use actix_web::{test, web, App}; use actix_web::{App, test, web};
use backend::{controller, Database, build_database_url}; use backend::{Database, build_database_url, controller};
use migration::{Migrator, MigratorTrait}; use migration::{Migrator, MigratorTrait};
use serde_json::json; use serde_json::json;
use serial_test::serial; use serial_test::serial;
use testcontainers::{runners::AsyncRunner, ContainerAsync, ImageExt}; use testcontainers::{ContainerAsync, ImageExt, runners::AsyncRunner};
use testcontainers_modules::{postgres::Postgres, redis::Redis}; use testcontainers_modules::{postgres::Postgres, redis::Redis};
async fn setup_test_environment() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Database) { async fn setup_test_environment() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Database) {
@ -57,8 +57,9 @@ mod tests {
let app = test::init_service( let app = test::init_service(
App::new() App::new()
.app_data(web::Data::new(database.clone())) .app_data(web::Data::new(database.clone()))
.service(web::scope("/api/v1").configure(controller::register_controllers)) .service(web::scope("/api/v1").configure(controller::register_controllers)),
).await; )
.await;
// Test creating a user // Test creating a user
let create_user_payload = json!({ let create_user_payload = json!({
@ -78,17 +79,23 @@ mod tests {
// Log response for debugging // Log response for debugging
let status = resp.status(); let status = resp.status();
let body = test::read_body(resp).await; let body = test::read_body(resp).await;
println!("Create user response: {} - {}", status, String::from_utf8_lossy(&body)); println!(
"Create user response: {} - {}",
status,
String::from_utf8_lossy(&body)
);
if status != StatusCode::CREATED { if status != StatusCode::CREATED {
// Try to get users list to see what endpoints are available // Try to get users list to see what endpoints are available
let req = test::TestRequest::get() let req = test::TestRequest::get().uri("/api/v1/user").to_request();
.uri("/api/v1/user")
.to_request();
let resp = test::call_service(&app, req).await; let resp = test::call_service(&app, req).await;
let resp_status = resp.status(); let resp_status = resp.status();
let body = test::read_body(resp).await; let body = test::read_body(resp).await;
println!("Get users response: {} - {}", resp_status, String::from_utf8_lossy(&body)); println!(
"Get users response: {} - {}",
resp_status,
String::from_utf8_lossy(&body)
);
} }
// For now, just verify the API is responding // For now, just verify the API is responding
@ -103,8 +110,9 @@ mod tests {
let app = test::init_service( let app = test::init_service(
App::new() App::new()
.app_data(web::Data::new(database.clone())) .app_data(web::Data::new(database.clone()))
.service(web::scope("/api/v1").configure(controller::register_controllers)) .service(web::scope("/api/v1").configure(controller::register_controllers)),
).await; )
.await;
// Test various endpoints to ensure they respond // Test various endpoints to ensure they respond
let endpoints = vec![ let endpoints = vec![
@ -116,9 +124,7 @@ mod tests {
]; ];
for endpoint in endpoints { for endpoint in endpoints {
let req = test::TestRequest::get() let req = test::TestRequest::get().uri(endpoint).to_request();
.uri(endpoint)
.to_request();
let resp = test::call_service(&app, req).await; let resp = test::call_service(&app, req).await;
let status = resp.status(); let status = resp.status();
@ -126,7 +132,12 @@ mod tests {
println!("Endpoint {} responded with status: {}", endpoint, status); println!("Endpoint {} responded with status: {}", endpoint, status);
// Verify endpoint is reachable (not 404) // Verify endpoint is reachable (not 404)
assert_ne!(status, StatusCode::NOT_FOUND, "Endpoint {} should exist", endpoint); assert_ne!(
status,
StatusCode::NOT_FOUND,
"Endpoint {} should exist",
endpoint
);
} }
} }
@ -137,7 +148,10 @@ mod tests {
// Test that we can connect to the database // Test that we can connect to the database
let connection = database.connection(); let connection = database.connection();
assert!(connection.ping().await.is_ok(), "Database should be reachable"); assert!(
connection.ping().await.is_ok(),
"Database should be reachable"
);
} }
#[actix_web::test] #[actix_web::test]
@ -148,8 +162,9 @@ mod tests {
let app = test::init_service( let app = test::init_service(
App::new() App::new()
.app_data(web::Data::new(database.clone())) .app_data(web::Data::new(database.clone()))
.service(web::scope("/api/v1").configure(controller::register_controllers)) .service(web::scope("/api/v1").configure(controller::register_controllers)),
).await; )
.await;
// Test non-existent endpoints // Test non-existent endpoints
let invalid_endpoints = vec![ let invalid_endpoints = vec![
@ -159,12 +174,15 @@ mod tests {
]; ];
for endpoint in invalid_endpoints { for endpoint in invalid_endpoints {
let req = test::TestRequest::get() let req = test::TestRequest::get().uri(endpoint).to_request();
.uri(endpoint)
.to_request();
let resp = test::call_service(&app, req).await; let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND, "Invalid endpoint {} should return 404", endpoint); assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"Invalid endpoint {} should return 404",
endpoint
);
} }
} }
} }