format integration_tests
This commit is contained in:
parent
49d27fd8fa
commit
9828ebf86a
1 changed files with 70 additions and 52 deletions
|
@ -1,9 +1,9 @@
|
|||
use actix_web::{test, web, App};
|
||||
use backend::{controller, Database, build_database_url};
|
||||
use actix_web::{App, test, web};
|
||||
use backend::{Database, build_database_url, controller};
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use serde_json::json;
|
||||
use serial_test::serial;
|
||||
use testcontainers::{runners::AsyncRunner, ContainerAsync, ImageExt};
|
||||
use testcontainers::{ContainerAsync, ImageExt, runners::AsyncRunner};
|
||||
use testcontainers_modules::{postgres::Postgres, redis::Redis};
|
||||
|
||||
async fn setup_test_environment() -> (ContainerAsync<Postgres>, ContainerAsync<Redis>, Database) {
|
||||
|
@ -57,8 +57,9 @@ mod tests {
|
|||
let app = test::init_service(
|
||||
App::new()
|
||||
.app_data(web::Data::new(database.clone()))
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers))
|
||||
).await;
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers)),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Test creating a user
|
||||
let create_user_payload = json!({
|
||||
|
@ -78,17 +79,23 @@ mod tests {
|
|||
// Log response for debugging
|
||||
let status = resp.status();
|
||||
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 {
|
||||
// Try to get users list to see what endpoints are available
|
||||
let req = test::TestRequest::get()
|
||||
.uri("/api/v1/user")
|
||||
.to_request();
|
||||
let req = test::TestRequest::get().uri("/api/v1/user").to_request();
|
||||
let resp = test::call_service(&app, req).await;
|
||||
let resp_status = resp.status();
|
||||
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
|
||||
|
@ -103,8 +110,9 @@ mod tests {
|
|||
let app = test::init_service(
|
||||
App::new()
|
||||
.app_data(web::Data::new(database.clone()))
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers))
|
||||
).await;
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers)),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Test various endpoints to ensure they respond
|
||||
let endpoints = vec![
|
||||
|
@ -116,9 +124,7 @@ mod tests {
|
|||
];
|
||||
|
||||
for endpoint in endpoints {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(endpoint)
|
||||
.to_request();
|
||||
let req = test::TestRequest::get().uri(endpoint).to_request();
|
||||
|
||||
let resp = test::call_service(&app, req).await;
|
||||
let status = resp.status();
|
||||
|
@ -126,7 +132,12 @@ mod tests {
|
|||
println!("Endpoint {} responded with status: {}", endpoint, status);
|
||||
|
||||
// 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
|
||||
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]
|
||||
|
@ -148,8 +162,9 @@ mod tests {
|
|||
let app = test::init_service(
|
||||
App::new()
|
||||
.app_data(web::Data::new(database.clone()))
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers))
|
||||
).await;
|
||||
.service(web::scope("/api/v1").configure(controller::register_controllers)),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Test non-existent endpoints
|
||||
let invalid_endpoints = vec![
|
||||
|
@ -159,12 +174,15 @@ mod tests {
|
|||
];
|
||||
|
||||
for endpoint in invalid_endpoints {
|
||||
let req = test::TestRequest::get()
|
||||
.uri(endpoint)
|
||||
.to_request();
|
||||
let req = test::TestRequest::get().uri(endpoint).to_request();
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue