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