Added test for register endpoint with transactions
This commit is contained in:
137
src/main.rs
137
src/main.rs
@@ -1,8 +1,3 @@
|
|||||||
use axum::{
|
|
||||||
Router,
|
|
||||||
routing::{get, post},
|
|
||||||
};
|
|
||||||
|
|
||||||
use icarus_auth::callers;
|
use icarus_auth::callers;
|
||||||
use icarus_auth::config;
|
use icarus_auth::config;
|
||||||
|
|
||||||
@@ -11,7 +6,7 @@ async fn main() {
|
|||||||
// initialize tracing
|
// initialize tracing
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let app = app().await;
|
let app = init::app().await;
|
||||||
|
|
||||||
// run our app with hyper, listening globally on port 3000
|
// run our app with hyper, listening globally on port 3000
|
||||||
let url = config::get_full();
|
let url = config::get_full();
|
||||||
@@ -19,30 +14,46 @@ async fn main() {
|
|||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn routes() -> Router {
|
mod db {
|
||||||
// build our application with a route
|
pub async fn migrations(pool: &sqlx::PgPool) {
|
||||||
Router::new()
|
// Run migrations using the sqlx::migrate! macro
|
||||||
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
// Assumes your migrations are in a ./migrations folder relative to Cargo.toml
|
||||||
.route(callers::endpoints::ROOT, get(callers::common::root))
|
sqlx::migrate!("./migrations")
|
||||||
.route(
|
.run(pool)
|
||||||
callers::endpoints::REGISTER,
|
.await
|
||||||
post(callers::register::register_user),
|
.expect("Failed to run migrations on testcontainer DB");
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn app() -> Router {
|
mod init {
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
use axum::{
|
||||||
.await
|
Router,
|
||||||
.expect("Failed to create pool");
|
routing::{get, post},
|
||||||
|
};
|
||||||
|
|
||||||
// Run migrations using the sqlx::migrate! macro
|
use crate::callers;
|
||||||
// Assumes your migrations are in a ./migrations folder relative to Cargo.toml
|
use crate::db;
|
||||||
sqlx::migrate!("./migrations")
|
|
||||||
.run(&pool)
|
|
||||||
.await
|
|
||||||
.expect("Failed to run migrations on testcontainer DB");
|
|
||||||
|
|
||||||
routes().await.layer(axum::Extension(pool))
|
pub async fn routes() -> Router {
|
||||||
|
// build our application with a route
|
||||||
|
Router::new()
|
||||||
|
.route(callers::endpoints::DBTEST, get(callers::common::db_ping))
|
||||||
|
.route(callers::endpoints::ROOT, get(callers::common::root))
|
||||||
|
.route(
|
||||||
|
callers::endpoints::REGISTER,
|
||||||
|
post(callers::register::register_user),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn app() -> Router {
|
||||||
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create pool");
|
||||||
|
|
||||||
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
|
routes().await.layer(axum::Extension(pool))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -53,11 +64,13 @@ mod tests {
|
|||||||
http::{Request, StatusCode},
|
http::{Request, StatusCode},
|
||||||
};
|
};
|
||||||
use http_body_util::BodyExt;
|
use http_body_util::BodyExt;
|
||||||
|
// use reqwest;
|
||||||
|
use serde_json::json;
|
||||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn hello_world() {
|
async fn test_hello_world() {
|
||||||
let app = app().await;
|
let app = init::app().await;
|
||||||
|
|
||||||
// `Router` implements `tower::Service<Request<Body>>` so we can
|
// `Router` implements `tower::Service<Request<Body>>` so we can
|
||||||
// call it like any tower service, no need to run an HTTP server.
|
// call it like any tower service, no need to run an HTTP server.
|
||||||
@@ -76,4 +89,72 @@ mod tests {
|
|||||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||||
assert_eq!(&body[..], b"Hello, World!");
|
assert_eq!(&body[..], b"Hello, World!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_register_user() {
|
||||||
|
let pool = icarus_auth::db_pool::create_pool()
|
||||||
|
.await
|
||||||
|
.expect("Failed to create pool");
|
||||||
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
|
let usr = icarus_auth::models::common::CreateUser {
|
||||||
|
username: String::from("somethingsss"),
|
||||||
|
password: String::from("Raindown!"),
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let url = icarus_auth::callers::endpoints::REGISTER;
|
||||||
|
let response = app.oneshot(
|
||||||
|
println!("Errr")
|
||||||
|
)
|
||||||
|
.await.unwrap();
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
match client.post(url).json(&usr).send().await {
|
||||||
|
Ok(response) => {}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
let payload = json!({
|
||||||
|
"username": usr.username,
|
||||||
|
"password": usr.password,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert!(true, "Info: {:?}", payload);
|
||||||
|
println!("Info: {:?}", payload);
|
||||||
|
|
||||||
|
let response = app
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(axum::http::Method::POST)
|
||||||
|
.uri(callers::endpoints::REGISTER)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
// .body(Body::new(axum::Json(usr)))
|
||||||
|
// .body(Body::empty())
|
||||||
|
.body(Body::from(payload.to_string()))
|
||||||
|
// .body(Body::from("{\"username\": \"sssss\",\"theanswer\"}"))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
// .unwrap();
|
||||||
|
|
||||||
|
match response {
|
||||||
|
Ok(resp) => {
|
||||||
|
assert_eq!(resp.status(), StatusCode::CREATED, "Message: {:?}", resp);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err.to_string());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
tx.rollback().await.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct CreateUser {
|
pub struct CreateUser {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
|
Reference in New Issue
Block a user