From 0f7f9fb8031a5e54647e58cff896fb4ad2336b74 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 17 Jun 2026 15:40:00 -0400 Subject: [PATCH] Code changes --- tests/test.rs | 153 ++++++++++++++++++++++++++------------------------ 1 file changed, 81 insertions(+), 72 deletions(-) diff --git a/tests/test.rs b/tests/test.rs index 942598c..9056a62 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -129,8 +129,22 @@ pub fn token_fields() -> (String, String, String) { ) } +/// Test User Id pub const TEST_USER_ID: uuid::Uuid = uuid::uuid!("cc938368-615a-4694-b2ca-6e122fa31c52"); +/// Test contact phone number +pub const TEST_CONTACT_PHONE_NUMBER: &str = "+10123456789"; + +/// Test contact updated firstname +pub const TEST_CONTACT_UPDATED_FIRSTNAME: &str = "Johnny"; +/// Test contact updated lastname +pub const TEST_CONTACT_UPDATED_LASTNAME: &str = "CASH"; +/// Test contact updated nickname +pub const TEST_CONTACT_UPDATED_NICKNAME: &str = "The Man in Black"; + +/// Test message content +pub const TEST_MESSAGE_CONTENT: &str = "The wind cries mary"; + pub async fn test_token() -> Result { let key: String = textsender_models::envy::environment::get_secret_main_key() .await @@ -209,21 +223,6 @@ mod request { }, Err(err) => Err(err), } - - /* - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(); - - app.clone().oneshot(req).await - */ } pub async fn update_contact_names( @@ -232,7 +231,7 @@ mod request { firstname: Option<&str>, lastname: Option<&str>, nickname: Option<&str>, - ) -> Result { + ) -> Result { let payload = serde_json::json!({ "firstname": firstname, "lastname": lastname, @@ -241,67 +240,54 @@ mod request { "id": id }); - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(textsender_api::caller::endpoints::UPDATE_CONTACT_NAME) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - app.clone().oneshot(req).await + match run_post(Some(&payload), textsender_api::caller::endpoints::UPDATE_CONTACT_NAME, axum::http::Method::PATCH, true).await { + Ok(req) => match app.clone().oneshot(req).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } - /* pub mod message { + use tower::ServiceExt; + pub async fn create_message( app: &axum::Router, - ) -> Result { + ) -> Result { let payload = serde_json::json!({ - "content": super::TEST_MESSAGE_CONTENT, - "user_id": super::TEST_USER_ID, + "content": super::super::TEST_MESSAGE_CONTENT, + "user_id": super::super::TEST_USER_ID, }); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(textsender_api::caller::endpoints::ADD_MESSAGE) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(); - app.clone().oneshot(req).await + match super::run_post(Some(&payload), textsender_api::caller::endpoints::ADD_MESSAGE, axum::http::Method::POST, true).await { + Ok(req) => match app.clone().oneshot(req).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn get_contact( app: &axum::Router, id: &uuid::Uuid, - ) -> Result { + ) -> Result { let uri = format!( "{}?id={}", textsender_api::caller::endpoints::GET_CONTACT, id ); - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(); - - app.clone().oneshot(req).await + match super::run_post(None, &uri, axum::http::Method::GET, false).await { + Ok(req) => match app.clone().oneshot(req).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } } - */ pub async fn run_post( payload: Option<&serde_json::Value>, @@ -335,18 +321,6 @@ mod request { } } -/// Test contact phone number -pub const TEST_CONTACT_PHONE_NUMBER: &str = "+10123456789"; - -/// Test contact updated firstname -pub const TEST_CONTACT_UPDATED_FIRSTNAME: &str = "Johnny"; -/// Test contact updated lastname -pub const TEST_CONTACT_UPDATED_LASTNAME: &str = "CASH"; -/// Test contact updated nickname -pub const TEST_CONTACT_UPDATED_NICKNAME: &str = "The Man in Black"; - -/// Test message content -pub const TEST_MESSAGE_CONTENT: &str = "The wind cries mary"; #[tokio::test] async fn test_create_contact() { @@ -530,10 +504,45 @@ async fn test_update_contact_names() { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } +#[tokio::test] +async fn test_create_message() { + let tm_pool = db_mgr::get_pool().await.unwrap(); + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; + + let app = init::app(pool).await; + + // Send request + match request::message::create_message(&app).await { + Ok(response) => { + let resp = util::get_resp_data::< + textsender_api::caller::message::response::AddMessageResponse, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let message = &resp.data[0]; + assert_eq!(TEST_MESSAGE_CONTENT, message.content, "The content of the created message should match"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; +} + /* #[tokio::test] -async fn test_ - -#[tokio::test] -async fn test_ +async fn test_get_message() */