pict-rs/tests/background.rs
asonix a71812930e Add tests with system deps
These new tests each spin up their own instance of pict-rs. This allows
for configuring pict-rs differently depending on the test that needs to
be run. It also opens the possibility of writing tests with object
storage and postgres requirements in the future.

So far there's pretty minimal test coverage. Just uploading,
downloading, deleting, and checking validation errors for images and
animations. In the future, tests need to be added for videos, many
variations of the process endpoint, and the admin endpoints.

The new tests are locked behind a configuration option (not a feature)
called `system_deps`. They can be enabled by passing

`RUSTFLAGS='--cfg system_deps'`

This is to prevent tests from running and failing in environments where
the exiftool, imagemagick, and ffmpeg binaries are not present.

This builds imagemagick from source for a few reasons
1. binaries for imagemagick 7 are not available for debian 12
2. the imagemagick appimage does not support avif files
3. the imagemagick appimage is limited to x86_64
2025-03-28 10:30:03 -05:00

89 lines
2.1 KiB
Rust

#![cfg(system_deps)]
use common::{
pict_rs_test_config, upload_form, with_pict_rs, OkString, PictRsResult, UploadResponse,
};
mod common;
#[derive(serde::Deserialize)]
struct BackgroundResponse {
#[allow(unused)]
msg: OkString,
uploads: Vec<Upload>,
}
#[derive(serde::Deserialize)]
struct Upload {
upload_id: String,
}
#[test]
fn can_upload_and_download_file() {
let address = "127.0.0.1:8090";
let config = pict_rs_test_config(address);
with_pict_rs(config, || async {
let client = reqwest::Client::new();
let form = upload_form(["./client-examples/earth.gif"]).await;
let response = client
.post(format!("http://{address}/image/backgrounded"))
.multipart(form)
.send()
.await
.expect("send request");
let backgrounded = response
.json::<PictRsResult<BackgroundResponse>>()
.await
.expect("valid response")
.unwrap();
assert_eq!(backgrounded.uploads.len(), 1);
let upload_id = &backgrounded.uploads[0].upload_id;
let response = loop {
let response = client
.get(format!(
"http://{address}/image/backgrounded/claim?upload_id={upload_id}"
))
.send()
.await
.expect("send request");
if response.status() == 200 {
break response;
}
assert!(response.status().is_success());
};
let upload = response
.json::<PictRsResult<UploadResponse>>()
.await
.expect("valid response")
.unwrap();
assert_eq!(upload.files.len(), 1);
let alias = &upload.files[0].file;
let response = client
.get(format!("http://{address}/image/original/{alias}"))
.send()
.await
.expect("download file");
assert!(response.status().is_success(), "download failed");
let length = response.bytes().await.expect("downlaod bytes").len();
assert!(length > 0);
})
.unwrap();
}