Add Login #4

Merged
phoenix merged 21 commits from add_login into main 2026-06-10 00:13:55 -04:00
Showing only changes of commit b54d7e49c9 - Show all commits
+83
View File
@@ -85,6 +85,25 @@ pub mod requests {
"firstname": String::from(super::TEST_FIRSTNAME),
"lastname": String::from(super::TEST_LASTNAME),
});
let req = axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(super::callers::endpoints::REGISTER)
.header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::from(payload.to_string()))
.unwrap();
app.clone().oneshot(req).await
}
pub async fn login_user(
app: &axum::Router,
username: &str,
password: &str,
) -> Result<axum::response::Response, std::convert::Infallible> {
let payload = serde_json::json!({
"username": username,
"password": password,
});
let req = axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(super::callers::endpoints::LOGIN)
@@ -109,6 +128,52 @@ const TEST_USERNAME: &str = "BillyBob01";
const TEST_PASSWORD: &str = "923ndcry392qryudx328qrdy328r";
const TEST_PHONE_NUMBER: &str = "+10123456789";
async fn register_user(
app: &axum::Router,
) -> Result<textsender_models::user::User, std::io::Error> {
match requests::register_user(&app).await {
Ok(response) => {
if axum::http::StatusCode::CREATED != response.status() {
Err(std::io::Error::other(""))
} else {
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
Ok(body) => {
let parsed_body: callers::register::response::Response =
serde_json::from_slice(&body).unwrap();
Ok(parsed_body.data[0].clone())
}
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
}
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
async fn login_user(
app: &axum::Router,
username: &str,
password: &str,
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
match requests::login_user(&app, username, password).await {
Ok(response) => {
if axum::http::StatusCode::OK != response.status() {
Err(std::io::Error::other(""))
} else {
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
Ok(body) => {
let parsed_body: callers::login::response::LoginResponse =
serde_json::from_slice(&body).unwrap();
Ok(parsed_body.data[0].clone())
}
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
}
Err(err) => Err(std::io::Error::other(err.to_string())),
}
}
#[tokio::test]
async fn test_register_user() {
let tm_pool = db_mgr::get_pool().await.unwrap();
@@ -176,6 +241,24 @@ async fn test_login_user() {
}
}
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
db::init::migrations(&pool).await;
let app = init::routes().await.layer(axum::Extension(pool));
match register_user(&app).await {
Ok(user) => match login_user(&app, &user.username, TEST_PASSWORD).await {
Ok(_login_result) => {}
Err(err) => {
assert!(false, "Error: {err:?}");
}
},
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
match db_mgr::drop_database(&tm_pool, &db_name).await {
Ok(()) => {}
Err(err) => {