this repo has no description
1mod common;
2mod helpers;
3use common::*;
4use helpers::*;
5use reqwest::StatusCode;
6use serde_json::Value;
7
8#[tokio::test]
9async fn test_search_accounts_as_admin() {
10 let client = client();
11 let (admin_jwt, _) = create_admin_account_and_login(&client).await;
12 let (user_did, _) = setup_new_user("search-target").await;
13 let res = client
14 .get(format!(
15 "{}/xrpc/com.atproto.admin.searchAccounts?limit=1000",
16 base_url().await
17 ))
18 .bearer_auth(&admin_jwt)
19 .send()
20 .await
21 .expect("Failed to send request");
22 assert_eq!(res.status(), StatusCode::OK);
23 let body: Value = res.json().await.unwrap();
24 let accounts = body["accounts"].as_array().expect("accounts should be array");
25 assert!(!accounts.is_empty(), "Should return some accounts");
26 let found = accounts.iter().any(|a| a["did"].as_str() == Some(&user_did));
27 assert!(found, "Should find the created user in results (DID: {})", user_did);
28}
29
30#[tokio::test]
31async fn test_search_accounts_with_handle_filter() {
32 let client = client();
33 let (admin_jwt, _) = create_admin_account_and_login(&client).await;
34 let ts = chrono::Utc::now().timestamp_millis();
35 let unique_handle = format!("unique-handle-{}.test", ts);
36 let create_payload = serde_json::json!({
37 "handle": unique_handle,
38 "email": format!("unique-{}@searchtest.com", ts),
39 "password": "test-password-123"
40 });
41 let create_res = client
42 .post(format!(
43 "{}/xrpc/com.atproto.server.createAccount",
44 base_url().await
45 ))
46 .json(&create_payload)
47 .send()
48 .await
49 .expect("Failed to create account");
50 assert_eq!(create_res.status(), StatusCode::OK);
51 let res = client
52 .get(format!(
53 "{}/xrpc/com.atproto.admin.searchAccounts?handle={}",
54 base_url().await,
55 unique_handle
56 ))
57 .bearer_auth(&admin_jwt)
58 .send()
59 .await
60 .expect("Failed to send request");
61 assert_eq!(res.status(), StatusCode::OK);
62 let body: Value = res.json().await.unwrap();
63 let accounts = body["accounts"].as_array().unwrap();
64 assert_eq!(accounts.len(), 1, "Should find exactly one account with this handle");
65 assert_eq!(accounts[0]["handle"].as_str(), Some(unique_handle.as_str()));
66}
67
68#[tokio::test]
69async fn test_search_accounts_pagination() {
70 let client = client();
71 let (admin_jwt, _) = create_admin_account_and_login(&client).await;
72 for i in 0..3 {
73 let _ = setup_new_user(&format!("search-page-{}", i)).await;
74 }
75 let res = client
76 .get(format!(
77 "{}/xrpc/com.atproto.admin.searchAccounts?limit=2",
78 base_url().await
79 ))
80 .bearer_auth(&admin_jwt)
81 .send()
82 .await
83 .expect("Failed to send request");
84 assert_eq!(res.status(), StatusCode::OK);
85 let body: Value = res.json().await.unwrap();
86 let accounts = body["accounts"].as_array().unwrap();
87 assert_eq!(accounts.len(), 2, "Should return exactly 2 accounts");
88 let cursor = body["cursor"].as_str();
89 assert!(cursor.is_some(), "Should have cursor for more results");
90 let res2 = client
91 .get(format!(
92 "{}/xrpc/com.atproto.admin.searchAccounts?limit=2&cursor={}",
93 base_url().await,
94 cursor.unwrap()
95 ))
96 .bearer_auth(&admin_jwt)
97 .send()
98 .await
99 .expect("Failed to send request");
100 assert_eq!(res2.status(), StatusCode::OK);
101 let body2: Value = res2.json().await.unwrap();
102 let accounts2 = body2["accounts"].as_array().unwrap();
103 assert!(!accounts2.is_empty(), "Should return more accounts after cursor");
104 let first_page_dids: Vec<&str> = accounts.iter().map(|a| a["did"].as_str().unwrap()).collect();
105 let second_page_dids: Vec<&str> = accounts2.iter().map(|a| a["did"].as_str().unwrap()).collect();
106 for did in &second_page_dids {
107 assert!(!first_page_dids.contains(did), "Second page should not repeat first page DIDs");
108 }
109}
110
111#[tokio::test]
112async fn test_search_accounts_requires_admin() {
113 let client = client();
114 let _ = create_account_and_login(&client).await;
115 let (_, user_jwt) = setup_new_user("search-nonadmin").await;
116 let res = client
117 .get(format!(
118 "{}/xrpc/com.atproto.admin.searchAccounts",
119 base_url().await
120 ))
121 .bearer_auth(&user_jwt)
122 .send()
123 .await
124 .expect("Failed to send request");
125 assert_eq!(res.status(), StatusCode::FORBIDDEN);
126}
127
128#[tokio::test]
129async fn test_search_accounts_requires_auth() {
130 let client = client();
131 let res = client
132 .get(format!(
133 "{}/xrpc/com.atproto.admin.searchAccounts",
134 base_url().await
135 ))
136 .send()
137 .await
138 .expect("Failed to send request");
139 assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
140}
141
142#[tokio::test]
143async fn test_search_accounts_returns_expected_fields() {
144 let client = client();
145 let (admin_jwt, _) = create_admin_account_and_login(&client).await;
146 let _ = setup_new_user("search-fields").await;
147 let res = client
148 .get(format!(
149 "{}/xrpc/com.atproto.admin.searchAccounts?limit=1",
150 base_url().await
151 ))
152 .bearer_auth(&admin_jwt)
153 .send()
154 .await
155 .expect("Failed to send request");
156 assert_eq!(res.status(), StatusCode::OK);
157 let body: Value = res.json().await.unwrap();
158 let accounts = body["accounts"].as_array().unwrap();
159 assert!(!accounts.is_empty());
160 let account = &accounts[0];
161 assert!(account["did"].as_str().is_some(), "Should have did");
162 assert!(account["handle"].as_str().is_some(), "Should have handle");
163 assert!(account["indexedAt"].as_str().is_some(), "Should have indexedAt");
164}