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!(prefs_arr[0]["$type"], "app.bsky.actor.defs#adultContentPref"); 178} 179 180#[tokio::test] 181async fn test_put_preferences_replaces_all() { 182 let client = client(); 183 let base = base_url().await; 184 let (token, _did) = create_account_and_login(&client).await; 185 let prefs1 = json!({ 186 "preferences": [ 187 { 188 "$type": "app.bsky.actor.defs#adultContentPref", 189 "enabled": true 190 }, 191 { 192 "$type": "app.bsky.actor.defs#contentLabelPref", 193 "label": "nsfw", 194 "visibility": "warn" 195 } 196 ] 197 }); 198 client 199 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 200 .header("Authorization", format!("Bearer {}", token)) 201 .json(&prefs1) 202 .send() 203 .await 204 .unwrap(); 205 let prefs2 = json!({ 206 "preferences": [ 207 { 208 "$type": "app.bsky.actor.defs#threadViewPref", 209 "sort": "newest" 210 } 211 ] 212 }); 213 client 214 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 215 .header("Authorization", format!("Bearer {}", token)) 216 .json(&prefs2) 217 .send() 218 .await 219 .unwrap(); 220 let resp = client 221 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 222 .header("Authorization", format!("Bearer {}", token)) 223 .send() 224 .await 225 .unwrap(); 226 assert_eq!(resp.status(), 200); 227 let body: Value = resp.json().await.unwrap(); 228 let prefs_arr = body["preferences"].as_array().unwrap(); 229 assert_eq!(prefs_arr.len(), 1); 230 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#threadViewPref"); 231} 232 233#[tokio::test] 234async fn test_put_preferences_saved_feeds() { 235 let client = client(); 236 let base = base_url().await; 237 let (token, _did) = create_account_and_login(&client).await; 238 let prefs = json!({ 239 "preferences": [ 240 { 241 "$type": "app.bsky.actor.defs#savedFeedsPrefV2", 242 "items": [ 243 { 244 "type": "feed", 245 "value": "at://did:plc:example/app.bsky.feed.generator/my-feed", 246 "pinned": true 247 } 248 ] 249 } 250 ] 251 }); 252 let resp = client 253 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 254 .header("Authorization", format!("Bearer {}", token)) 255 .json(&prefs) 256 .send() 257 .await 258 .unwrap(); 259 assert_eq!(resp.status(), 200); 260 let resp = client 261 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 262 .header("Authorization", format!("Bearer {}", token)) 263 .send() 264 .await 265 .unwrap(); 266 assert_eq!(resp.status(), 200); 267 let body: Value = resp.json().await.unwrap(); 268 let prefs_arr = body["preferences"].as_array().unwrap(); 269 assert_eq!(prefs_arr.len(), 1); 270 let saved_feeds = &prefs_arr[0]; 271 assert_eq!(saved_feeds["$type"], "app.bsky.actor.defs#savedFeedsPrefV2"); 272 assert!(saved_feeds["items"].as_array().unwrap().len() == 1); 273} 274 275#[tokio::test] 276async fn test_put_preferences_muted_words() { 277 let client = client(); 278 let base = base_url().await; 279 let (token, _did) = create_account_and_login(&client).await; 280 let prefs = json!({ 281 "preferences": [ 282 { 283 "$type": "app.bsky.actor.defs#mutedWordsPref", 284 "items": [ 285 { 286 "value": "spoiler", 287 "targets": ["content", "tag"], 288 "actorTarget": "all" 289 } 290 ] 291 } 292 ] 293 }); 294 let resp = client 295 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 296 .header("Authorization", format!("Bearer {}", token)) 297 .json(&prefs) 298 .send() 299 .await 300 .unwrap(); 301 assert_eq!(resp.status(), 200); 302 let resp = client 303 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 304 .header("Authorization", format!("Bearer {}", token)) 305 .send() 306 .await 307 .unwrap(); 308 let body: Value = resp.json().await.unwrap(); 309 let prefs_arr = body["preferences"].as_array().unwrap(); 310 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#mutedWordsPref"); 311} 312 313#[tokio::test] 314async fn test_preferences_isolation_between_users() { 315 let client = client(); 316 let base = base_url().await; 317 let (token1, _did1) = create_account_and_login(&client).await; 318 let (token2, _did2) = create_account_and_login(&client).await; 319 let prefs1 = json!({ 320 "preferences": [ 321 { 322 "$type": "app.bsky.actor.defs#adultContentPref", 323 "enabled": true 324 } 325 ] 326 }); 327 client 328 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 329 .header("Authorization", format!("Bearer {}", token1)) 330 .json(&prefs1) 331 .send() 332 .await 333 .unwrap(); 334 let resp = client 335 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 336 .header("Authorization", format!("Bearer {}", token2)) 337 .send() 338 .await 339 .unwrap(); 340 assert_eq!(resp.status(), 200); 341 let body: Value = resp.json().await.unwrap(); 342 assert!(body["preferences"].as_array().unwrap().is_empty()); 343} 344 345#[tokio::test] 346async fn test_declared_age_pref_computed_from_birth_date() { 347 let client = client(); 348 let base = base_url().await; 349 let (token, _did) = create_account_and_login(&client).await; 350 let prefs = json!({ 351 "preferences": [ 352 { 353 "$type": "app.bsky.actor.defs#personalDetailsPref", 354 "birthDate": "1990-01-15" 355 } 356 ] 357 }); 358 let resp = client 359 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 360 .header("Authorization", format!("Bearer {}", token)) 361 .json(&prefs) 362 .send() 363 .await 364 .unwrap(); 365 assert_eq!(resp.status(), 200); 366 let get_resp = client 367 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 368 .header("Authorization", format!("Bearer {}", token)) 369 .send() 370 .await 371 .unwrap(); 372 assert_eq!(get_resp.status(), 200); 373 let body: Value = get_resp.json().await.unwrap(); 374 let prefs_arr = body["preferences"].as_array().unwrap(); 375 assert_eq!(prefs_arr.len(), 2); 376 let personal_details = prefs_arr 377 .iter() 378 .find(|p| p["$type"] == "app.bsky.actor.defs#personalDetailsPref"); 379 assert!(personal_details.is_some()); 380 assert_eq!(personal_details.unwrap()["birthDate"], "1990-01-15"); 381 let declared_age = prefs_arr 382 .iter() 383 .find(|p| p["$type"] == "app.bsky.actor.defs#declaredAgePref"); 384 assert!(declared_age.is_some()); 385 let declared_age = declared_age.unwrap(); 386 assert_eq!(declared_age["isOverAge13"], true); 387 assert_eq!(declared_age["isOverAge16"], true); 388 assert_eq!(declared_age["isOverAge18"], true); 389} 390 391#[tokio::test] 392async fn test_declared_age_pref_computed_under_18() { 393 let client = client(); 394 let base = base_url().await; 395 let (token, _did) = create_account_and_login(&client).await; 396 let current_year = chrono::Utc::now().format("%Y").to_string().parse::<i32>().unwrap(); 397 let birth_year = current_year - 15; 398 let prefs = json!({ 399 "preferences": [ 400 { 401 "$type": "app.bsky.actor.defs#personalDetailsPref", 402 "birthDate": format!("{}-06-15", birth_year) 403 } 404 ] 405 }); 406 let resp = client 407 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 408 .header("Authorization", format!("Bearer {}", token)) 409 .json(&prefs) 410 .send() 411 .await 412 .unwrap(); 413 assert_eq!(resp.status(), 200); 414 let get_resp = client 415 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 416 .header("Authorization", format!("Bearer {}", token)) 417 .send() 418 .await 419 .unwrap(); 420 assert_eq!(get_resp.status(), 200); 421 let body: Value = get_resp.json().await.unwrap(); 422 let prefs_arr = body["preferences"].as_array().unwrap(); 423 let declared_age = prefs_arr 424 .iter() 425 .find(|p| p["$type"] == "app.bsky.actor.defs#declaredAgePref"); 426 assert!(declared_age.is_some()); 427 let declared_age = declared_age.unwrap(); 428 assert_eq!(declared_age["isOverAge13"], true); 429 assert_eq!(declared_age["isOverAge16"], false); 430 assert_eq!(declared_age["isOverAge18"], false); 431}