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