diff --git a/tests/test.rs b/tests/test.rs index 257fe59..23592f9 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -188,6 +188,30 @@ mod request { .unwrap(); app.clone().oneshot(req).await } + + pub async fn get_contact( + app: &axum::Router, + id: &uuid::Uuid, + ) -> 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 + } } /// Test contact phone number @@ -228,3 +252,58 @@ async fn test_create_contact() { let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } + +#[tokio::test] +async fn test_get_contact() { + 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::create_contact(&app).await { + Ok(response) => { + let resp = util::get_resp_data::< + textsender_api::caller::contact::response::AddContactResponse, + >(response) + .await; + assert_eq!(false, resp.data.is_empty(), "Should not be empty"); + let contact = &resp.data[0]; + assert_eq!(true, contact.id.is_some(), "Missing Contact Id"); + let id = contact.id.unwrap(); + match request::get_contact(&app, &id).await { + Ok(response) => { + let resp = util::get_resp_data::< + textsender_api::caller::contact::response::GetContactResponse, + >(response) + .await; + assert_eq!( + false, + resp.data.is_empty(), + "At least Contact record should have been returned" + ); + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }; + + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; +}