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| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 295 | support::event_messages(event).into_iter().any(|event| { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 296 | event.own |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 297 | && event.transaction_id.is_some() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 298 | && support::media_of(event).is_some_and(|media| media.filename == "pending.png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | let media = support::event_messages(&event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 304 | .find(|event| event.own && support::media_of(event).is_some()) |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 305 | .and_then(support::media_of) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | assert_eq!((media.width, media.height), (Some(32), Some(16))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | assert_eq!(media.size_bytes, Some(original.len() as u64)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | let media_id = media.id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | let fetched = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | .media(&media_id, MediaSize::Full) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | assert_eq!(&*fetched.bytes, &original[..]); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | async fn image_upload_uses_caller_supplied_metadata_and_thumbnail() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | .expect_mime_type("image/png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | .respond_with(|request: &wiremock::Request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | let uri = if request.body == b"thumbnail bytes" { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | "mxc://localhost/image-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | "mxc://localhost/image-upload" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | ResponseTemplate::new(200).set_body_json(json!({ "content_uri": uri })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | .expect(2) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | .ok(event_id!("$image-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | let original = b"image bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | let thumbnail_bytes = b"thumbnail bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | "large.png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | "image/png", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | original.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | upload.details = Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | width: Some(900), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | height: Some(450), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | blurhash: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | thumbnail_bytes.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | 800, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | 400, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | assert_eq!(content["msgtype"], "m.image"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | assert_eq!(content["info"]["w"], 900); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | assert_eq!(content["info"]["h"], 450); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | assert_eq!(content["info"]["size"], original.len()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | assert_eq!(content["info"]["thumbnail_info"]["w"], 800); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | assert_eq!(content["info"]["thumbnail_info"]["h"], 400); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | assert_eq!(content["info"]["thumbnail_info"]["mimetype"], "image/png"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | content["info"]["thumbnail_info"]["size"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | thumbnail_bytes.len() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | content["info"]["thumbnail_url"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | "mxc://localhost/image-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | assert_eq!(content["url"], "mxc://localhost/image-upload"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | let requests = harness.server.received_requests().await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | let uploads = requests |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | .filter(|request| request.url.path().ends_with("/_matrix/media/v3/upload")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | assert_eq!(uploads.len(), 2); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | assert!(uploads.iter().any(|request| request.body == original)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | uploads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | .any(|request| request.body == thumbnail_bytes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | async fn video_upload_preserves_metadata_and_supplied_thumbnail() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 414 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | .expect_mime_type("image/png") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | .ok(mxc_uri!("mxc://localhost/video-thumb")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | .expect_mime_type("video/mp4") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | .ok(mxc_uri!("mxc://localhost/video-upload")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 431 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | .ok(event_id!("$video-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | "clip.mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | "video/mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | b"video bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | upload.details = Some(UploadDetails::Video { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | width: Some(1920), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | height: Some(1080), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | duration_ms: Some(3500), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | blurhash: Some("blurhash".to_owned()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 454 | b"poster bytes".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 455 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 456 | 320, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | 180, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | assert_eq!(content["msgtype"], "m.video"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | assert_eq!(content["info"]["w"], 1920); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | assert_eq!(content["info"]["h"], 1080); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | assert_eq!(content["info"]["duration"], 3500); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | assert_eq!(content["info"]["size"], 11); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | assert_eq!(content["info"]["thumbnail_info"]["w"], 320); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | assert_eq!(content["info"]["thumbnail_info"]["h"], 180); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | assert_eq!(content["info"]["thumbnail_info"]["size"], 12); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | content["info"]["thumbnail_url"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | "mxc://localhost/video-thumb" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | assert_eq!(content["url"], "mxc://localhost/video-upload"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | let requests = harness.server.received_requests().await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | assert!(requests.iter().any(|request| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | request.url.path().ends_with("/_matrix/media/v3/upload") && request.body == b"poster bytes" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | })); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | async fn voice_upload_emits_voice_details_and_correlated_progress() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | .mock_authenticated_media_config() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | .ok_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | .mock_upload() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | .expect_mime_type("audio/ogg") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | .respond_with( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | ResponseTemplate::new(200) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | .set_body_json(json!({ "content_uri": "mxc://localhost/voice-upload" })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | .set_delay(Duration::from_millis(500)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | .mock_room_send() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | .ok(event_id!("$voice-upload:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | .expect(1) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | let data = b"voice bytes".to_vec(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 515 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 516 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 517 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | data.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 519 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 520 | upload.details = Some(UploadDetails::Voice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | duration_ms: 4200, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | waveform: vec![0, 512, 1024], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | harness.handle.upload(upload).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | let pending = harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | .handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | .pending_send_ids(room_id!("!media:localhost")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | assert_eq!(pending.len(), 1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 532 | let transaction_id = pending[0].clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | let progress = support::receive_until(&mut harness.events, |event| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | matches!(event, Event::MediaUploadProgress { transaction_id: seen, complete: true, .. } if seen == &transaction_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 536 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | assert!(matches!(progress, Event::MediaUploadProgress { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 538 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 539 | transaction_id: seen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | uploaded_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | total_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | complete: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | } 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 | 544 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | let content = uploaded_content(&harness.server).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | assert_eq!(content["msgtype"], "m.audio"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | assert_eq!(content["info"]["duration"], 4200); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 548 | assert_eq!(content["info"]["size"], data.len()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 549 | assert_eq!(content["org.matrix.msc1767.audio"]["duration"], 4200); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | assert_eq!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | content["org.matrix.msc1767.audio"]["waveform"], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | json!([0, 512, 1024]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | assert_eq!(content["org.matrix.msc3245.voice"], json!({})); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 556 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | async fn mismatched_upload_metadata_and_invalid_waveform_fail_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | let mut mismatched = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | "clip.mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | "video/mp4", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | b"video bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | mismatched.details = Some(UploadDetails::Image { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | width: Some(32), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | height: Some(32), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | blurhash: None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 573 | let error = harness.handle.upload(mismatched).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 574 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | let mut voice = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | b"voice bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | voice.details = Some(UploadDetails::Voice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | duration_ms: 1000, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | waveform: vec![1025], |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 586 | let error = harness.handle.upload(voice).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 587 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 598 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | async fn upload_over_media_limit_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | let mut harness = start_with_config( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | plain_image(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | EngineConfig::default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | .with_initial_history_limit(0) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | .with_max_media_bytes(4), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 608 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | let upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | "large.bin", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | "application/octet-stream", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | b"too large".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 616 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 617 | assert_eq!(error.kind, ErrorKind::MediaTooLarge); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 620 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 630 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | async fn audio_thumbnail_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | let mut harness = start(plain_image()).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 633 | let mut upload = AttachmentUpload::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | room_id!("!media:localhost").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | "voice.ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 636 | "audio/ogg", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 637 | b"voice bytes".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 639 | upload.thumbnail = Some(UploadThumbnail::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 640 | b"thumbnail".to_vec(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 641 | "image/png", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 642 | 80, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 643 | 80, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 644 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 645 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 646 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | assert!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 648 | !harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 649 | .server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | .any(|request| { request.url.path().ends_with("/_matrix/media/v3/upload") }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 656 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | #[tokio::test] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 660 | async fn invalid_upload_mime_fails_without_uploading() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | let mut harness = start(plain_image()).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 662 | let upload = AttachmentUpload::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 663 | room_id!("!media:localhost").to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 664 | "photo.jpg", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 665 | "not a MIME type", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 666 | b"photo data".to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 668 | let error = harness.handle.upload(upload).await.unwrap_err(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 669 | assert_eq!(error.kind, ErrorKind::Invalid); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 671 | support::shut_down(&harness.handle, &mut harness.events, harness.running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | async fn start(content: RoomMessageEventContent) -> MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | start_with_config( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | EngineConfig::default().with_initial_history_limit(0), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | async fn start_with_config(content: RoomMessageEventContent, config: EngineConfig) -> MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | let room_id = room_id!("!media:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 697 | handle.open_room(room_id.to_owned()).await.unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 698 | support::receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 700 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 701 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | JoinedRoomBuilder::new(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | .add_timeline_event(EventFactory::new().event(content).sender(&ALICE)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 711 | let event = support::receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 712 | support::event_messages(event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 713 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 714 | .any(|event| support::media_of(event).is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 715 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 716 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 717 | let media_id = support::event_messages(&event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 718 | .into_iter() |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 719 | .find_map(|event| support::media_of(event).map(|media| media.id.clone())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 720 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 721 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 722 | MediaHarness { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 723 | server, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 724 | client: sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 725 | handle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 726 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 727 | running, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 728 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 729 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 730 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 731 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 732 | async fn mount_downloads(server: &MatrixMockServer) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 733 | server.mock_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 734 | server.mock_authed_media_download().ok_image().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 735 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 736 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 737 | fn thumbnail_size() -> MediaSize { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 738 | MediaSize::Thumbnail { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 739 | width: 800, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 740 | height: 800, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 741 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 742 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 743 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 744 | async fn fetch(harness: &MediaHarness, size: MediaSize) -> Arc<[u8]> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 745 | harness |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 746 | .handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 747 | .media(&harness.media_id, size) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 748 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 749 | .unwrap() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 750 | .bytes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 751 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 752 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 753 | async fn media_paths(server: &MatrixMockServer) -> Vec<String> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 754 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 755 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 756 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 757 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 758 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 759 | .map(|request| request.url.path().to_owned()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 760 | .filter(|path| path.contains("/media/")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 761 | .collect() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 762 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 763 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 764 | async fn uploaded_content(server: &MatrixMockServer) -> Value { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 765 | tokio::time::timeout(Duration::from_secs(5), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 766 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 767 | if let Some(request) = server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 768 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 769 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 770 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 771 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 772 | .find(|request| request.url.path().contains("/send/m.room.message/")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 773 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 774 | return serde_json::from_slice(&request.body).unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 775 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 776 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 777 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 778 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 779 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 780 | .expect("queued media was not sent") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 781 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 782 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | fn file_with_thumbnail() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | let mut info = FileInfo::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | info.thumbnail_source = Some(MediaSource::Plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | mxc_uri!("mxc://localhost/explicit-thumbnail").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 787 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 788 | RoomMessageEventContent::new(MessageType::File( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 789 | FileMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 790 | "file.bin".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 791 | mxc_uri!("mxc://localhost/full").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 792 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 793 | .info(Box::new(info)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 794 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 795 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 796 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 797 | fn plain_image() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 798 | RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 799 | "image.jpg".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 800 | mxc_uri!("mxc://localhost/image").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 801 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 802 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 803 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | fn plain_audio() -> RoomMessageEventContent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 805 | RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 806 | "audio.ogg".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 807 | mxc_uri!("mxc://localhost/audio").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 808 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 809 | } |