From 7188138018ed1d2ebef772b2647ca4b18f457cd6 Mon Sep 17 00:00:00 2001 From: phoenix Date: Wed, 12 Aug 2026 16:53:24 -0400 Subject: [PATCH] Refactoring tests (#257) Reviewed-on: http://git.kundeng.us/phoenix/soaricarus_api/pulls/257 --- Cargo.lock | 4 +- Cargo.toml | 2 +- src/callers/queue/song.rs | 25 ++ src/main.rs | 556 +++++++++++++++++++------------------- 4 files changed, 306 insertions(+), 281 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a9d6d28..baeb1f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1937,8 +1937,8 @@ checksum = "a4933f3f57a8e9d9da04db23fb153356ecaf00cbd14aee46279c33dc80925c37" [[package]] name = "labyrinth" -version = "0.0.2" -source = "git+ssh://git@git.kundeng.us/phoenix/labyrinth.git?tag=v0.0.2-1-84f98521c8-928#84f98521c8c98693e422db48eb0aa7c124c2586b" +version = "0.0.4" +source = "git+ssh://git@git.kundeng.us/phoenix/labyrinth.git?tag=v0.0.4#f406ed196145bb667587c13f4fd0866a1fa6e915" dependencies = [ "aws-config", "aws-sdk-s3", diff --git a/Cargo.toml b/Cargo.toml index 3120101..59259dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ utoipa-swagger-ui = { version = "9.0.2", features = ["axum"] } simeta = { git = "ssh://git@git.kundeng.us/phoenix/simeta.git", tag = "v0.6.1-main-b690995806-680" } simodels = { git = "ssh://git@git.kundeng.us/phoenix/simodels.git", tag = "v0.11.3-main-fe9d101bd0-111" } sienvy = { git = "ssh://git@git.kundeng.us/phoenix/sienvy.git", tag = "v0.8.0-main-d06c8fdf49-006" } -labyrinth = { git = "ssh://git@git.kundeng.us/phoenix/labyrinth.git", tag = "v0.0.2-1-84f98521c8-928" } +labyrinth = { git = "ssh://git@git.kundeng.us/phoenix/labyrinth.git", tag = "v0.0.4" } [dev-dependencies] common-multipart-rfc7578 = { version = "0.7.0" } diff --git a/src/callers/queue/song.rs b/src/callers/queue/song.rs index 8746da0..b0744c1 100644 --- a/src/callers/queue/song.rs +++ b/src/callers/queue/song.rs @@ -212,6 +212,31 @@ pub mod endpoint { match lr.upload(&file_path, &data).await { Ok(res) => { println!("Result: {res:?}"); + + println!("Downloading file"); + match lr.download(&file_path).await { + Ok(res) => { + if res.is_empty() { + println!("This should not be empty"); + } else { + println!("Size: {:?}", res.len()); + println!("Going to delete file"); + + match lr.delete(&file_path).await { + Ok(res) => { + println!("Result: {res:?}"); + println!("Deleted"); + } + Err(err) => { + eprintln!("Error: {err:?}"); + } + } + } + } + Err(err) => { + eprintln!("Error: {err:?}"); + } + } } Err(err) => match err { labyrinth::Error::Info(err_str) => { diff --git a/src/main.rs b/src/main.rs index af7f0a4..b78eb13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,9 +25,6 @@ async fn main() { mod tests { use std::io::Write; - use common_multipart_rfc7578::client::multipart::{ - Body as MultipartBody, Form as MultipartForm, - }; use tower::ServiceExt; use crate::db; @@ -193,269 +190,318 @@ mod tests { pub async fn song_queue_req( app: &axum::Router, - ) -> Result { + ) -> Result { // Create multipart form - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", "tests/I/track01.flac"); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::queue::endpoints::QUEUESONG) - .header(axum::http::header::CONTENT_TYPE, content_type) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - app.clone().oneshot(req).await + match run_post( + Some(ReqBody::Multipart(( + "flac".to_string(), + "tests/I/track01.flac".to_string(), + ))), + crate::callers::queue::endpoints::QUEUESONG, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn song_queue_link_req( app: &axum::Router, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::link_user_to_queued_song(song_queue_id, user_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUESONGLINKUSERID) - .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(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUESONGLINKUSERID, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_queue_req( app: &axum::Router, - ) -> Result { - let fetch_req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(crate::callers::queue::endpoints::NEXTQUEUESONG) - .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(fetch_req).await + ) -> Result { + match run_post( + None, + crate::callers::queue::endpoints::NEXTQUEUESONG, + axum::http::Method::GET, + false, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_metadata_queue_req( app: &axum::Router, id: &uuid::Uuid, - ) -> Result { + ) -> Result { let uri = format!( "{}?id={}", crate::callers::queue::endpoints::QUEUEMETADATA, 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(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn fetch_queue_data_req( app: &axum::Router, id: &uuid::Uuid, - ) -> Result { + ) -> Result { let raw_uri = String::from(crate::callers::queue::endpoints::QUEUESONGDATA); let end_index = raw_uri.len() - 4; let mut uri: String = (&raw_uri[..end_index]).to_string(); uri += &id.to_string(); - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header( - axum::http::header::CONTENT_TYPE, - simeta::detection::song::constants::mime::FLAC, - ) - .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(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn upload_coverart_queue_req( app: &axum::Router, - ) -> Result { - let mut form = MultipartForm::default(); - let _ = form.add_file( - simeta::detection::coverart::constants::JPEG_TYPE, - "tests/I/Coverart-1.jpg", - ); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::queue::endpoints::QUEUECOVERART) - .header(axum::http::header::CONTENT_TYPE, content_type) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(); - - app.clone().oneshot(req).await + ) -> Result { + match run_post( + Some(ReqBody::Multipart(( + simeta::detection::coverart::constants::JPEG_TYPE.to_string(), + "tests/I/Coverart-1.jpg".to_string(), + ))), + crate::callers::queue::endpoints::QUEUECOVERART, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn queue_metadata_req( app: &axum::Router, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::queue_metadata(&song_queue_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::queue::endpoints::QUEUEMETADATA) - .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(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUEMETADATA, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn coverart_queue_song_queue_link_req( app: &axum::Router, coverart_id: &uuid::Uuid, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::link_queued_coverart_to_queued_song( song_queue_id, coverart_id, ) .await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUECOVERARTLINK) - .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(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUECOVERARTLINK, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn create_coverart_req( app: &axum::Router, song_id: &uuid::Uuid, coverart_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::create_coverart(song_id, coverart_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::CREATECOVERART) - .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(ReqBody::Json(payload)), + crate::callers::endpoints::CREATECOVERART, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn create_song_req( app: &axum::Router, song_queue_id: &uuid::Uuid, user_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::create_song(song_queue_id, user_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::POST) - .uri(crate::callers::endpoints::CREATESONG) - .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(ReqBody::Json(payload)), + crate::callers::endpoints::CREATESONG, + axum::http::Method::POST, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn update_song_queue_status_req( app: &axum::Router, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let payload = super::payload_data::update_song_queue_status_to_ready(song_queue_id).await; - let req = axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUESONG) - .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(ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUESONG, + axum::http::Method::PATCH, + true, + ) + .await + { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } } pub async fn get_queued_coverart( app: &axum::Router, coverart_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { let uri = format!( "{}?id={}", crate::callers::queue::endpoints::QUEUECOVERART, coverart_queue_id ); - let req = axum::http::Request::builder() - .method(axum::http::Method::GET) + match run_post(None, &uri, axum::http::Method::GET, false).await { + Ok(request) => match app.clone().oneshot(request).await { + Ok(response) => Ok(response), + Err(err) => Err(axum::http::Error::from(err)), + }, + Err(err) => Err(err), + } + } + + pub enum ReqBody { + Json(serde_json::Value), + Multipart((String, String)), + } + + pub async fn run_post( + payload: Option, + uri: &str, + method: axum::http::Method, + has_body: bool, + ) -> Result, axum::http::Error> { + let mut content_type = "application/json; charset=utf-8".to_string(); + let body = if has_body { + assert_eq!( + true, + payload.is_some(), + "Has request body and payload has data" + ); + + match payload { + Some(p) => match p { + ReqBody::Json(val) => axum::body::Body::from(val.to_string()), + ReqBody::Multipart((t, p)) => { + let mut form = MultipartForm::default(); + let _ = form.add_file(t, p); + + content_type = form.content_type(); + let body = MultipartBody::from(form); + axum::body::Body::from_stream(body) + } + }, + None => { + eprintln!("This should not empty"); + axum::body::Body::empty() + } + } + } 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::CONTENT_TYPE, content_type) .header( axum::http::header::AUTHORIZATION, super::bearer_auth().await, ) - .body(axum::body::Body::empty()) - .unwrap(); - - app.clone().oneshot(req).await + .body(body) + { + Ok(t) => Ok(t), + Err(err) => Err(err), + } } } @@ -463,7 +509,7 @@ mod tests { // Flow for queueing song pub async fn queue_song_flow( app: &axum::Router, - ) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { + ) -> Result<(axum::response::Response, uuid::Uuid), axum::http::Error> { match super::request::song_queue_req(&app).await { Ok(response) => { let resp = super::util::get_resp_data::< @@ -506,6 +552,7 @@ mod tests { Err(err) => Err(err), } } + Err(err) => Err(err), } } Err(err) => Err(err), @@ -519,7 +566,7 @@ mod tests { pub async fn queue_coverart_flow( app: &axum::Router, song_queue_id: &uuid::Uuid, - ) -> Result { + ) -> Result { match super::request::upload_coverart_queue_req(&app).await { Ok(response) => { let resp = super::util::get_resp_data::< @@ -555,15 +602,17 @@ mod tests { Err(err) => Err(err), } } + Err(err) => Err(err), } } + Err(err) => Err(err), } } // Returns coverart response and song_queue_id pub async fn queue_song_and_coverart_flow( app: &axum::Router, - ) -> Result<(axum::response::Response, uuid::Uuid), std::convert::Infallible> { + ) -> Result<(axum::response::Response, uuid::Uuid), axum::http::Error> { match queue_song_flow(&app).await { Ok((song_response, user_id)) => { let resp = super::util::get_resp_data::< @@ -931,13 +980,6 @@ mod tests { } let songpath = song.song_path().unwrap(); - let mut form = MultipartForm::default(); - let _ = form.add_file("flac", &songpath); - - // Create request - let content_type = form.content_type(); - let body = MultipartBody::from(form); - let raw_uri = String::from(crate::callers::queue::endpoints::QUEUESONGUPDATE); let end_index = raw_uri.len() - 5; @@ -951,16 +993,17 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, content_type) - .header( - axum::http::header::AUTHORIZATION, - bearer_auth().await, - ) - .body(axum::body::Body::from_stream(body)) - .unwrap(), + request::run_post( + Some(request::ReqBody::Multipart(( + "flac".to_string(), + songpath, + ))), + &uri, + axum::http::Method::PATCH, + true, + ) + .await + .unwrap(), ) .await { @@ -1441,16 +1484,14 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(uri) - .header(axum::http::header::CONTENT_TYPE, "image/jpeg") - .header( - axum::http::header::AUTHORIZATION, - bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(), + request::run_post( + None, + &uri, + axum::http::Method::GET, + false, + ) + .await + .unwrap(), ) .await { @@ -1648,6 +1689,9 @@ mod tests { } } } + Err(err) => { + assert!(false, "Error: {:?}", err); + } } } Err(err) => { @@ -1715,13 +1759,14 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUESONGDATAWIPE) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header(axum::http::header::AUTHORIZATION, bearer_auth().await) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), + request::run_post( + Some(request::ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUESONGDATAWIPE, + axum::http::Method::PATCH, + true, + ) + .await + .unwrap(), ) .await { @@ -1833,19 +1878,14 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::PATCH) - .uri(crate::callers::queue::endpoints::QUEUECOVERARTDATAWIPE) - .header( - axum::http::header::CONTENT_TYPE, - "application/json", - ) - .header( - axum::http::header::AUTHORIZATION, - bearer_auth().await, - ) - .body(axum::body::Body::from(payload.to_string())) - .unwrap(), + request::run_post( + Some(request::ReqBody::Json(payload)), + crate::callers::queue::endpoints::QUEUECOVERARTDATAWIPE, + axum::http::Method::PATCH, + true, + ) + .await + .unwrap(), ) .await { @@ -1913,15 +1953,8 @@ mod tests { match app .clone() .oneshot( - 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()) + super::request::run_post(None, &uri, axum::http::Method::GET, false) + .await .unwrap(), ) .await @@ -1970,15 +2003,8 @@ mod tests { match app .clone() .oneshot( - 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()) + super::request::run_post(None, &uri, axum::http::Method::GET, false) + .await .unwrap(), ) .await @@ -2059,14 +2085,8 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(&uri) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) + super::request::run_post(None, &uri, axum::http::Method::GET, false) + .await .unwrap(), ) .await @@ -2119,14 +2139,8 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(&uri) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) + super::request::run_post(None, &uri, axum::http::Method::GET, false) + .await .unwrap(), ) .await @@ -2181,14 +2195,8 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(&uri) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) + super::request::run_post(None, &uri, axum::http::Method::GET, false) + .await .unwrap(), ) .await @@ -2308,14 +2316,8 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::DELETE) - .uri(&uri) - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) + super::request::run_post(None, &uri, axum::http::Method::DELETE, false) + .await .unwrap(), ) .await @@ -2380,16 +2382,14 @@ mod tests { match app .clone() .oneshot( - axum::http::Request::builder() - .method(axum::http::Method::GET) - .uri(crate::callers::endpoints::GETALLSONGS) - .header(axum::http::header::CONTENT_TYPE, "application/json") - .header( - axum::http::header::AUTHORIZATION, - super::bearer_auth().await, - ) - .body(axum::body::Body::empty()) - .unwrap(), + super::request::run_post( + None, + crate::callers::endpoints::GETALLSONGS, + axum::http::Method::GET, + false, + ) + .await + .unwrap(), ) .await {