this repo has no description
1mod common; 2use common::{base_url, client, create_account_and_login}; 3use serde_json::{Value, json}; 4 5#[tokio::test] 6async fn test_get_preferences_empty() { 7 let client = client(); 8 let base = base_url().await; 9 let (token, _did) = create_account_and_login(&client).await; 10 let resp = client 11 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 12 .header("Authorization", format!("Bearer {}", token)) 13 .send() 14 .await 15 .unwrap(); 16 assert_eq!(resp.status(), 200); 17 let body: Value = resp.json().await.unwrap(); 18 assert!(body.get("preferences").is_some()); 19 assert!(body["preferences"].as_array().unwrap().is_empty()); 20} 21 22#[tokio::test] 23async fn test_get_preferences_no_auth() { 24 let client = client(); 25 let base = base_url().await; 26 let resp = client 27 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 28 .send() 29 .await 30 .unwrap(); 31 assert_eq!(resp.status(), 401); 32} 33 34#[tokio::test] 35async fn test_put_preferences_success() { 36 let client = client(); 37 let base = base_url().await; 38 let (token, _did) = create_account_and_login(&client).await; 39 let prefs = json!({ 40 "preferences": [ 41 { 42 "$type": "app.bsky.actor.defs#adultContentPref", 43 "enabled": true 44 }, 45 { 46 "$type": "app.bsky.actor.defs#contentLabelPref", 47 "label": "nsfw", 48 "visibility": "warn" 49 } 50 ] 51 }); 52 let resp = client 53 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 54 .header("Authorization", format!("Bearer {}", token)) 55 .json(&prefs) 56 .send() 57 .await 58 .unwrap(); 59 assert_eq!(resp.status(), 200); 60 let resp = client 61 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 62 .header("Authorization", format!("Bearer {}", token)) 63 .send() 64 .await 65 .unwrap(); 66 assert_eq!(resp.status(), 200); 67 let body: Value = resp.json().await.unwrap(); 68 let prefs_arr = body["preferences"].as_array().unwrap(); 69 assert_eq!(prefs_arr.len(), 2); 70 let adult_pref = prefs_arr.iter().find(|p| { 71 p.get("$type").and_then(|t| t.as_str()) == Some("app.bsky.actor.defs#adultContentPref") 72 }); 73 assert!(adult_pref.is_some()); 74 assert_eq!(adult_pref.unwrap()["enabled"], true); 75} 76 77#[tokio::test] 78async fn test_put_preferences_no_auth() { 79 let client = client(); 80 let base = base_url().await; 81 let prefs = json!({ 82 "preferences": [] 83 }); 84 let resp = client 85 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 86 .json(&prefs) 87 .send() 88 .await 89 .unwrap(); 90 assert_eq!(resp.status(), 401); 91} 92 93#[tokio::test] 94async fn test_put_preferences_missing_type() { 95 let client = client(); 96 let base = base_url().await; 97 let (token, _did) = create_account_and_login(&client).await; 98 let prefs = json!({ 99 "preferences": [ 100 { 101 "enabled": true 102 } 103 ] 104 }); 105 let resp = client 106 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 107 .header("Authorization", format!("Bearer {}", token)) 108 .json(&prefs) 109 .send() 110 .await 111 .unwrap(); 112 assert_eq!(resp.status(), 400); 113 let body: Value = resp.json().await.unwrap(); 114 assert_eq!(body["error"], "InvalidRequest"); 115} 116 117#[tokio::test] 118async fn test_put_preferences_invalid_namespace() { 119 let client = client(); 120 let base = base_url().await; 121 let (token, _did) = create_account_and_login(&client).await; 122 let prefs = json!({ 123 "preferences": [ 124 { 125 "$type": "com.example.somePref", 126 "value": "test" 127 } 128 ] 129 }); 130 let resp = client 131 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 132 .header("Authorization", format!("Bearer {}", token)) 133 .json(&prefs) 134 .send() 135 .await 136 .unwrap(); 137 assert_eq!(resp.status(), 400); 138 let body: Value = resp.json().await.unwrap(); 139 assert_eq!(body["error"], "InvalidRequest"); 140} 141 142#[tokio::test] 143async fn test_put_preferences_read_only_silently_filtered() { 144 let client = client(); 145 let base = base_url().await; 146 let (token, _did) = create_account_and_login(&client).await; 147 let prefs = json!({ 148 "preferences": [ 149 { 150 "$type": "app.bsky.actor.defs#declaredAgePref", 151 "isOverAge18": true 152 }, 153 { 154 "$type": "app.bsky.actor.defs#adultContentPref", 155 "enabled": true 156 } 157 ] 158 }); 159 let resp = client 160 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 161 .header("Authorization", format!("Bearer {}", token)) 162 .json(&prefs) 163 .send() 164 .await 165 .unwrap(); 166 assert_eq!(resp.status(), 200); 167 let get_resp = client 168 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 169 .header("Authorization", format!("Bearer {}", token)) 170 .send() 171 .await 172 .unwrap(); 173 assert_eq!(get_resp.status(), 200); 174 let body: Value = get_resp.json().await.unwrap(); 175 let prefs_arr = body["preferences"].as_array().unwrap(); 176 assert_eq!(prefs_arr.len(), 1); 177 assert_eq!( 178 prefs_arr[0]["$type"], 179 "app.bsky.actor.defs#adultContentPref" 180 ); 181} 182 183#[tokio::test] 184async fn test_put_preferences_replaces_all() { 185 let client = client(); 186 let base = base_url().await; 187 let (token, _did) = create_account_and_login(&client).await; 188 let prefs1 = json!({ 189 "preferences": [ 190 { 191 "$type": "app.bsky.actor.defs#adultContentPref", 192 "enabled": true 193 }, 194 { 195 "$type": "app.bsky.actor.defs#contentLabelPref", 196 "label": "nsfw", 197 "visibility": "warn" 198 } 199 ] 200 }); 201 client 202 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 203 .header("Authorization", format!("Bearer {}", token)) 204 .json(&prefs1) 205 .send() 206 .await 207 .unwrap(); 208 let prefs2 = json!({ 209 "preferences": [ 210 { 211 "$type": "app.bsky.actor.defs#threadViewPref", 212 "sort": "newest" 213 } 214 ] 215 }); 216 client 217 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 218 .header("Authorization", format!("Bearer {}", token)) 219 .json(&prefs2) 220 .send() 221 .await 222 .unwrap(); 223 let resp = client 224 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 225 .header("Authorization", format!("Bearer {}", token)) 226 .send() 227 .await 228 .unwrap(); 229 assert_eq!(resp.status(), 200); 230 let body: Value = resp.json().await.unwrap(); 231 let prefs_arr = body["preferences"].as_array().unwrap(); 232 assert_eq!(prefs_arr.len(), 1); 233 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#threadViewPref"); 234} 235 236#[tokio::test] 237async fn test_put_preferences_saved_feeds() { 238 let client = client(); 239 let base = base_url().await; 240 let (token, _did) = create_account_and_login(&client).await; 241 let prefs = json!({ 242 "preferences": [ 243 { 244 "$type": "app.bsky.actor.defs#savedFeedsPrefV2", 245 "items": [ 246 { 247 "type": "feed", 248 "value": "at://did:plc:example/app.bsky.feed.generator/my-feed", 249 "pinned": true 250 } 251 ] 252 } 253 ] 254 }); 255 let resp = client 256 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 257 .header("Authorization", format!("Bearer {}", token)) 258 .json(&prefs) 259 .send() 260 .await 261 .unwrap(); 262 assert_eq!(resp.status(), 200); 263 let resp = client 264 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 265 .header("Authorization", format!("Bearer {}", token)) 266 .send() 267 .await 268 .unwrap(); 269 assert_eq!(resp.status(), 200); 270 let body: Value = resp.json().await.unwrap(); 271 let prefs_arr = body["preferences"].as_array().unwrap(); 272 assert_eq!(prefs_arr.len(), 1); 273 let saved_feeds = &prefs_arr[0]; 274 assert_eq!(saved_feeds["$type"], "app.bsky.actor.defs#savedFeedsPrefV2"); 275 assert!(saved_feeds["items"].as_array().unwrap().len() == 1); 276} 277 278#[tokio::test] 279async fn test_put_preferences_muted_words() { 280 let client = client(); 281 let base = base_url().await; 282 let (token, _did) = create_account_and_login(&client).await; 283 let prefs = json!({ 284 "preferences": [ 285 { 286 "$type": "app.bsky.actor.defs#mutedWordsPref", 287 "items": [ 288 { 289 "value": "spoiler", 290 "targets": ["content", "tag"], 291 "actorTarget": "all" 292 } 293 ] 294 } 295 ] 296 }); 297 let resp = client 298 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 299 .header("Authorization", format!("Bearer {}", token)) 300 .json(&prefs) 301 .send() 302 .await 303 .unwrap(); 304 assert_eq!(resp.status(), 200); 305 let resp = client 306 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 307 .header("Authorization", format!("Bearer {}", token)) 308 .send() 309 .await 310 .unwrap(); 311 let body: Value = resp.json().await.unwrap(); 312 let prefs_arr = body["preferences"].as_array().unwrap(); 313 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#mutedWordsPref"); 314} 315 316#[tokio::test] 317async fn test_preferences_isolation_between_users() { 318 let client = client(); 319 let base = base_url().await; 320 let (token1, _did1) = create_account_and_login(&client).await; 321 let (token2, _did2) = create_account_and_login(&client).await; 322 let prefs1 = json!({ 323 "preferences": [ 324 { 325 "$type": "app.bsky.actor.defs#adultContentPref", 326 "enabled": true 327 } 328 ] 329 }); 330 client 331 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 332 .header("Authorization", format!("Bearer {}", token1)) 333 .json(&prefs1) 334 .send() 335 .await 336 .unwrap(); 337 let resp = client 338 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 339 .header("Authorization", format!("Bearer {}", token2)) 340 .send() 341 .await 342 .unwrap(); 343 assert_eq!(resp.status(), 200); 344 let body: Value = resp.json().await.unwrap(); 345 assert!(body["preferences"].as_array().unwrap().is_empty()); 346} 347 348#[tokio::test] 349async fn test_declared_age_pref_computed_from_birth_date() { 350 let client = client(); 351 let base = base_url().await; 352 let (token, _did) = create_account_and_login(&client).await; 353 let prefs = json!({ 354 "preferences": [ 355 { 356 "$type": "app.bsky.actor.defs#personalDetailsPref", 357 "birthDate": "1990-01-15" 358 } 359 ] 360 }); 361 let resp = client 362 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 363 .header("Authorization", format!("Bearer {}", token)) 364 .json(&prefs) 365 .send() 366 .await 367 .unwrap(); 368 assert_eq!(resp.status(), 200); 369 let get_resp = client 370 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 371 .header("Authorization", format!("Bearer {}", token)) 372 .send() 373 .await 374 .unwrap(); 375 assert_eq!(get_resp.status(), 200); 376 let body: Value = get_resp.json().await.unwrap(); 377 let prefs_arr = body["preferences"].as_array().unwrap(); 378 assert_eq!(prefs_arr.len(), 2); 379 let personal_details = prefs_arr 380 .iter() 381 .find(|p| p["$type"] == "app.bsky.actor.defs#personalDetailsPref"); 382 assert!(personal_details.is_some()); 383 assert_eq!(personal_details.unwrap()["birthDate"], "1990-01-15"); 384 let declared_age = prefs_arr 385 .iter() 386 .find(|p| p["$type"] == "app.bsky.actor.defs#declaredAgePref"); 387 assert!(declared_age.is_some()); 388 let declared_age = declared_age.unwrap(); 389 assert_eq!(declared_age["isOverAge13"], true); 390 assert_eq!(declared_age["isOverAge16"], true); 391 assert_eq!(declared_age["isOverAge18"], true); 392} 393 394#[tokio::test] 395async fn test_declared_age_pref_computed_under_18() { 396 let client = client(); 397 let base = base_url().await; 398 let (token, _did) = create_account_and_login(&client).await; 399 let current_year = chrono::Utc::now() 400 .format("%Y") 401 .to_string() 402 .parse::<i32>() 403 .unwrap(); 404 let birth_year = current_year - 15; 405 let prefs = json!({ 406 "preferences": [ 407 { 408 "$type": "app.bsky.actor.defs#personalDetailsPref", 409 "birthDate": format!("{}-06-15", birth_year) 410 } 411 ] 412 }); 413 let resp = client 414 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 415 .header("Authorization", format!("Bearer {}", token)) 416 .json(&prefs) 417 .send() 418 .await 419 .unwrap(); 420 assert_eq!(resp.status(), 200); 421 let get_resp = client 422 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 423 .header("Authorization", format!("Bearer {}", token)) 424 .send() 425 .await 426 .unwrap(); 427 assert_eq!(get_resp.status(), 200); 428 let body: Value = get_resp.json().await.unwrap(); 429 let prefs_arr = body["preferences"].as_array().unwrap(); 430 let declared_age = prefs_arr 431 .iter() 432 .find(|p| p["$type"] == "app.bsky.actor.defs#declaredAgePref"); 433 assert!(declared_age.is_some()); 434 let declared_age = declared_age.unwrap(); 435 assert_eq!(declared_age["isOverAge13"], true); 436 assert_eq!(declared_age["isOverAge16"], false); 437 assert_eq!(declared_age["isOverAge18"], false); 438}