Get message endpoint (#14)
Reviewed-on: phoenix/textsender_api#14
This commit was merged in pull request #14.
This commit is contained in:
+226
-42
@@ -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<String, josekit::JoseError> {
|
||||
let key: String = textsender_models::envy::environment::get_secret_main_key()
|
||||
.await
|
||||
@@ -170,47 +184,45 @@ mod request {
|
||||
|
||||
pub async fn create_contact(
|
||||
app: &axum::Router,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let payload = serde_json::json!({
|
||||
"phone_number": super::TEST_CONTACT_PHONE_NUMBER,
|
||||
"user_id": super::TEST_USER_ID,
|
||||
});
|
||||
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(textsender_api::caller::endpoints::ADD_CONTACT)
|
||||
.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::ADD_CONTACT,
|
||||
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<axum::response::Response, std::convert::Infallible> {
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
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 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 update_contact_names(
|
||||
@@ -219,7 +231,7 @@ mod request {
|
||||
firstname: Option<&str>,
|
||||
lastname: Option<&str>,
|
||||
nickname: Option<&str>,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let payload = serde_json::json!({
|
||||
"firstname": firstname,
|
||||
"lastname": lastname,
|
||||
@@ -228,30 +240,101 @@ mod request {
|
||||
"id": id
|
||||
});
|
||||
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::PATCH)
|
||||
.uri(textsender_api::caller::endpoints::UPDATE_CONTACT_NAME)
|
||||
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<axum::response::Response, axum::http::Error> {
|
||||
let payload = serde_json::json!({
|
||||
"content": super::super::TEST_MESSAGE_CONTENT,
|
||||
"user_id": super::super::TEST_USER_ID,
|
||||
});
|
||||
|
||||
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_message(
|
||||
app: &axum::Router,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<axum::response::Response, axum::http::Error> {
|
||||
let uri = format!(
|
||||
"{}?id={}",
|
||||
textsender_api::caller::endpoints::GET_MESSAGE,
|
||||
id
|
||||
);
|
||||
|
||||
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>,
|
||||
uri: &str,
|
||||
method: axum::http::Method,
|
||||
has_body: bool,
|
||||
) -> Result<axum::http::Request<axum::body::Body>, axum::http::Error> {
|
||||
let body = if has_body {
|
||||
assert_eq!(
|
||||
true,
|
||||
payload.is_some(),
|
||||
"Has request body and payload has data"
|
||||
);
|
||||
axum::body::Body::from(payload.unwrap().to_string())
|
||||
} else {
|
||||
axum::body::Body::empty()
|
||||
};
|
||||
match axum::http::Request::builder()
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.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
|
||||
.body(body)
|
||||
{
|
||||
Ok(t) => Ok(t),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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";
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_contact() {
|
||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||
@@ -433,3 +516,104 @@ 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_get_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;
|
||||
let mut message_result: Option<textsender_models::message::Message> = None;
|
||||
|
||||
// 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"
|
||||
);
|
||||
message_result = Some(message.clone());
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(true, message_result.is_some());
|
||||
|
||||
let message = message_result.unwrap();
|
||||
let message_id = message.id.unwrap();
|
||||
match request::message::get_message(&app, &message_id).await {
|
||||
Ok(response) => {
|
||||
let resp = util::get_resp_data::<
|
||||
textsender_api::caller::message::response::GetMessageResponse,
|
||||
>(response)
|
||||
.await;
|
||||
assert_eq!(false, resp.data.is_empty(), "Should not be empty");
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user