| 1 |
|
| 2 |
|
| 3 |
use crate::harness::TestHarness; |
| 4 |
use makenotwork::db::{OtaReleaseId, SyncAppId, UserId}; |
| 5 |
use serde::Deserialize; |
| 6 |
use serde_json::json; |
| 7 |
use sqlx::PgPool; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#[derive(Deserialize)] |
| 12 |
struct AuthResponse { |
| 13 |
token: String, |
| 14 |
#[serde(rename = "user_id")] |
| 15 |
_user_id: UserId, |
| 16 |
#[serde(rename = "app_id")] |
| 17 |
_app_id: SyncAppId, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Deserialize)] |
| 21 |
struct ReleaseResponse { |
| 22 |
id: OtaReleaseId, |
| 23 |
version: String, |
| 24 |
} |
| 25 |
|
| 26 |
#[derive(Deserialize)] |
| 27 |
struct UploadArtifactResponse { |
| 28 |
upload_url: String, |
| 29 |
} |
| 30 |
|
| 31 |
#[derive(Deserialize)] |
| 32 |
struct TauriUpdaterResponse { |
| 33 |
version: String, |
| 34 |
url: String, |
| 35 |
signature: String, |
| 36 |
notes: String, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
async fn create_sync_app_with_slug( |
| 43 |
pool: &PgPool, |
| 44 |
user_id: UserId, |
| 45 |
slug: &str, |
| 46 |
) -> (SyncAppId, String) { |
| 47 |
let api_key = format!("test-ota-key-{}", slug); |
| 48 |
let key_hash = crate::harness::hash_api_key(&api_key); |
| 49 |
let key_prefix = &api_key[..8]; |
| 50 |
let app_id: SyncAppId = sqlx::query_scalar( |
| 51 |
"INSERT INTO sync_apps (creator_id, name, api_key_hash, api_key_prefix, slug) VALUES ($1, $2, $3, $4, $5) RETURNING id", |
| 52 |
) |
| 53 |
.bind(user_id) |
| 54 |
.bind(format!("OTA App {}", slug)) |
| 55 |
.bind(&key_hash) |
| 56 |
.bind(key_prefix) |
| 57 |
.bind(slug) |
| 58 |
.fetch_one(pool) |
| 59 |
.await |
| 60 |
.expect("Failed to create sync app"); |
| 61 |
|
| 62 |
(app_id, api_key) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
async fn create_sync_app(pool: &PgPool, user_id: UserId) -> (SyncAppId, String) { |
| 67 |
let api_key = format!("test-ota-key-{}", uuid::Uuid::new_v4()); |
| 68 |
let key_hash = crate::harness::hash_api_key(&api_key); |
| 69 |
let key_prefix = &api_key[..8]; |
| 70 |
let app_id: SyncAppId = sqlx::query_scalar( |
| 71 |
"INSERT INTO sync_apps (creator_id, name, api_key_hash, api_key_prefix) VALUES ($1, 'OTA App', $2, $3) RETURNING id", |
| 72 |
) |
| 73 |
.bind(user_id) |
| 74 |
.bind(&key_hash) |
| 75 |
.bind(key_prefix) |
| 76 |
.fetch_one(pool) |
| 77 |
.await |
| 78 |
.expect("Failed to create sync app"); |
| 79 |
|
| 80 |
(app_id, api_key) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
async fn setup_authenticated(h: &mut TestHarness) -> (SyncAppId, String) { |
| 85 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 86 |
let (app_id, api_key) = create_sync_app(&h.db, user_id).await; |
| 87 |
|
| 88 |
let resp = h |
| 89 |
.client |
| 90 |
.post_json( |
| 91 |
"/api/sync/auth", |
| 92 |
&json!({ |
| 93 |
"email": "ota@example.com", |
| 94 |
"password": "Password1!", |
| 95 |
"api_key": api_key, |
| 96 |
"key": "test-sdk-key", |
| 97 |
}) |
| 98 |
.to_string(), |
| 99 |
) |
| 100 |
.await; |
| 101 |
assert_eq!(resp.status, 200, "Auth failed: {}", resp.text); |
| 102 |
|
| 103 |
let auth: AuthResponse = resp.json(); |
| 104 |
h.client.set_bearer_token(&auth.token); |
| 105 |
|
| 106 |
(app_id, api_key) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
async fn harness_with_synckit_storage() -> TestHarness { |
| 111 |
TestHarness::with_synckit_storage().await |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
#[tokio::test] |
| 117 |
async fn set_app_slug() { |
| 118 |
let mut h = TestHarness::new().await; |
| 119 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 120 |
|
| 121 |
|
| 122 |
let resp = h |
| 123 |
.client |
| 124 |
.put_json( |
| 125 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 126 |
&json!({ "slug": "goingson" }).to_string(), |
| 127 |
) |
| 128 |
.await; |
| 129 |
assert_eq!(resp.status, 204, "Set slug failed: {}", resp.text); |
| 130 |
|
| 131 |
|
| 132 |
h.client.clear_bearer_token(); |
| 133 |
let resp = h |
| 134 |
.client |
| 135 |
.get("/api/sync/ota/goingson/linux/x86_64/0.0.1") |
| 136 |
.await; |
| 137 |
assert_eq!(resp.status, 204, "Slug lookup should work: {}", resp.text); |
| 138 |
} |
| 139 |
|
| 140 |
#[tokio::test] |
| 141 |
async fn slug_validation() { |
| 142 |
let mut h = TestHarness::new().await; |
| 143 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 144 |
|
| 145 |
|
| 146 |
let resp = h |
| 147 |
.client |
| 148 |
.put_json( |
| 149 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 150 |
&json!({ "slug": "ab" }).to_string(), |
| 151 |
) |
| 152 |
.await; |
| 153 |
assert_eq!(resp.status, 400, "Should reject 2-char slug"); |
| 154 |
|
| 155 |
|
| 156 |
let resp = h |
| 157 |
.client |
| 158 |
.put_json( |
| 159 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 160 |
&json!({ "slug": "GoingsOn" }).to_string(), |
| 161 |
) |
| 162 |
.await; |
| 163 |
assert_eq!(resp.status, 400, "Should reject uppercase"); |
| 164 |
|
| 165 |
|
| 166 |
let resp = h |
| 167 |
.client |
| 168 |
.put_json( |
| 169 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 170 |
&json!({ "slug": "my_app!" }).to_string(), |
| 171 |
) |
| 172 |
.await; |
| 173 |
assert_eq!(resp.status, 400, "Should reject special chars"); |
| 174 |
|
| 175 |
|
| 176 |
let resp = h |
| 177 |
.client |
| 178 |
.put_json( |
| 179 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 180 |
&json!({ "slug": "-myapp" }).to_string(), |
| 181 |
) |
| 182 |
.await; |
| 183 |
assert_eq!(resp.status, 400, "Should reject leading hyphen"); |
| 184 |
|
| 185 |
|
| 186 |
let resp = h |
| 187 |
.client |
| 188 |
.put_json( |
| 189 |
&format!("/api/sync/ota/apps/{}/slug", app_id), |
| 190 |
&json!({ "slug": "my-app" }).to_string(), |
| 191 |
) |
| 192 |
.await; |
| 193 |
assert_eq!(resp.status, 204, "Valid slug should work: {}", resp.text); |
| 194 |
} |
| 195 |
|
| 196 |
#[tokio::test] |
| 197 |
async fn slug_uniqueness() { |
| 198 |
let mut h = TestHarness::new().await; |
| 199 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 200 |
let (app1_id, api_key) = create_sync_app(&h.db, user_id).await; |
| 201 |
|
| 202 |
|
| 203 |
let resp = h |
| 204 |
.client |
| 205 |
.post_json( |
| 206 |
"/api/sync/auth", |
| 207 |
&json!({ |
| 208 |
"email": "ota@example.com", |
| 209 |
"password": "Password1!", |
| 210 |
"api_key": api_key, |
| 211 |
"key": "test-sdk-key", |
| 212 |
}) |
| 213 |
.to_string(), |
| 214 |
) |
| 215 |
.await; |
| 216 |
let auth: AuthResponse = resp.json(); |
| 217 |
h.client.set_bearer_token(&auth.token); |
| 218 |
|
| 219 |
|
| 220 |
let resp = h |
| 221 |
.client |
| 222 |
.put_json( |
| 223 |
&format!("/api/sync/ota/apps/{}/slug", app1_id), |
| 224 |
&json!({ "slug": "unique-slug" }).to_string(), |
| 225 |
) |
| 226 |
.await; |
| 227 |
assert_eq!(resp.status, 204); |
| 228 |
|
| 229 |
|
| 230 |
let api_key2 = "test-ota-key-second"; |
| 231 |
let key_hash2 = crate::harness::hash_api_key(api_key2); |
| 232 |
let key_prefix2 = &api_key2[..8]; |
| 233 |
let app2_id: SyncAppId = sqlx::query_scalar( |
| 234 |
"INSERT INTO sync_apps (creator_id, name, api_key_hash, api_key_prefix) VALUES ($1, 'Second', $2, $3) RETURNING id", |
| 235 |
) |
| 236 |
.bind(user_id) |
| 237 |
.bind(&key_hash2) |
| 238 |
.bind(key_prefix2) |
| 239 |
.fetch_one(&h.db) |
| 240 |
.await |
| 241 |
.unwrap(); |
| 242 |
|
| 243 |
|
| 244 |
let resp = h |
| 245 |
.client |
| 246 |
.post_json( |
| 247 |
"/api/sync/auth", |
| 248 |
&json!({ |
| 249 |
"email": "ota@example.com", |
| 250 |
"password": "Password1!", |
| 251 |
"api_key": api_key2, |
| 252 |
"key": "test-sdk-key", |
| 253 |
}) |
| 254 |
.to_string(), |
| 255 |
) |
| 256 |
.await; |
| 257 |
let auth2: AuthResponse = resp.json(); |
| 258 |
h.client.set_bearer_token(&auth2.token); |
| 259 |
|
| 260 |
let resp = h |
| 261 |
.client |
| 262 |
.put_json( |
| 263 |
&format!("/api/sync/ota/apps/{}/slug", app2_id), |
| 264 |
&json!({ "slug": "unique-slug" }).to_string(), |
| 265 |
) |
| 266 |
.await; |
| 267 |
assert_eq!(resp.status, 500, "Duplicate slug should fail: {}", resp.text); |
| 268 |
} |
| 269 |
|
| 270 |
#[tokio::test] |
| 271 |
async fn create_and_list_releases() { |
| 272 |
let mut h = TestHarness::new().await; |
| 273 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 274 |
|
| 275 |
|
| 276 |
let resp = h |
| 277 |
.client |
| 278 |
.post_json( |
| 279 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 280 |
&json!({ |
| 281 |
"version": "0.2.1", |
| 282 |
"notes": "Bug fixes", |
| 283 |
"signature": "dW50cnVzdGVkIGNvbW1lbnQ6..." |
| 284 |
}) |
| 285 |
.to_string(), |
| 286 |
) |
| 287 |
.await; |
| 288 |
assert_eq!(resp.status, 201, "Create release failed: {}", resp.text); |
| 289 |
let release: ReleaseResponse = resp.json(); |
| 290 |
assert_eq!(release.version, "0.2.1"); |
| 291 |
|
| 292 |
|
| 293 |
let resp = h |
| 294 |
.client |
| 295 |
.get(&format!("/api/sync/ota/apps/{}/releases", app_id)) |
| 296 |
.await; |
| 297 |
assert_eq!(resp.status, 200); |
| 298 |
let releases: Vec<ReleaseResponse> = resp.json(); |
| 299 |
assert_eq!(releases.len(), 1); |
| 300 |
assert_eq!(releases[0].version, "0.2.1"); |
| 301 |
} |
| 302 |
|
| 303 |
#[tokio::test] |
| 304 |
async fn version_validation() { |
| 305 |
let mut h = TestHarness::new().await; |
| 306 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 307 |
|
| 308 |
|
| 309 |
let resp = h |
| 310 |
.client |
| 311 |
.post_json( |
| 312 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 313 |
&json!({ "version": "not-semver", "notes": "", "signature": "" }).to_string(), |
| 314 |
) |
| 315 |
.await; |
| 316 |
assert_eq!(resp.status, 400, "Should reject non-semver"); |
| 317 |
|
| 318 |
|
| 319 |
let resp = h |
| 320 |
.client |
| 321 |
.post_json( |
| 322 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 323 |
&json!({ "version": "1.2", "notes": "", "signature": "" }).to_string(), |
| 324 |
) |
| 325 |
.await; |
| 326 |
assert_eq!(resp.status, 400, "Should reject incomplete semver"); |
| 327 |
} |
| 328 |
|
| 329 |
#[tokio::test] |
| 330 |
async fn duplicate_version() { |
| 331 |
let mut h = TestHarness::new().await; |
| 332 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 333 |
|
| 334 |
|
| 335 |
let resp = h |
| 336 |
.client |
| 337 |
.post_json( |
| 338 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 339 |
&json!({ "version": "1.0.0", "notes": "first" }).to_string(), |
| 340 |
) |
| 341 |
.await; |
| 342 |
assert_eq!(resp.status, 201); |
| 343 |
|
| 344 |
|
| 345 |
let resp = h |
| 346 |
.client |
| 347 |
.post_json( |
| 348 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 349 |
&json!({ "version": "1.0.0", "notes": "duplicate" }).to_string(), |
| 350 |
) |
| 351 |
.await; |
| 352 |
assert_eq!(resp.status, 409, "Duplicate version should return 409 Conflict: {}", resp.text); |
| 353 |
} |
| 354 |
|
| 355 |
#[tokio::test] |
| 356 |
async fn upload_artifact() { |
| 357 |
let mut h = harness_with_synckit_storage().await; |
| 358 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 359 |
|
| 360 |
|
| 361 |
let resp = h |
| 362 |
.client |
| 363 |
.post_json( |
| 364 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 365 |
&json!({ |
| 366 |
"version": "0.3.0", |
| 367 |
"notes": "New release", |
| 368 |
"signature": "sig123" |
| 369 |
}) |
| 370 |
.to_string(), |
| 371 |
) |
| 372 |
.await; |
| 373 |
assert_eq!(resp.status, 201); |
| 374 |
let release: ReleaseResponse = resp.json(); |
| 375 |
|
| 376 |
|
| 377 |
let resp = h |
| 378 |
.client |
| 379 |
.post_json( |
| 380 |
&format!( |
| 381 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 382 |
app_id, release.id |
| 383 |
), |
| 384 |
&json!({ |
| 385 |
"target": "linux", |
| 386 |
"arch": "x86_64", |
| 387 |
"file_size": 12345678 |
| 388 |
}) |
| 389 |
.to_string(), |
| 390 |
) |
| 391 |
.await; |
| 392 |
assert_eq!(resp.status, 201, "Upload artifact failed: {}", resp.text); |
| 393 |
let upload: UploadArtifactResponse = resp.json(); |
| 394 |
assert!(!upload.upload_url.is_empty()); |
| 395 |
} |
| 396 |
|
| 397 |
#[tokio::test] |
| 398 |
async fn updater_check_newer_version() { |
| 399 |
let mut h = harness_with_synckit_storage().await; |
| 400 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 401 |
let (app_id, api_key) = create_sync_app_with_slug(&h.db, user_id, "testapp").await; |
| 402 |
|
| 403 |
|
| 404 |
let resp = h |
| 405 |
.client |
| 406 |
.post_json( |
| 407 |
"/api/sync/auth", |
| 408 |
&json!({ |
| 409 |
"email": "ota@example.com", |
| 410 |
"password": "Password1!", |
| 411 |
"api_key": api_key, |
| 412 |
"key": "test-sdk-key", |
| 413 |
}) |
| 414 |
.to_string(), |
| 415 |
) |
| 416 |
.await; |
| 417 |
let auth: AuthResponse = resp.json(); |
| 418 |
h.client.set_bearer_token(&auth.token); |
| 419 |
|
| 420 |
|
| 421 |
let resp = h |
| 422 |
.client |
| 423 |
.post_json( |
| 424 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 425 |
&json!({ |
| 426 |
"version": "1.2.0", |
| 427 |
"notes": "Big update", |
| 428 |
"signature": "update-sig" |
| 429 |
}) |
| 430 |
.to_string(), |
| 431 |
) |
| 432 |
.await; |
| 433 |
assert_eq!(resp.status, 201); |
| 434 |
let release: ReleaseResponse = resp.json(); |
| 435 |
|
| 436 |
|
| 437 |
let resp = h |
| 438 |
.client |
| 439 |
.post_json( |
| 440 |
&format!( |
| 441 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 442 |
app_id, release.id |
| 443 |
), |
| 444 |
&json!({ "target": "linux", "arch": "x86_64", "file_size": 5000000 }).to_string(), |
| 445 |
) |
| 446 |
.await; |
| 447 |
assert_eq!(resp.status, 201); |
| 448 |
|
| 449 |
|
| 450 |
h.client.clear_bearer_token(); |
| 451 |
let resp = h |
| 452 |
.client |
| 453 |
.get("/api/sync/ota/testapp/linux/x86_64/1.0.0") |
| 454 |
.await; |
| 455 |
assert_eq!(resp.status, 200, "Should return update: {}", resp.text); |
| 456 |
let update: TauriUpdaterResponse = resp.json(); |
| 457 |
assert_eq!(update.version, "1.2.0"); |
| 458 |
assert_eq!(update.signature, "update-sig"); |
| 459 |
assert_eq!(update.notes, "Big update"); |
| 460 |
assert!(update.url.contains("/download/")); |
| 461 |
} |
| 462 |
|
| 463 |
#[tokio::test] |
| 464 |
async fn updater_check_no_update() { |
| 465 |
let mut h = harness_with_synckit_storage().await; |
| 466 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 467 |
let (app_id, api_key) = create_sync_app_with_slug(&h.db, user_id, "testapp").await; |
| 468 |
|
| 469 |
|
| 470 |
let resp = h |
| 471 |
.client |
| 472 |
.post_json( |
| 473 |
"/api/sync/auth", |
| 474 |
&json!({ |
| 475 |
"email": "ota@example.com", |
| 476 |
"password": "Password1!", |
| 477 |
"api_key": api_key, |
| 478 |
"key": "test-sdk-key", |
| 479 |
}) |
| 480 |
.to_string(), |
| 481 |
) |
| 482 |
.await; |
| 483 |
let auth: AuthResponse = resp.json(); |
| 484 |
h.client.set_bearer_token(&auth.token); |
| 485 |
|
| 486 |
let resp = h |
| 487 |
.client |
| 488 |
.post_json( |
| 489 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 490 |
&json!({ "version": "1.0.0", "notes": "", "signature": "sig" }).to_string(), |
| 491 |
) |
| 492 |
.await; |
| 493 |
assert_eq!(resp.status, 201); |
| 494 |
let release: ReleaseResponse = resp.json(); |
| 495 |
|
| 496 |
let resp = h |
| 497 |
.client |
| 498 |
.post_json( |
| 499 |
&format!( |
| 500 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 501 |
app_id, release.id |
| 502 |
), |
| 503 |
&json!({ "target": "linux", "arch": "x86_64", "file_size": 1000 }).to_string(), |
| 504 |
) |
| 505 |
.await; |
| 506 |
assert_eq!(resp.status, 201); |
| 507 |
|
| 508 |
|
| 509 |
h.client.clear_bearer_token(); |
| 510 |
let resp = h |
| 511 |
.client |
| 512 |
.get("/api/sync/ota/testapp/linux/x86_64/1.0.0") |
| 513 |
.await; |
| 514 |
assert_eq!(resp.status, 204, "Same version = no update"); |
| 515 |
|
| 516 |
|
| 517 |
let resp = h |
| 518 |
.client |
| 519 |
.get("/api/sync/ota/testapp/linux/x86_64/2.0.0") |
| 520 |
.await; |
| 521 |
assert_eq!(resp.status, 204, "Newer version = no update"); |
| 522 |
} |
| 523 |
|
| 524 |
#[tokio::test] |
| 525 |
async fn updater_check_missing_platform() { |
| 526 |
let mut h = harness_with_synckit_storage().await; |
| 527 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 528 |
let (app_id, api_key) = create_sync_app_with_slug(&h.db, user_id, "testapp").await; |
| 529 |
|
| 530 |
let resp = h |
| 531 |
.client |
| 532 |
.post_json( |
| 533 |
"/api/sync/auth", |
| 534 |
&json!({ |
| 535 |
"email": "ota@example.com", |
| 536 |
"password": "Password1!", |
| 537 |
"api_key": api_key, |
| 538 |
"key": "test-sdk-key", |
| 539 |
}) |
| 540 |
.to_string(), |
| 541 |
) |
| 542 |
.await; |
| 543 |
let auth: AuthResponse = resp.json(); |
| 544 |
h.client.set_bearer_token(&auth.token); |
| 545 |
|
| 546 |
|
| 547 |
let resp = h |
| 548 |
.client |
| 549 |
.post_json( |
| 550 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 551 |
&json!({ "version": "2.0.0", "notes": "", "signature": "s" }).to_string(), |
| 552 |
) |
| 553 |
.await; |
| 554 |
assert_eq!(resp.status, 201); |
| 555 |
let release: ReleaseResponse = resp.json(); |
| 556 |
|
| 557 |
let resp = h |
| 558 |
.client |
| 559 |
.post_json( |
| 560 |
&format!( |
| 561 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 562 |
app_id, release.id |
| 563 |
), |
| 564 |
&json!({ "target": "linux", "arch": "x86_64", "file_size": 1000 }).to_string(), |
| 565 |
) |
| 566 |
.await; |
| 567 |
assert_eq!(resp.status, 201); |
| 568 |
|
| 569 |
|
| 570 |
h.client.clear_bearer_token(); |
| 571 |
let resp = h |
| 572 |
.client |
| 573 |
.get("/api/sync/ota/testapp/darwin/aarch64/1.0.0") |
| 574 |
.await; |
| 575 |
assert_eq!( |
| 576 |
resp.status, 204, |
| 577 |
"Missing platform artifact should return 204" |
| 578 |
); |
| 579 |
} |
| 580 |
|
| 581 |
#[tokio::test] |
| 582 |
async fn artifact_download_redirect() { |
| 583 |
let mut h = harness_with_synckit_storage().await; |
| 584 |
let user_id = h.signup("otauser", "ota@example.com", "Password1!").await; |
| 585 |
let (app_id, api_key) = create_sync_app_with_slug(&h.db, user_id, "dlapp").await; |
| 586 |
|
| 587 |
let resp = h |
| 588 |
.client |
| 589 |
.post_json( |
| 590 |
"/api/sync/auth", |
| 591 |
&json!({ |
| 592 |
"email": "ota@example.com", |
| 593 |
"password": "Password1!", |
| 594 |
"api_key": api_key, |
| 595 |
"key": "test-sdk-key", |
| 596 |
}) |
| 597 |
.to_string(), |
| 598 |
) |
| 599 |
.await; |
| 600 |
let auth: AuthResponse = resp.json(); |
| 601 |
h.client.set_bearer_token(&auth.token); |
| 602 |
|
| 603 |
let resp = h |
| 604 |
.client |
| 605 |
.post_json( |
| 606 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 607 |
&json!({ "version": "1.0.0", "notes": "", "signature": "s" }).to_string(), |
| 608 |
) |
| 609 |
.await; |
| 610 |
assert_eq!(resp.status, 201); |
| 611 |
let release: ReleaseResponse = resp.json(); |
| 612 |
|
| 613 |
let resp = h |
| 614 |
.client |
| 615 |
.post_json( |
| 616 |
&format!( |
| 617 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 618 |
app_id, release.id |
| 619 |
), |
| 620 |
&json!({ "target": "linux", "arch": "x86_64", "file_size": 999 }).to_string(), |
| 621 |
) |
| 622 |
.await; |
| 623 |
assert_eq!(resp.status, 201); |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
let _s3_key = format!("ota/{}/1.0.0/linux/x86_64/artifact", app_id); |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
} |
| 650 |
|
| 651 |
#[tokio::test] |
| 652 |
async fn delete_release_cascades() { |
| 653 |
let mut h = harness_with_synckit_storage().await; |
| 654 |
let (app_id, _) = setup_authenticated(&mut h).await; |
| 655 |
|
| 656 |
|
| 657 |
let resp = h |
| 658 |
.client |
| 659 |
.post_json( |
| 660 |
&format!("/api/sync/ota/apps/{}/releases", app_id), |
| 661 |
&json!({ "version": "1.0.0", "notes": "", "signature": "" }).to_string(), |
| 662 |
) |
| 663 |
.await; |
| 664 |
assert_eq!(resp.status, 201); |
| 665 |
let release: ReleaseResponse = resp.json(); |
| 666 |
|
| 667 |
let resp = h |
| 668 |
.client |
| 669 |
.post_json( |
| 670 |
&format!( |
| 671 |
"/api/sync/ota/apps/{}/releases/{}/artifacts", |
| 672 |
app_id, release.id |
| 673 |
), |
| 674 |
&json!({ "target": "linux", "arch": "x86_64", "file_size": 500 }).to_string(), |
| 675 |
) |
| 676 |
.await; |
| 677 |
assert_eq!(resp.status, 201); |
| 678 |
|
| 679 |
|
| 680 |
let resp = h |
| 681 |
.client |
| 682 |
.get(&format!("/api/sync/ota/apps/{}/releases", app_id)) |
| 683 |
.await; |
| 684 |
let releases: Vec<ReleaseResponse> = resp.json(); |
| 685 |
assert_eq!(releases.len(), 1); |
| 686 |
|
| 687 |
|
| 688 |
let resp = h |
| 689 |
.client |
| 690 |
.delete(&format!( |
| 691 |
"/api/sync/ota/apps/{}/releases/{}", |
| 692 |
app_id, release.id |
| 693 |
)) |
| 694 |
.await; |
| 695 |
assert_eq!(resp.status, 204, "Delete failed: {}", resp.text); |
| 696 |
|
| 697 |
|
| 698 |
let resp = h |
| 699 |
.client |
| 700 |
.get(&format!("/api/sync/ota/apps/{}/releases", app_id)) |
| 701 |
.await; |
| 702 |
let releases: Vec<ReleaseResponse> = resp.json(); |
| 703 |
assert_eq!(releases.len(), 0); |
| 704 |
|
| 705 |
|
| 706 |
let count: (i64,) = |
| 707 |
sqlx::query_as("SELECT COUNT(*) FROM ota_artifacts WHERE release_id = $1") |
| 708 |
.bind(release.id) |
| 709 |
.fetch_one(&h.db) |
| 710 |
.await |
| 711 |
.unwrap(); |
| 712 |
assert_eq!(count.0, 0, "Artifacts should be cascade-deleted"); |
| 713 |
} |
| 714 |
|
| 715 |
#[tokio::test] |
| 716 |
async fn ownership_check() { |
| 717 |
let mut h = TestHarness::new().await; |
| 718 |
|
| 719 |
|
| 720 |
let user_a = h.signup("usera", "a@example.com", "Password1!").await; |
| 721 |
let (app_a_id, _) = create_sync_app(&h.db, user_a).await; |
| 722 |
|
| 723 |
|
| 724 |
let user_b = h.signup("userb", "b@example.com", "Password1!").await; |
| 725 |
let (_, api_key_b) = create_sync_app(&h.db, user_b).await; |
| 726 |
|
| 727 |
let resp = h |
| 728 |
.client |
| 729 |
.post_json( |
| 730 |
"/api/sync/auth", |
| 731 |
&json!({ |
| 732 |
"email": "b@example.com", |
| 733 |
"password": "Password1!", |
| 734 |
"api_key": api_key_b, |
| 735 |
"key": "test-sdk-key", |
| 736 |
}) |
| 737 |
.to_string(), |
| 738 |
) |
| 739 |
.await; |
| 740 |
assert_eq!(resp.status, 200); |
| 741 |
let auth_b: AuthResponse = resp.json(); |
| 742 |
h.client.set_bearer_token(&auth_b.token); |
| 743 |
|
| 744 |
|
| 745 |
let resp = h |
| 746 |
.client |
| 747 |
.put_json( |
| 748 |
&format!("/api/sync/ota/apps/{}/slug", app_a_id), |
| 749 |
&json!({ "slug": "stolen" }).to_string(), |
| 750 |
) |
| 751 |
.await; |
| 752 |
assert_eq!(resp.status, 403, "Should deny cross-user access"); |
| 753 |
|
| 754 |
|
| 755 |
let resp = h |
| 756 |
.client |
| 757 |
.post_json( |
| 758 |
&format!("/api/sync/ota/apps/{}/releases", app_a_id), |
| 759 |
&json!({ "version": "9.9.9", "notes": "hack" }).to_string(), |
| 760 |
) |
| 761 |
.await; |
| 762 |
assert_eq!(resp.status, 403, "Should deny cross-user release creation"); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
#[tokio::test] |
| 771 |
async fn is_s3_key_live_covers_ota_artifacts_in_synckit_bucket() { |
| 772 |
let mut h = TestHarness::new().await; |
| 773 |
let user_id = h.signup("otaliveuser", "otalive@example.com", "Password1!").await; |
| 774 |
let (app_id, _key) = create_sync_app(&h.db, user_id).await; |
| 775 |
|
| 776 |
let release_id: OtaReleaseId = sqlx::query_scalar( |
| 777 |
"INSERT INTO ota_releases (app_id, version, notes, signature) VALUES ($1, '1.0.0', '', 'sig') RETURNING id", |
| 778 |
) |
| 779 |
.bind(app_id) |
| 780 |
.fetch_one(&h.db) |
| 781 |
.await |
| 782 |
.unwrap(); |
| 783 |
|
| 784 |
let key = format!("ota/{}/1.0.0/darwin/aarch64/app.tar.gz", app_id); |
| 785 |
sqlx::query("INSERT INTO ota_artifacts (release_id, target, arch, s3_key, file_size) VALUES ($1, 'darwin', 'aarch64', $2, 1234)") |
| 786 |
.bind(release_id) |
| 787 |
.bind(&key) |
| 788 |
.execute(&h.db) |
| 789 |
.await |
| 790 |
.unwrap(); |
| 791 |
|
| 792 |
|
| 793 |
assert!( |
| 794 |
makenotwork::db::pending_s3_deletions::is_s3_key_live(&h.db, "synckit", &key) |
| 795 |
.await |
| 796 |
.unwrap(), |
| 797 |
"live OTA artifact in the synckit bucket must be reported live" |
| 798 |
); |
| 799 |
|
| 800 |
assert!( |
| 801 |
!makenotwork::db::pending_s3_deletions::is_s3_key_live(&h.db, "synckit", "ota/ghost/0.0.0/x/y/z") |
| 802 |
.await |
| 803 |
.unwrap(), |
| 804 |
"an unreferenced synckit key must not be reported live" |
| 805 |
); |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
#[tokio::test] |
| 813 |
async fn is_s3_key_live_covers_main_bucket() { |
| 814 |
let mut h = TestHarness::new().await; |
| 815 |
let user_id = h.signup("mainliveuser", "mainlive@example.com", "Password1!").await; |
| 816 |
|
| 817 |
let key = format!("{user_id}/media/cover.png"); |
| 818 |
sqlx::query( |
| 819 |
"INSERT INTO media_files (user_id, filename, s3_key, content_type, media_type) \ |
| 820 |
VALUES ($1, 'cover.png', $2, 'image/png', 'image')", |
| 821 |
) |
| 822 |
.bind(user_id) |
| 823 |
.bind(&key) |
| 824 |
.execute(&h.db) |
| 825 |
.await |
| 826 |
.unwrap(); |
| 827 |
|
| 828 |
assert!( |
| 829 |
makenotwork::db::pending_s3_deletions::is_s3_key_live(&h.db, "main", &key) |
| 830 |
.await |
| 831 |
.unwrap(), |
| 832 |
"a live media_files key must be reported live in the main bucket" |
| 833 |
); |
| 834 |
assert!( |
| 835 |
!makenotwork::db::pending_s3_deletions::is_s3_key_live(&h.db, "main", "nobody/media/ghost.mp3") |
| 836 |
.await |
| 837 |
.unwrap(), |
| 838 |
"an unreferenced main-bucket key must not be reported live" |
| 839 |
); |
| 840 |
} |
| 841 |
|