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_rejected() { 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 }); 155 let resp = client 156 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 157 .header("Authorization", format!("Bearer {}", token)) 158 .json(&prefs) 159 .send() 160 .await 161 .unwrap(); 162 assert_eq!(resp.status(), 400); 163 let body: Value = resp.json().await.unwrap(); 164 assert_eq!(body["error"], "InvalidRequest"); 165} 166 167#[tokio::test] 168async fn test_put_preferences_replaces_all() { 169 let client = client(); 170 let base = base_url().await; 171 let (token, _did) = create_account_and_login(&client).await; 172 let prefs1 = json!({ 173 "preferences": [ 174 { 175 "$type": "app.bsky.actor.defs#adultContentPref", 176 "enabled": true 177 }, 178 { 179 "$type": "app.bsky.actor.defs#contentLabelPref", 180 "label": "nsfw", 181 "visibility": "warn" 182 } 183 ] 184 }); 185 client 186 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 187 .header("Authorization", format!("Bearer {}", token)) 188 .json(&prefs1) 189 .send() 190 .await 191 .unwrap(); 192 let prefs2 = json!({ 193 "preferences": [ 194 { 195 "$type": "app.bsky.actor.defs#threadViewPref", 196 "sort": "newest" 197 } 198 ] 199 }); 200 client 201 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 202 .header("Authorization", format!("Bearer {}", token)) 203 .json(&prefs2) 204 .send() 205 .await 206 .unwrap(); 207 let resp = client 208 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 209 .header("Authorization", format!("Bearer {}", token)) 210 .send() 211 .await 212 .unwrap(); 213 assert_eq!(resp.status(), 200); 214 let body: Value = resp.json().await.unwrap(); 215 let prefs_arr = body["preferences"].as_array().unwrap(); 216 assert_eq!(prefs_arr.len(), 1); 217 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#threadViewPref"); 218} 219 220#[tokio::test] 221async fn test_put_preferences_saved_feeds() { 222 let client = client(); 223 let base = base_url().await; 224 let (token, _did) = create_account_and_login(&client).await; 225 let prefs = json!({ 226 "preferences": [ 227 { 228 "$type": "app.bsky.actor.defs#savedFeedsPrefV2", 229 "items": [ 230 { 231 "type": "feed", 232 "value": "at://did:plc:example/app.bsky.feed.generator/my-feed", 233 "pinned": true 234 } 235 ] 236 } 237 ] 238 }); 239 let resp = client 240 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 241 .header("Authorization", format!("Bearer {}", token)) 242 .json(&prefs) 243 .send() 244 .await 245 .unwrap(); 246 assert_eq!(resp.status(), 200); 247 let resp = client 248 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 249 .header("Authorization", format!("Bearer {}", token)) 250 .send() 251 .await 252 .unwrap(); 253 assert_eq!(resp.status(), 200); 254 let body: Value = resp.json().await.unwrap(); 255 let prefs_arr = body["preferences"].as_array().unwrap(); 256 assert_eq!(prefs_arr.len(), 1); 257 let saved_feeds = &prefs_arr[0]; 258 assert_eq!(saved_feeds["$type"], "app.bsky.actor.defs#savedFeedsPrefV2"); 259 assert!(saved_feeds["items"].as_array().unwrap().len() == 1); 260} 261 262#[tokio::test] 263async fn test_put_preferences_muted_words() { 264 let client = client(); 265 let base = base_url().await; 266 let (token, _did) = create_account_and_login(&client).await; 267 let prefs = json!({ 268 "preferences": [ 269 { 270 "$type": "app.bsky.actor.defs#mutedWordsPref", 271 "items": [ 272 { 273 "value": "spoiler", 274 "targets": ["content", "tag"], 275 "actorTarget": "all" 276 } 277 ] 278 } 279 ] 280 }); 281 let resp = client 282 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 283 .header("Authorization", format!("Bearer {}", token)) 284 .json(&prefs) 285 .send() 286 .await 287 .unwrap(); 288 assert_eq!(resp.status(), 200); 289 let resp = client 290 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 291 .header("Authorization", format!("Bearer {}", token)) 292 .send() 293 .await 294 .unwrap(); 295 let body: Value = resp.json().await.unwrap(); 296 let prefs_arr = body["preferences"].as_array().unwrap(); 297 assert_eq!(prefs_arr[0]["$type"], "app.bsky.actor.defs#mutedWordsPref"); 298} 299 300#[tokio::test] 301async fn test_preferences_isolation_between_users() { 302 let client = client(); 303 let base = base_url().await; 304 let (token1, _did1) = create_account_and_login(&client).await; 305 let (token2, _did2) = create_account_and_login(&client).await; 306 let prefs1 = json!({ 307 "preferences": [ 308 { 309 "$type": "app.bsky.actor.defs#adultContentPref", 310 "enabled": true 311 } 312 ] 313 }); 314 client 315 .post(format!("{}/xrpc/app.bsky.actor.putPreferences", base)) 316 .header("Authorization", format!("Bearer {}", token1)) 317 .json(&prefs1) 318 .send() 319 .await 320 .unwrap(); 321 let resp = client 322 .get(format!("{}/xrpc/app.bsky.actor.getPreferences", base)) 323 .header("Authorization", format!("Bearer {}", token2)) 324 .send() 325 .await 326 .unwrap(); 327 assert_eq!(resp.status(), 200); 328 let body: Value = resp.json().await.unwrap(); 329 assert!(body["preferences"].as_array().unwrap().is_empty()); 330}