From c2d1b9ee370cf4988e1b1589d1d1b9b7d32e2af2 Mon Sep 17 00:00:00 2001 From: Alie Date: Fri, 26 Jan 2024 17:32:25 +0100 Subject: [PATCH] add test for image posting --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e807cf5..759c032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ async fn main() -> DynResult<()> { Ok(url) => match url { Some(url) => { let mut retry: u8 = 0; - while let Err(err) = post_image(&mastodon, &url, &config).await { + while let Err(err) = post_image(&mastodon, &url, &config, Visibility::Unlisted).await { log::warn!("Cannot post image, retry: {}, {}", retry, err); async_std::task::sleep(Duration::new(1, 0)).await; retry += 1; @@ -188,7 +188,12 @@ fn get_next_url(config: &Config) -> DynResult> { } } -async fn post_image(account: &Mastodon, url: &String, config: &Config) -> DynResult<()> { +async fn post_image( + account: &Mastodon, + url: &String, + config: &Config, + visibility: Visibility, +) -> DynResult { fetch_url(url, &config.files.tempfile).await?; //.expect("Error fetching url"); let attachment = account .media(&config.files.tempfile, Some(url.to_string())) @@ -198,12 +203,12 @@ async fn post_image(account: &Mastodon, url: &String, config: &Config) -> DynRes .await?; //.expect("Attachment processing error"); let status = StatusBuilder::new() .media_ids(&[attachment.id]) - .visibility(Visibility::Unlisted) + .visibility(visibility) .sensitive(true) .build()?; //.expect("Could not build status"); // we should retry - account.new_status(status).await?; //.expect("Error generating status"); // we should retry or delete last url in posted + let status = account.new_status(status).await?; //.expect("Error generating status"); // we should retry or delete last url in posted log::info!("Image status posted: {}", url); - Ok(()) + Ok(status) } async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> { @@ -301,6 +306,39 @@ mod tests { .send() .await .unwrap(); + account.delete_status(&status.id).await.unwrap(); + + assert_eq!(response.status(), StatusCode::OK) + } + + #[tokio::test] + async fn post_image_works() { + let client = reqwest::Client::new(); + let config = get_config(); + let account = get_account(&config).await; + + let status = post_image( + &account, + &"https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b".to_string(), + &config, + Visibility::Direct, + ) + .await + .unwrap(); + + let response = account.get_status(&status.id).await.unwrap(); + + + account.delete_status(&status.id).await.unwrap(); + let attachment = &response.media_attachments[0]; + + + let response = client + .get(attachment.url.clone().unwrap()) + .bearer_auth(dbg!(&account.data.token)) + .send() + .await + .unwrap(); assert_eq!(response.status(), StatusCode::OK) }