Adding code for test
This commit is contained in:
+118
@@ -212,11 +212,46 @@ mod request {
|
|||||||
|
|
||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn update_contact_names(
|
||||||
|
app: &axum::Router,
|
||||||
|
id: &uuid::Uuid,
|
||||||
|
firstname: Option<&str>,
|
||||||
|
lastname: Option<&str>,
|
||||||
|
nickname: Option<&str>
|
||||||
|
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"firstname": firstname,
|
||||||
|
"lastname": lastname,
|
||||||
|
"nickname": nickname,
|
||||||
|
"phone_number": super::TEST_CONTACT_PHONE_NUMBER,
|
||||||
|
"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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test contact phone number
|
/// Test contact phone number
|
||||||
pub const TEST_CONTACT_PHONE_NUMBER: &str = "+10123456789";
|
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";
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_create_contact() {
|
async fn test_create_contact() {
|
||||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
@@ -307,3 +342,86 @@ async fn test_get_contact() {
|
|||||||
|
|
||||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_update_contact_names() {
|
||||||
|
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;
|
||||||
|
|
||||||
|
let mut opt_contact: Option<textsender_models::contact::Contact> = None;
|
||||||
|
|
||||||
|
// 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"
|
||||||
|
);
|
||||||
|
opt_contact = Some(resp.data[0].clone())
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {:?}", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match opt_contact {
|
||||||
|
Some(contact) => {
|
||||||
|
let new_firstname = Some(TEST_CONTACT_UPDATED_FIRSTNAME);
|
||||||
|
let new_lastname = Some(TEST_CONTACT_UPDATED_LASTNAME);
|
||||||
|
let new_nickname = Some(TEST_CONTACT_UPDATED_NICKNAME);
|
||||||
|
|
||||||
|
let contact_id = contact.id.unwrap();
|
||||||
|
match request::update_contact_names(&app, &contact_id, new_firstname, new_lastname, new_nickname).await {
|
||||||
|
Ok(response) => {
|
||||||
|
let resp = util::get_resp_data::<
|
||||||
|
textsender_api::caller::contact::response::UpdateContactNamesResponse,
|
||||||
|
>(response)
|
||||||
|
.await;
|
||||||
|
assert_eq!(false, resp.data.is_empty(), "Error updating Contact names");
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
assert!(false, "Contact not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user