mod mock;
use mock::fixtures;
use rustigram_types::file::{InputFile, InputProfilePhoto, InputProfilePhotoStatic};
#[tokio::test]
async fn setting_a_profile_photo_uploads_a_multipart_form() {
let (server, client) = mock::spawn().await;
mock::mount_catch_all(&server).await;
let _ = client
.set_my_profile_photo(InputProfilePhoto::Static(InputProfilePhotoStatic {
photo: "attach://photo".to_owned(),
}))
.await;
let request = mock::only_request(&server).await;
let content_type = request
.headers
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or_default();
assert!(
content_type.starts_with("multipart/form-data"),
"a profile photo must go out as multipart, got `{content_type}`"
);
let fields = mock::multipart_field_names(&request);
assert!(
fields.iter().any(|f| f == "photo"),
"the photo descriptor never reached the form: {fields:?}"
);
}
#[tokio::test]
async fn a_sticker_set_thumbnail_uploads_its_bytes() {
let (server, client) = mock::spawn().await;
mock::mount_catch_all(&server).await;
let _ = client
.set_sticker_set_thumbnail("set_name", 7, "static")
.thumbnail(fixtures::uploaded_file())
.await;
let request = mock::only_request(&server).await;
let fields = mock::multipart_field_names(&request);
for expected in ["name", "user_id", "format", "thumbnail"] {
assert!(
fields.iter().any(|f| f == expected),
"`{expected}` never reached the form: {fields:?}"
);
}
}
#[tokio::test]
async fn a_sticker_set_thumbnail_by_file_id_carries_the_same_fields() {
let (server, client) = mock::spawn().await;
mock::mount_catch_all(&server).await;
let _ = client
.set_sticker_set_thumbnail("set_name", 7, "static")
.thumbnail(fixtures::input_file())
.await;
let body = mock::json_body(&mock::only_request(&server).await);
for expected in ["name", "user_id", "format", "thumbnail"] {
assert!(
body.get(expected).is_some(),
"`{expected}` is missing from the JSON body: {body}"
);
}
}
#[tokio::test]
async fn a_webhook_certificate_uploads_with_its_options() {
let (server, client) = mock::spawn().await;
mock::mount_catch_all(&server).await;
let _ = client
.set_webhook("https://example.test/hook")
.certificate(InputFile::Bytes {
filename: "cert.pem".to_owned(),
data: b"-----BEGIN CERTIFICATE-----".to_vec(),
mime_type: "application/x-pem-file".to_owned(),
})
.secret_token("s3cret")
.max_connections(40)
.drop_pending_updates(true)
.await;
let request = mock::only_request(&server).await;
let content_type = request
.headers
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or_default();
assert!(
content_type.starts_with("multipart/form-data"),
"a certificate upload must go out as multipart, got `{content_type}`"
);
let fields = mock::multipart_field_names(&request);
for expected in [
"url",
"certificate",
"secret_token",
"max_connections",
"drop_pending_updates",
] {
assert!(
fields.iter().any(|f| f == expected),
"`{expected}` was set on the builder and never reached the form: {fields:?}"
);
}
}
#[tokio::test]
async fn a_webhook_without_a_certificate_carries_the_same_options() {
let (server, client) = mock::spawn().await;
mock::mount_catch_all(&server).await;
let _ = client
.set_webhook("https://example.test/hook")
.secret_token("s3cret")
.max_connections(40)
.drop_pending_updates(true)
.await;
let body = mock::json_body(&mock::only_request(&server).await);
for expected in [
"url",
"secret_token",
"max_connections",
"drop_pending_updates",
] {
assert!(
body.get(expected).is_some(),
"`{expected}` is missing from the JSON body: {body}"
);
}
}