31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod support; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use std::{sync::Arc, time::Duration}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | use macltok::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | AttachmentUpload, ClientHandle, Engine, EngineConfig, EngineError, ErrorKind, Event, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | EventStream, MediaId, MediaSize, UploadDetails, UploadThumbnail, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | use matrix_sdk::{ |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 10 | media::{MediaFormat, MediaRequestParameters}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | ruma::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | api::client::sync::sync_events::v5, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | events::room::{ |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 15 | EncryptedFile, MediaSource, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | message::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | AudioMessageEventContent, FileInfo, FileMessageEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | ImageMessageEventContent, MessageType, RoomMessageEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | media::Method, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | mxc_uri, room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | use serde_json::{Value, json}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | use wiremock::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | Mock, ResponseTemplate, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | matchers::{method, path_regex}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | struct MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | server: MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | client: matrix_sdk::Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | handle: ClientHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | events: EventStream, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | running: tokio::task::JoinHandle<Result<(), EngineError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | media_id: MediaId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | async fn full_size_uses_full_source() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | let mut harness = start(file_with_thumbnail()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | mount_downloads(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | let bytes = fetch(&harness, MediaSize::Full).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | assert_eq!(&*bytes, b"binaryjpegfullimagedata"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | media_paths(&harness.server) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | .any(|path| path.ends_with("/localhost/full")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | #[tokio::test] |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 60 | async fn encrypted_media_is_cached_as_plaintext_like_the_sdk_does() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 61 | use std::io::Read as _; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 62 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 63 | let plaintext = b"very secret image".to_vec(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 64 | let mut cursor = std::io::Cursor::new(plaintext.clone()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 65 | let mut encryptor = matrix_sdk_crypto::AttachmentEncryptor::new(&mut cursor); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 66 | let mut ciphertext = Vec::new(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 67 | encryptor.read_to_end(&mut ciphertext).unwrap(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 68 | let keys = encryptor.finish(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 69 | let file = EncryptedFile::new( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 70 | mxc_uri!("mxc://localhost/secret").to_owned(), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 71 | keys.encryption_info, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 72 | keys.hashes, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 73 | ); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 74 | let source = MediaSource::Encrypted(Box::new(file.clone())); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 75 | let mut harness = start(RoomMessageEventContent::new(MessageType::Image( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 76 | ImageMessageEventContent::encrypted("secret.png".to_owned(), file), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 77 | ))) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 78 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 79 | Mock::given(method("GET")) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 80 | .and(path_regex("/download/localhost/secret$")) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 81 | .respond_with(ResponseTemplate::new(200).set_body_bytes(ciphertext)) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 82 | .mount(harness.server.server()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 83 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 84 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 85 | let from_sdk = harness |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 86 | .client |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 87 | .media() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 88 | .get_media_content( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 89 | &MediaRequestParameters { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 90 | source, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 91 | format: MediaFormat::File, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 92 | }, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 93 | true, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 94 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 95 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 96 | .unwrap(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 97 | assert_eq!(from_sdk, plaintext); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | assert_eq!(&*fetch(&harness, MediaSize::Full).await, &plaintext[..]); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 99 | assert_eq!(media_paths(&harness.server).await.len(), 1); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 100 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 102 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 103 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 104 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | async fn simultaneous_fetches_share_one_download() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | Mock::given(method("GET")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | .and(path_regex("/download/localhost/image$")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | .respond_with( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | ResponseTemplate::new(200) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | .set_body_bytes(b"shared image bytes".to_vec()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | .set_delay(Duration::from_millis(300)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .mount(harness.server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | let (first, second) = tokio::time::timeout(Duration::from_secs(5), async { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | tokio::join!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | harness.handle.media(&harness.media_id, MediaSize::Full), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | harness.handle.media(&harness.media_id, MediaSize::Full), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | .expect("both fetches did not complete"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | assert_eq!(&*first.unwrap().bytes, b"shared image bytes"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | assert_eq!(&*second.unwrap().bytes, b"shared image bytes"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | let downloads = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | .filter(|request| request.url.path().ends_with("/download/localhost/image")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | .count(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | assert_eq!(downloads, 1, "duplicate in-flight media download"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | async fn downloaded_media_over_limit_is_rejected() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | let mut harness = start_with_config( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | plain_image(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | EngineConfig::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | .with_initial_history_limit(0) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | .with_max_media_bytes(4), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | Mock::given(method("GET")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | .and(path_regex("/download/localhost/image$")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | .respond_with(ResponseTemplate::new(200).set_body_bytes(b"too large".to_vec())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | .mount(harness.server.server()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | let error = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | .media(&harness.media_id, MediaSize::Full) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | .unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | assert_eq!(error.kind, ErrorKind::MediaTooLarge); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | async fn thumbnail_size_prefers_explicit_thumbnail() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | let mut harness = start(file_with_thumbnail()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | mount_downloads(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | let bytes = fetch(&harness, thumbnail_size()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | assert_eq!(&*bytes, b"binaryjpegfullimagedata"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | let paths = media_paths(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | paths |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | .any(|path| path.ends_with("/localhost/explicit-thumbnail")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | assert!(!paths.iter().any(|path| path.ends_with("/localhost/full"))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | async fn plain_image_thumbnail_uses_thumbnail_endpoint() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | .mock_media_thumbnail(Method::Scale, 800, 800, false) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | .mock_authed_media_thumbnail(Method::Scale, 800, 800, false) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | .ok() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | let bytes = fetch(&harness, thumbnail_size()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | assert_eq!(&*bytes, b"binaryjpegthumbnaildata"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | media_paths(&harness.server) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | .any(|path| path.contains("/thumbnail/localhost/image")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | #[tokio::test] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | async fn audio_thumbnail_fetch_fails_with_media_unavailable() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | let mut harness = start(plain_audio()).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | let error = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | .media(&harness.media_id, thumbnail_size()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | .unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | assert_eq!(error.kind, ErrorKind::MediaUnavailable); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | async fn media_upload_is_queued_and_sent() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | .expect_mime_type("image/jpeg") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | .ok(mxc_uri!("mxc://localhost/uploaded")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | .ok(event_id!("$uploaded:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | let mut upload = AttachmentUpload::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | room_id!("!media:localhost").to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | "photo.jpg", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | "image/jpeg", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | b"photo data".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | upload.caption = Some("holiday".to_owned()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | tokio::time::timeout(Duration::from_secs(5), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | let requests = harness.server.received_requests().await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | if requests.iter().any(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | request.url.path().contains("/send/m.room.message/") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | && String::from_utf8_lossy(&request.body).contains("mxc://localhost/uploaded") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | }) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | .expect("queued media was not sent"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | async fn queued_offline_attachment_exposes_fetchable_local_media() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | harness.client.send_queue().set_enabled(false).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | let original = b"offline image bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | "pending.png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | "image/png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | original.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | upload.details = Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | width: Some(32), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | height: Some(16), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | blurhash: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | harness.handle.upload(upload).await.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | let event = support::receive_until(&mut harness.events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | support::event_messages(event).into_iter().any(|message| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | message.own |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | && message.transaction_id.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | && message |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | .media |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | .as_ref() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | .is_some_and(|media| media.filename == "pending.png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | let media = support::event_messages(&event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | .find(|message| message.own && message.media.is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | .and_then(|message| message.media.as_ref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | assert_eq!((media.width, media.height), (Some(32), Some(16))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | assert_eq!(media.size_bytes, Some(original.len() as u64)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | let media_id = media.id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | let fetched = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | .media(&media_id, MediaSize::Full) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | assert_eq!(&*fetched.bytes, &original[..]); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | async fn image_upload_uses_caller_supplied_metadata_and_thumbnail() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | .expect_mime_type("image/png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | .respond_with(|request: &wiremock::Request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | let uri = if request.body == b"thumbnail bytes" { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | "mxc://localhost/image-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | "mxc://localhost/image-upload" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | ResponseTemplate::new(200).set_body_json(json!({ "content_uri": uri })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | .expect(2) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | .ok(event_id!("$image-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | let original = b"image bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | let thumbnail_bytes = b"thumbnail bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | "large.png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | "image/png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | original.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | upload.details = Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | width: Some(900), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | height: Some(450), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | blurhash: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | thumbnail_bytes.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | 800, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | 400, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | assert_eq!(content["msgtype"], "m.image"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | assert_eq!(content["info"]["w"], 900); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | assert_eq!(content["info"]["h"], 450); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | assert_eq!(content["info"]["size"], original.len()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | assert_eq!(content["info"]["thumbnail_info"]["w"], 800); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | assert_eq!(content["info"]["thumbnail_info"]["h"], 400); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | assert_eq!(content["info"]["thumbnail_info"]["mimetype"], "image/png"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | content["info"]["thumbnail_info"]["size"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | thumbnail_bytes.len() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | content["info"]["thumbnail_url"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | "mxc://localhost/image-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | assert_eq!(content["url"], "mxc://localhost/image-upload"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | let requests = harness.server.received_requests().await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | let uploads = requests |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | .filter(|request| request.url.path().ends_with("/_matrix/media/v3/upload")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | assert_eq!(uploads.len(), 2); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | assert!(uploads.iter().any(|request| request.body == original)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | uploads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | .any(|request| request.body == thumbnail_bytes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | async fn video_upload_preserves_metadata_and_supplied_thumbnail() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 414 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | .expect_mime_type("image/png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | .ok(mxc_uri!("mxc://localhost/video-thumb")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 431 | .expect_mime_type("video/mp4") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | .ok(mxc_uri!("mxc://localhost/video-upload")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | .ok(event_id!("$video-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | "clip.mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | "video/mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | b"video bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | upload.details = Some(UploadDetails::Video { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | width: Some(1920), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | height: Some(1080), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | duration_ms: Some(3500), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | blurhash: Some("blurhash".to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 456 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | b"poster bytes".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | 320, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 460 | 180, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 461 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | assert_eq!(content["msgtype"], "m.video"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | assert_eq!(content["info"]["w"], 1920); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | assert_eq!(content["info"]["h"], 1080); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | assert_eq!(content["info"]["duration"], 3500); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | assert_eq!(content["info"]["size"], 11); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | assert_eq!(content["info"]["thumbnail_info"]["w"], 320); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | assert_eq!(content["info"]["thumbnail_info"]["h"], 180); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | assert_eq!(content["info"]["thumbnail_info"]["size"], 12); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | content["info"]["thumbnail_url"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | "mxc://localhost/video-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | assert_eq!(content["url"], "mxc://localhost/video-upload"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | let requests = harness.server.received_requests().await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | assert!(requests.iter().any(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | request.url.path().ends_with("/_matrix/media/v3/upload") && request.body == b"poster bytes" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | })); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | async fn voice_upload_emits_voice_details_and_correlated_progress() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | .expect_mime_type("audio/ogg") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | .respond_with( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | ResponseTemplate::new(200) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | .set_body_json(json!({ "content_uri": "mxc://localhost/voice-upload" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | .set_delay(Duration::from_millis(500)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | .ok(event_id!("$voice-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 515 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 516 | let data = b"voice bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 517 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 519 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 520 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | data.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | upload.details = Some(UploadDetails::Voice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 524 | duration_ms: 4200, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | waveform: vec![0, 512, 1024], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | let pending = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | .handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | .pending_send_ids(room_id!("!media:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 532 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 533 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | assert_eq!(pending.len(), 1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | let transaction_id = pending[0].clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | let progress = support::receive_until(&mut harness.events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | matches!(event, Event::MediaUploadProgress { transaction_id: seen, complete: true, .. } if seen == &transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 538 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 539 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | assert!(matches!(progress, Event::MediaUploadProgress { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | transaction_id: seen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | uploaded_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 544 | total_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | complete: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | } if room_id == room_id!("!media:localhost") && seen == transaction_id && uploaded_bytes == data.len() as u64 && total_bytes == data.len() as u64)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 548 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 549 | assert_eq!(content["msgtype"], "m.audio"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | assert_eq!(content["info"]["duration"], 4200); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | assert_eq!(content["info"]["size"], data.len()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | assert_eq!(content["org.matrix.msc1767.audio"]["duration"], 4200); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | content["org.matrix.msc1767.audio"]["waveform"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | json!([0, 512, 1024]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | assert_eq!(content["org.matrix.msc3245.voice"], json!({})); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 559 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | async fn mismatched_upload_metadata_and_invalid_waveform_fail_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | let mut mismatched = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | "clip.mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | "video/mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | b"video bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | mismatched.details = Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | width: Some(32), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | height: Some(32), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | blurhash: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 576 | let error = harness.handle.upload(mismatched).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 577 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | let mut voice = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | b"voice bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | voice.details = Some(UploadDetails::Voice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | duration_ms: 1000, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | waveform: vec![1025], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 589 | let error = harness.handle.upload(voice).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 590 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 601 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | async fn upload_over_media_limit_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | let mut harness = start_with_config( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | plain_image(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 608 | EngineConfig::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | .with_initial_history_limit(0) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | .with_max_media_bytes(4), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | let upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | "large.bin", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | "application/octet-stream", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | b"too large".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 619 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 620 | assert_eq!(error.kind, ErrorKind::MediaTooLarge); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 627 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 630 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 633 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | async fn audio_thumbnail_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 636 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 637 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 639 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 640 | b"voice bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 641 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 642 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 643 | b"thumbnail".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 644 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 645 | 80, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 646 | 80, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 648 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 649 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 659 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | #[tokio::test] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 663 | async fn invalid_upload_mime_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | let mut harness = start(plain_image()).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 665 | let upload = AttachmentUpload::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 666 | room_id!("!media:localhost").to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 667 | "photo.jpg", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 668 | "not a MIME type", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 669 | b"photo data".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 671 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 672 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 674 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | async fn start(content: RoomMessageEventContent) -> MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | start_with_config( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | async fn start_with_config(content: RoomMessageEventContent, config: EngineConfig) -> MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | let room_id = room_id!("!media:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 697 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 698 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 700 | handle.open_room(room_id.to_owned()).await.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 701 | support::receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 711 | .add_timeline_event(EventFactory::new().event(content).sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 712 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 713 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 714 | let event = support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 715 | support::event_messages(event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 716 | .into_iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 717 | .any(|message| message.media.is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 718 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 719 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 720 | let media_id = support::event_messages(&event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 721 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 722 | .find_map(|message| message.media.as_ref().map(|media| media.id.clone())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 723 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 724 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 725 | MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 726 | server, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 727 | client: sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 728 | handle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 729 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 730 | running, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 731 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 732 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 733 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 734 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 735 | async fn mount_downloads(server: &MatrixMockServer) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 736 | server.mock_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 737 | server.mock_authed_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 738 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 739 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 740 | fn thumbnail_size() -> MediaSize { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 741 | MediaSize::Thumbnail { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 742 | width: 800, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 743 | height: 800, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 744 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 745 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 746 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 747 | async fn fetch(harness: &MediaHarness, size: MediaSize) -> Arc<[u8]> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 748 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 749 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 750 | .media(&harness.media_id, size) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 751 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 752 | .unwrap() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 753 | .bytes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 754 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 755 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 756 | async fn media_paths(server: &MatrixMockServer) -> Vec<String> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 757 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 758 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 759 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 760 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 761 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 762 | .map(|request| request.url.path().to_owned()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 763 | .filter(|path| path.contains("/media/")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 764 | .collect() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 765 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 766 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 767 | async fn uploaded_content(server: &MatrixMockServer) -> Value { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 768 | tokio::time::timeout(Duration::from_secs(5), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 769 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 770 | if let Some(request) = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 771 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 772 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 773 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 774 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 775 | .find(|request| request.url.path().contains("/send/m.room.message/")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 776 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 777 | return serde_json::from_slice(&request.body).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 778 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 779 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 780 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 781 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 782 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | .expect("queued media was not sent") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | fn file_with_thumbnail() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 787 | let mut info = FileInfo::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 788 | info.thumbnail_source = Some(MediaSource::Plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 789 | mxc_uri!("mxc://localhost/explicit-thumbnail").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 790 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 791 | RoomMessageEventContent::new(MessageType::File( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 792 | FileMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 793 | "file.bin".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 794 | mxc_uri!("mxc://localhost/full").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 795 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 796 | .info(Box::new(info)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 797 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 798 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 799 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 800 | fn plain_image() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 801 | RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 802 | "image.jpg".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 803 | mxc_uri!("mxc://localhost/image").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 805 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 806 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 807 | fn plain_audio() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 808 | RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 809 | "audio.ogg".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 810 | mxc_uri!("mxc://localhost/audio").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 811 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 812 | } |