| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
use crate::harness::TestHarness; |
| 44 |
use serde_json::{Value, json}; |
| 45 |
|
| 46 |
|
| 47 |
async fn creator_with_project(h: &mut TestHarness, username: &str) -> (String, String) { |
| 48 |
let setup = h.create_creator_with_item(username, "audio", 0).await; |
| 49 |
h.trust_user(setup.user_id).await; |
| 50 |
h.grant_tier(setup.user_id, "small_files").await; |
| 51 |
(setup.project_id, setup.item_id) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
async fn presign_project_cover(h: &mut TestHarness, project_id: &str, file_name: &str) -> String { |
| 56 |
let body = json!({ |
| 57 |
"project_id": project_id, |
| 58 |
"file_name": file_name, |
| 59 |
"content_type": "image/png", |
| 60 |
}); |
| 61 |
let resp = h |
| 62 |
.client |
| 63 |
.post_json("/api/projects/image/presign", &body.to_string()) |
| 64 |
.await; |
| 65 |
assert_eq!(resp.status.as_u16(), 200, "presign failed: {}", resp.text); |
| 66 |
let data: Value = resp.json(); |
| 67 |
data["s3_key"] |
| 68 |
.as_str() |
| 69 |
.expect("presign returns an s3_key") |
| 70 |
.to_string() |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
fn upload_bytes(h: &TestHarness, key: &str, size: usize) { |
| 75 |
h.storage |
| 76 |
.as_ref() |
| 77 |
.expect("with_storage provides an in-memory bucket") |
| 78 |
.put(key, vec![b'x'; size]); |
| 79 |
} |
| 80 |
|
| 81 |
async fn confirm_project_cover( |
| 82 |
h: &mut TestHarness, |
| 83 |
project_id: &str, |
| 84 |
key: &str, |
| 85 |
) -> crate::harness::client::TestResponse { |
| 86 |
let body = json!({ "project_id": project_id, "s3_key": key }); |
| 87 |
h.client |
| 88 |
.post_json("/api/projects/image/confirm", &body.to_string()) |
| 89 |
.await |
| 90 |
} |
| 91 |
|
| 92 |
async fn deletions_for(h: &TestHarness, key: &str) -> i64 { |
| 93 |
sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM pending_s3_deletions WHERE s3_key = $1") |
| 94 |
.bind(key) |
| 95 |
.fetch_one(&h.db) |
| 96 |
.await |
| 97 |
.expect("count queued deletions") |
| 98 |
} |
| 99 |
|
| 100 |
async fn pending_upload_rows(h: &TestHarness, key: &str) -> i64 { |
| 101 |
sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM pending_uploads WHERE s3_key = $1") |
| 102 |
.bind(key) |
| 103 |
.fetch_one(&h.db) |
| 104 |
.await |
| 105 |
.expect("count pending uploads") |
| 106 |
} |
| 107 |
|
| 108 |
async fn storage_used(h: &TestHarness, username: &str) -> i64 { |
| 109 |
sqlx::query_scalar::<_, i64>("SELECT storage_used_bytes FROM users WHERE username = $1") |
| 110 |
.bind(username) |
| 111 |
.fetch_one(&h.db) |
| 112 |
.await |
| 113 |
.expect("read storage counter") |
| 114 |
} |
| 115 |
|
| 116 |
async fn cover_url(h: &TestHarness, project_id: &str) -> Option<String> { |
| 117 |
sqlx::query_scalar::<_, Option<String>>( |
| 118 |
"SELECT cover_image_url FROM projects WHERE id = $1::uuid", |
| 119 |
) |
| 120 |
.bind(project_id) |
| 121 |
.fetch_one(&h.db) |
| 122 |
.await |
| 123 |
.expect("read project cover url") |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#[tokio::test] |
| 135 |
async fn confirming_a_key_minted_for_another_creator_is_refused_and_deletes_nothing() { |
| 136 |
let mut h = TestHarness::with_storage().await; |
| 137 |
|
| 138 |
let (victim_project, _) = creator_with_project(&mut h, "coverowner").await; |
| 139 |
let victim_key = presign_project_cover(&mut h, &victim_project, "mine.png").await; |
| 140 |
upload_bytes(&h, &victim_key, 2048); |
| 141 |
h.client.post_form("/logout", "").await; |
| 142 |
|
| 143 |
let (thief_project, _) = creator_with_project(&mut h, "coverthief").await; |
| 144 |
let resp = confirm_project_cover(&mut h, &thief_project, &victim_key).await; |
| 145 |
|
| 146 |
assert_eq!( |
| 147 |
resp.status.as_u16(), |
| 148 |
400, |
| 149 |
"a key with no pending_uploads row for this user is not confirmable: {}", |
| 150 |
resp.text |
| 151 |
); |
| 152 |
assert_eq!( |
| 153 |
deletions_for(&h, &victim_key).await, |
| 154 |
0, |
| 155 |
"the refusal must happen before the size-reject path, or a stranger can \ |
| 156 |
aim the deletion queue at an in-flight upload" |
| 157 |
); |
| 158 |
assert_eq!( |
| 159 |
pending_upload_rows(&h, &victim_key).await, |
| 160 |
1, |
| 161 |
"and the owner's claim on the key is untouched" |
| 162 |
); |
| 163 |
assert_eq!( |
| 164 |
cover_url(&h, &thief_project).await, |
| 165 |
None, |
| 166 |
"nothing was written to the caller's own project either" |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
#[tokio::test] |
| 179 |
async fn a_retried_confirm_is_refused_without_touching_the_live_cover() { |
| 180 |
let mut h = TestHarness::with_storage().await; |
| 181 |
let (project_id, _) = creator_with_project(&mut h, "coverretry").await; |
| 182 |
|
| 183 |
let key = presign_project_cover(&mut h, &project_id, "cover.png").await; |
| 184 |
upload_bytes(&h, &key, 4096); |
| 185 |
let first = confirm_project_cover(&mut h, &project_id, &key).await; |
| 186 |
assert_eq!(first.status.as_u16(), 200, "first confirm: {}", first.text); |
| 187 |
let url = cover_url(&h, &project_id) |
| 188 |
.await |
| 189 |
.expect("the cover url is written"); |
| 190 |
let charged = storage_used(&h, "coverretry").await; |
| 191 |
|
| 192 |
assert_eq!( |
| 193 |
pending_upload_rows(&h, &key).await, |
| 194 |
0, |
| 195 |
"a successful confirm consumes the key's claim" |
| 196 |
); |
| 197 |
|
| 198 |
let again = confirm_project_cover(&mut h, &project_id, &key).await; |
| 199 |
assert_eq!( |
| 200 |
again.status.as_u16(), |
| 201 |
400, |
| 202 |
"the key is spent, so the gate refuses before anything else runs: {}", |
| 203 |
again.text |
| 204 |
); |
| 205 |
assert_eq!( |
| 206 |
deletions_for(&h, &key).await, |
| 207 |
0, |
| 208 |
"and the object the project is displaying is not queued for deletion (Run #6)" |
| 209 |
); |
| 210 |
assert_eq!( |
| 211 |
cover_url(&h, &project_id).await.as_deref(), |
| 212 |
Some(url.as_str()), |
| 213 |
"the project still shows the same image" |
| 214 |
); |
| 215 |
assert_eq!( |
| 216 |
storage_used(&h, "coverretry").await, |
| 217 |
charged, |
| 218 |
"and is still charged once" |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
#[tokio::test] |
| 233 |
async fn a_confirm_that_lands_on_the_cover_already_shown_returns_it_and_deletes_nothing() { |
| 234 |
let mut h = TestHarness::with_storage().await; |
| 235 |
let (project_id, _) = creator_with_project(&mut h, "covercrash").await; |
| 236 |
|
| 237 |
let key = presign_project_cover(&mut h, &project_id, "cover.png").await; |
| 238 |
upload_bytes(&h, &key, 4096); |
| 239 |
let first = confirm_project_cover(&mut h, &project_id, &key).await; |
| 240 |
assert_eq!(first.status.as_u16(), 200, "first confirm: {}", first.text); |
| 241 |
let url = cover_url(&h, &project_id) |
| 242 |
.await |
| 243 |
.expect("the cover url is written"); |
| 244 |
let charged = storage_used(&h, "covercrash").await; |
| 245 |
|
| 246 |
|
| 247 |
let user_id: makenotwork::db::UserId = |
| 248 |
sqlx::query_scalar("SELECT id FROM users WHERE username = 'covercrash'") |
| 249 |
.fetch_one(&h.db) |
| 250 |
.await |
| 251 |
.expect("read the creator id"); |
| 252 |
sqlx::query("INSERT INTO pending_uploads (user_id, s3_key, bucket) VALUES ($1, $2, 'main')") |
| 253 |
.bind(user_id) |
| 254 |
.bind(&key) |
| 255 |
.execute(&h.db) |
| 256 |
.await |
| 257 |
.expect("restore the pending row a crash would have left"); |
| 258 |
|
| 259 |
let again = confirm_project_cover(&mut h, &project_id, &key).await; |
| 260 |
assert_eq!( |
| 261 |
again.status.as_u16(), |
| 262 |
200, |
| 263 |
"the key already on display is confirmed, not re-applied: {}", |
| 264 |
again.text |
| 265 |
); |
| 266 |
let body: Value = again.json(); |
| 267 |
assert_eq!( |
| 268 |
body["image_url"].as_str(), |
| 269 |
Some(url.as_str()), |
| 270 |
"and the answer is the URL the project already has" |
| 271 |
); |
| 272 |
|
| 273 |
assert_eq!( |
| 274 |
deletions_for(&h, &key).await, |
| 275 |
0, |
| 276 |
"the live object must not be queued as the replaced one (Run #6)" |
| 277 |
); |
| 278 |
assert_eq!( |
| 279 |
pending_upload_rows(&h, &key).await, |
| 280 |
0, |
| 281 |
"and the stale row is cleared, or the reaper deletes the live cover (Run #7)" |
| 282 |
); |
| 283 |
assert_eq!( |
| 284 |
storage_used(&h, "covercrash").await, |
| 285 |
charged, |
| 286 |
"one image on display, charged once" |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#[tokio::test] |
| 295 |
async fn replacing_a_cover_charges_the_delta_and_queues_the_old_key() { |
| 296 |
let mut h = TestHarness::with_storage().await; |
| 297 |
let (project_id, _) = creator_with_project(&mut h, "coverswap").await; |
| 298 |
|
| 299 |
let first_key = presign_project_cover(&mut h, &project_id, "first.png").await; |
| 300 |
upload_bytes(&h, &first_key, 4000); |
| 301 |
let resp = confirm_project_cover(&mut h, &project_id, &first_key).await; |
| 302 |
assert_eq!(resp.status.as_u16(), 200, "first confirm: {}", resp.text); |
| 303 |
let after_first = storage_used(&h, "coverswap").await; |
| 304 |
assert_eq!(after_first, 4000, "the first image is charged in full"); |
| 305 |
|
| 306 |
let second_key = presign_project_cover(&mut h, &project_id, "second.png").await; |
| 307 |
upload_bytes(&h, &second_key, 6000); |
| 308 |
let resp = confirm_project_cover(&mut h, &project_id, &second_key).await; |
| 309 |
assert_eq!(resp.status.as_u16(), 200, "replace confirm: {}", resp.text); |
| 310 |
|
| 311 |
assert_eq!( |
| 312 |
storage_used(&h, "coverswap").await, |
| 313 |
6000, |
| 314 |
"a replacement charges the delta: one image on display, one image billed" |
| 315 |
); |
| 316 |
assert!( |
| 317 |
deletions_for(&h, &first_key).await > 0, |
| 318 |
"the replaced object is queued for deletion rather than left to the reaper" |
| 319 |
); |
| 320 |
assert!( |
| 321 |
cover_url(&h, &project_id) |
| 322 |
.await |
| 323 |
.is_some_and(|u| u.contains(&second_key)), |
| 324 |
"and the project displays the new image" |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
#[tokio::test] |
| 333 |
async fn a_strangers_project_is_refused_at_presign_and_at_confirm() { |
| 334 |
let mut h = TestHarness::with_storage().await; |
| 335 |
|
| 336 |
let (owned_project, _) = creator_with_project(&mut h, "coverwall").await; |
| 337 |
h.client.post_form("/logout", "").await; |
| 338 |
let (own_project, _) = creator_with_project(&mut h, "coverintruder").await; |
| 339 |
|
| 340 |
let body = json!({ |
| 341 |
"project_id": owned_project, |
| 342 |
"file_name": "theirs.png", |
| 343 |
"content_type": "image/png", |
| 344 |
}); |
| 345 |
let resp = h |
| 346 |
.client |
| 347 |
.post_json("/api/projects/image/presign", &body.to_string()) |
| 348 |
.await; |
| 349 |
assert_eq!( |
| 350 |
resp.status.as_u16(), |
| 351 |
403, |
| 352 |
"a stranger cannot mint an upload slot against another creator's project: {}", |
| 353 |
resp.text |
| 354 |
); |
| 355 |
|
| 356 |
|
| 357 |
let own_key = presign_project_cover(&mut h, &own_project, "ok.png").await; |
| 358 |
upload_bytes(&h, &own_key, 1024); |
| 359 |
let resp = confirm_project_cover(&mut h, &owned_project, &own_key).await; |
| 360 |
assert_eq!( |
| 361 |
resp.status.as_u16(), |
| 362 |
403, |
| 363 |
"nor confirm into it with a key they legitimately own: {}", |
| 364 |
resp.text |
| 365 |
); |
| 366 |
assert_eq!( |
| 367 |
cover_url(&h, &owned_project).await, |
| 368 |
None, |
| 369 |
"the target project is untouched" |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
#[tokio::test] |
| 377 |
async fn an_unknown_project_is_not_found_rather_than_forbidden() { |
| 378 |
let mut h = TestHarness::with_storage().await; |
| 379 |
creator_with_project(&mut h, "covermissing").await; |
| 380 |
|
| 381 |
let body = json!({ |
| 382 |
"project_id": "ce9f7087-d503-4cf1-8f80-b2080508e5fe", |
| 383 |
"file_name": "ghost.png", |
| 384 |
"content_type": "image/png", |
| 385 |
}); |
| 386 |
let resp = h |
| 387 |
.client |
| 388 |
.post_json("/api/projects/image/presign", &body.to_string()) |
| 389 |
.await; |
| 390 |
|
| 391 |
assert_eq!( |
| 392 |
resp.status.as_u16(), |
| 393 |
404, |
| 394 |
"an id that matches no project is not found: {}", |
| 395 |
resp.text |
| 396 |
); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
#[tokio::test] |
| 403 |
async fn the_image_routes_refuse_a_non_image_upload() { |
| 404 |
let mut h = TestHarness::with_storage().await; |
| 405 |
let (project_id, item_id) = creator_with_project(&mut h, "coverwrongtype").await; |
| 406 |
|
| 407 |
let body = json!({ |
| 408 |
"project_id": project_id, |
| 409 |
"file_name": "cover.png", |
| 410 |
"content_type": "audio/mpeg", |
| 411 |
}); |
| 412 |
let resp = h |
| 413 |
.client |
| 414 |
.post_json("/api/projects/image/presign", &body.to_string()) |
| 415 |
.await; |
| 416 |
assert_eq!( |
| 417 |
resp.status.as_u16(), |
| 418 |
400, |
| 419 |
"audio is not a project cover: {}", |
| 420 |
resp.text |
| 421 |
); |
| 422 |
|
| 423 |
let body = json!({ |
| 424 |
"item_id": item_id, |
| 425 |
"file_name": "cover.mp3", |
| 426 |
"content_type": "image/png", |
| 427 |
}); |
| 428 |
let resp = h |
| 429 |
.client |
| 430 |
.post_json("/api/items/image/presign", &body.to_string()) |
| 431 |
.await; |
| 432 |
assert_eq!( |
| 433 |
resp.status.as_u16(), |
| 434 |
400, |
| 435 |
"the extension has to agree with the declared image type: {}", |
| 436 |
resp.text |
| 437 |
); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
#[tokio::test] |
| 444 |
async fn the_item_route_also_refuses_a_key_it_did_not_mint() { |
| 445 |
let mut h = TestHarness::with_storage().await; |
| 446 |
|
| 447 |
let (project_id, _) = creator_with_project(&mut h, "itemcoverowner").await; |
| 448 |
let victim_key = presign_project_cover(&mut h, &project_id, "mine.png").await; |
| 449 |
upload_bytes(&h, &victim_key, 2048); |
| 450 |
h.client.post_form("/logout", "").await; |
| 451 |
|
| 452 |
let (_, thief_item) = creator_with_project(&mut h, "itemcoverthief").await; |
| 453 |
let body = json!({ "item_id": thief_item, "s3_key": victim_key }); |
| 454 |
let resp = h |
| 455 |
.client |
| 456 |
.post_json("/api/items/image/confirm", &body.to_string()) |
| 457 |
.await; |
| 458 |
|
| 459 |
assert_eq!( |
| 460 |
resp.status.as_u16(), |
| 461 |
400, |
| 462 |
"the item route proves key ownership the same way: {}", |
| 463 |
resp.text |
| 464 |
); |
| 465 |
assert_eq!( |
| 466 |
deletions_for(&h, &victim_key).await, |
| 467 |
0, |
| 468 |
"and refuses before anything can be queued for deletion" |
| 469 |
); |
| 470 |
} |
| 471 |
|