add test for image posting

This commit is contained in:
Alie 2024-01-26 17:32:25 +01:00
parent c011b61e23
commit c2d1b9ee37
1 changed files with 43 additions and 5 deletions

View File

@ -64,7 +64,7 @@ async fn main() -> DynResult<()> {
Ok(url) => match url { Ok(url) => match url {
Some(url) => { Some(url) => {
let mut retry: u8 = 0; 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); log::warn!("Cannot post image, retry: {}, {}", retry, err);
async_std::task::sleep(Duration::new(1, 0)).await; async_std::task::sleep(Duration::new(1, 0)).await;
retry += 1; retry += 1;
@ -188,7 +188,12 @@ fn get_next_url(config: &Config) -> DynResult<Option<String>> {
} }
} }
async fn post_image(account: &Mastodon, url: &String, config: &Config) -> DynResult<()> { async fn post_image(
account: &Mastodon,
url: &String,
config: &Config,
visibility: Visibility,
) -> DynResult<Status> {
fetch_url(url, &config.files.tempfile).await?; //.expect("Error fetching url"); fetch_url(url, &config.files.tempfile).await?; //.expect("Error fetching url");
let attachment = account let attachment = account
.media(&config.files.tempfile, Some(url.to_string())) .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"); .await?; //.expect("Attachment processing error");
let status = StatusBuilder::new() let status = StatusBuilder::new()
.media_ids(&[attachment.id]) .media_ids(&[attachment.id])
.visibility(Visibility::Unlisted) .visibility(visibility)
.sensitive(true) .sensitive(true)
.build()?; //.expect("Could not build status"); // we should retry .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); log::info!("Image status posted: {}", url);
Ok(()) Ok(status)
} }
async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> { async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
@ -301,6 +306,39 @@ mod tests {
.send() .send()
.await .await
.unwrap(); .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) assert_eq!(response.status(), StatusCode::OK)
} }