Added test
This commit is contained in:
+124
-10
@@ -1,6 +1,3 @@
|
|||||||
// use std::io::Write;
|
|
||||||
|
|
||||||
// use common_multipart_rfc7578::client::multipart::{Body as MultipartBody, Form as MultipartForm};
|
|
||||||
use textsender_api::db;
|
use textsender_api::db;
|
||||||
|
|
||||||
mod db_mgr {
|
mod db_mgr {
|
||||||
@@ -178,6 +175,19 @@ pub async fn test_token() -> Result<String, josekit::JoseError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn test_valid_schedulable_time() -> time::OffsetDateTime {
|
||||||
|
let now = time::OffsetDateTime::now_utc();
|
||||||
|
now + std::time::Duration::from_secs(60 * 60)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn convert_time_to_iso(time: time::OffsetDateTime) -> Result<String, time::error::Format> {
|
||||||
|
use time::format_description::well_known::Iso8601;
|
||||||
|
match time.format(&Iso8601::DEFAULT) {
|
||||||
|
Ok(converted) => Ok(converted),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn bearer_auth() -> String {
|
pub async fn bearer_auth() -> String {
|
||||||
let token = match test_token().await {
|
let token = match test_token().await {
|
||||||
Ok(access_token) => access_token,
|
Ok(access_token) => access_token,
|
||||||
@@ -191,9 +201,6 @@ pub async fn bearer_auth() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod request {
|
mod request {
|
||||||
// use common_multipart_rfc7578::client::multipart::{
|
|
||||||
// Body as MultipartBody, Form as MultipartForm,
|
|
||||||
// };
|
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
pub async fn create_contact(
|
pub async fn create_contact(
|
||||||
@@ -317,15 +324,13 @@ mod request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod scheduling {
|
pub mod scheduling {
|
||||||
use time::format_description::well_known::Iso8601;
|
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
pub async fn create_scheduled_message(
|
pub async fn create_scheduled_message(
|
||||||
app: &axum::Router,
|
app: &axum::Router,
|
||||||
) -> Result<axum::response::Response, axum::http::Error> {
|
) -> Result<axum::response::Response, axum::http::Error> {
|
||||||
let now = time::OffsetDateTime::now_utc();
|
let scheduled = super::super::super::test_valid_schedulable_time();
|
||||||
let scheduled = now + std::time::Duration::from_secs(60 * 60);
|
let scheduled = match super::super::super::convert_time_to_iso(scheduled) {
|
||||||
let scheduled = match scheduled.format(&Iso8601::DEFAULT) {
|
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {err:?}");
|
assert!(false, "Error: {err:?}");
|
||||||
@@ -373,6 +378,34 @@ mod request {
|
|||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_scheduled_message_event(
|
||||||
|
app: &axum::Router,
|
||||||
|
contact_id: &uuid::Uuid,
|
||||||
|
message_id: &uuid::Uuid,
|
||||||
|
scheduled_message_id: &uuid::Uuid,
|
||||||
|
) -> Result<axum::response::Response, axum::http::Error> {
|
||||||
|
let payload = serde_json::json!({
|
||||||
|
"contact_id": contact_id,
|
||||||
|
"message_id": message_id,
|
||||||
|
"scheduled_message_id": scheduled_message_id
|
||||||
|
});
|
||||||
|
|
||||||
|
match super::super::run_post(
|
||||||
|
Some(&payload),
|
||||||
|
textsender_api::caller::endpoints::CREATE_SCHEDULED_MESSAGE_EVENT,
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -687,3 +720,84 @@ async fn test_get_scheduled_message() {
|
|||||||
|
|
||||||
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_create_scheduled_message() {
|
||||||
|
let (tm_pool, db_name, pool) = db_mgr::get_database_ready().await;
|
||||||
|
let app = init::app(pool).await;
|
||||||
|
|
||||||
|
let mut contact_id: uuid::Uuid = uuid::Uuid::nil();
|
||||||
|
let mut message_id: uuid::Uuid = uuid::Uuid::nil();
|
||||||
|
let mut scheduled_message_id: uuid::Uuid = uuid::Uuid::nil();
|
||||||
|
|
||||||
|
use textsender_api::repo;
|
||||||
|
|
||||||
|
let mut contact = textsender_models::contact::Contact {
|
||||||
|
phone_number: String::from(TEST_CONTACT_PHONE_NUMBER),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
match repo::contact::insert(&tm_pool, &contact).await {
|
||||||
|
Ok(id) => {
|
||||||
|
contact.id = Some(id);
|
||||||
|
contact_id = id;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut message = textsender_models::message::Message {
|
||||||
|
content: String::from(TEST_MESSAGE_CONTENT),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
match repo::message::insert(&tm_pool, &message, &TEST_USER_ID).await {
|
||||||
|
Ok(id) => {
|
||||||
|
message.id = Some(id);
|
||||||
|
message_id = id;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let scheduled_time = test_valid_schedulable_time();
|
||||||
|
let mut scheduled_message = textsender_models::message::scheduling::ScheduledMessage {
|
||||||
|
scheduled: Some(scheduled_time),
|
||||||
|
status: String::from(textsender_models::message::scheduling::PENDING),
|
||||||
|
user_id: TEST_USER_ID,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
match repo::message::scheduling::insert(&tm_pool, &scheduled_message, &TEST_USER_ID).await {
|
||||||
|
Ok((id, created)) => {
|
||||||
|
scheduled_message.id = id;
|
||||||
|
scheduled_message_id = id;
|
||||||
|
scheduled_message.created = Some(created);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match request::message::scheduling::create_scheduled_message_event(
|
||||||
|
&app,
|
||||||
|
&contact_id,
|
||||||
|
&message_id,
|
||||||
|
&scheduled_message_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
let resp = util::get_resp_data::<
|
||||||
|
textsender_api::caller::message::scheduling::response::GetScheduledMessageResponse,
|
||||||
|
>(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