this repo has no description
1mod common;
2use common::*;
3use reqwest::StatusCode;
4use serde_json::Value;
5
6#[tokio::test]
7async fn test_get_latest_commit_success() {
8 let client = client();
9 let (_, did) = create_account_and_login(&client).await;
10
11 let params = [("did", did.as_str())];
12 let res = client
13 .get(format!(
14 "{}/xrpc/com.atproto.sync.getLatestCommit",
15 base_url().await
16 ))
17 .query(¶ms)
18 .send()
19 .await
20 .expect("Failed to send request");
21
22 assert_eq!(res.status(), StatusCode::OK);
23 let body: Value = res.json().await.expect("Response was not valid JSON");
24 assert!(body["cid"].is_string());
25 assert!(body["rev"].is_string());
26}
27
28#[tokio::test]
29async fn test_get_latest_commit_not_found() {
30 let client = client();
31 let params = [("did", "did:plc:nonexistent12345")];
32 let res = client
33 .get(format!(
34 "{}/xrpc/com.atproto.sync.getLatestCommit",
35 base_url().await
36 ))
37 .query(¶ms)
38 .send()
39 .await
40 .expect("Failed to send request");
41
42 assert_eq!(res.status(), StatusCode::NOT_FOUND);
43 let body: Value = res.json().await.expect("Response was not valid JSON");
44 assert_eq!(body["error"], "RepoNotFound");
45}
46
47#[tokio::test]
48async fn test_get_latest_commit_missing_param() {
49 let client = client();
50 let res = client
51 .get(format!(
52 "{}/xrpc/com.atproto.sync.getLatestCommit",
53 base_url().await
54 ))
55 .send()
56 .await
57 .expect("Failed to send request");
58
59 assert_eq!(res.status(), StatusCode::BAD_REQUEST);
60}
61
62#[tokio::test]
63async fn test_list_repos() {
64 let client = client();
65 let _ = create_account_and_login(&client).await;
66
67 let res = client
68 .get(format!(
69 "{}/xrpc/com.atproto.sync.listRepos",
70 base_url().await
71 ))
72 .send()
73 .await
74 .expect("Failed to send request");
75
76 assert_eq!(res.status(), StatusCode::OK);
77 let body: Value = res.json().await.expect("Response was not valid JSON");
78 assert!(body["repos"].is_array());
79 let repos = body["repos"].as_array().unwrap();
80 assert!(!repos.is_empty());
81
82 let repo = &repos[0];
83 assert!(repo["did"].is_string());
84 assert!(repo["head"].is_string());
85 assert!(repo["active"].is_boolean());
86}
87
88#[tokio::test]
89async fn test_list_repos_with_limit() {
90 let client = client();
91 let _ = create_account_and_login(&client).await;
92 let _ = create_account_and_login(&client).await;
93 let _ = create_account_and_login(&client).await;
94
95 let params = [("limit", "2")];
96 let res = client
97 .get(format!(
98 "{}/xrpc/com.atproto.sync.listRepos",
99 base_url().await
100 ))
101 .query(¶ms)
102 .send()
103 .await
104 .expect("Failed to send request");
105
106 assert_eq!(res.status(), StatusCode::OK);
107 let body: Value = res.json().await.expect("Response was not valid JSON");
108 let repos = body["repos"].as_array().unwrap();
109 assert!(repos.len() <= 2);
110}
111
112#[tokio::test]
113async fn test_list_repos_pagination() {
114 let client = client();
115 let _ = create_account_and_login(&client).await;
116 let _ = create_account_and_login(&client).await;
117 let _ = create_account_and_login(&client).await;
118
119 let params = [("limit", "1")];
120 let res = client
121 .get(format!(
122 "{}/xrpc/com.atproto.sync.listRepos",
123 base_url().await
124 ))
125 .query(¶ms)
126 .send()
127 .await
128 .expect("Failed to send request");
129
130 assert_eq!(res.status(), StatusCode::OK);
131 let body: Value = res.json().await.expect("Response was not valid JSON");
132 let repos = body["repos"].as_array().unwrap();
133 assert_eq!(repos.len(), 1);
134
135 if let Some(cursor) = body["cursor"].as_str() {
136 let params = [("limit", "1"), ("cursor", cursor)];
137 let res = client
138 .get(format!(
139 "{}/xrpc/com.atproto.sync.listRepos",
140 base_url().await
141 ))
142 .query(¶ms)
143 .send()
144 .await
145 .expect("Failed to send request");
146
147 assert_eq!(res.status(), StatusCode::OK);
148 let body: Value = res.json().await.expect("Response was not valid JSON");
149 let repos2 = body["repos"].as_array().unwrap();
150 assert_eq!(repos2.len(), 1);
151 assert_ne!(repos[0]["did"], repos2[0]["did"]);
152 }
153}
154
155#[tokio::test]
156async fn test_get_repo_status_success() {
157 let client = client();
158 let (_, did) = create_account_and_login(&client).await;
159
160 let params = [("did", did.as_str())];
161 let res = client
162 .get(format!(
163 "{}/xrpc/com.atproto.sync.getRepoStatus",
164 base_url().await
165 ))
166 .query(¶ms)
167 .send()
168 .await
169 .expect("Failed to send request");
170
171 assert_eq!(res.status(), StatusCode::OK);
172 let body: Value = res.json().await.expect("Response was not valid JSON");
173 assert_eq!(body["did"], did);
174 assert_eq!(body["active"], true);
175 assert!(body["rev"].is_string());
176}
177
178#[tokio::test]
179async fn test_get_repo_status_not_found() {
180 let client = client();
181 let params = [("did", "did:plc:nonexistent12345")];
182 let res = client
183 .get(format!(
184 "{}/xrpc/com.atproto.sync.getRepoStatus",
185 base_url().await
186 ))
187 .query(¶ms)
188 .send()
189 .await
190 .expect("Failed to send request");
191
192 assert_eq!(res.status(), StatusCode::NOT_FOUND);
193 let body: Value = res.json().await.expect("Response was not valid JSON");
194 assert_eq!(body["error"], "RepoNotFound");
195}
196
197#[tokio::test]
198async fn test_notify_of_update() {
199 let client = client();
200 let params = [("hostname", "example.com")];
201 let res = client
202 .post(format!(
203 "{}/xrpc/com.atproto.sync.notifyOfUpdate",
204 base_url().await
205 ))
206 .query(¶ms)
207 .send()
208 .await
209 .expect("Failed to send request");
210
211 assert_eq!(res.status(), StatusCode::OK);
212}
213
214#[tokio::test]
215async fn test_request_crawl() {
216 let client = client();
217 let payload = serde_json::json!({"hostname": "example.com"});
218 let res = client
219 .post(format!(
220 "{}/xrpc/com.atproto.sync.requestCrawl",
221 base_url().await
222 ))
223 .json(&payload)
224 .send()
225 .await
226 .expect("Failed to send request");
227
228 assert_eq!(res.status(), StatusCode::OK);
229}
230
231#[tokio::test]
232async fn test_get_repo_success() {
233 let client = client();
234 let (access_jwt, did) = create_account_and_login(&client).await;
235
236 let post_payload = serde_json::json!({
237 "repo": did,
238 "collection": "app.bsky.feed.post",
239 "record": {
240 "$type": "app.bsky.feed.post",
241 "text": "Test post for getRepo",
242 "createdAt": chrono::Utc::now().to_rfc3339()
243 }
244 });
245 let _ = client
246 .post(format!(
247 "{}/xrpc/com.atproto.repo.createRecord",
248 base_url().await
249 ))
250 .bearer_auth(&access_jwt)
251 .json(&post_payload)
252 .send()
253 .await
254 .expect("Failed to create record");
255
256 let params = [("did", did.as_str())];
257 let res = client
258 .get(format!(
259 "{}/xrpc/com.atproto.sync.getRepo",
260 base_url().await
261 ))
262 .query(¶ms)
263 .send()
264 .await
265 .expect("Failed to send request");
266
267 assert_eq!(res.status(), StatusCode::OK);
268 assert_eq!(
269 res.headers()
270 .get("content-type")
271 .and_then(|h| h.to_str().ok()),
272 Some("application/vnd.ipld.car")
273 );
274 let body = res.bytes().await.expect("Failed to get body");
275 assert!(!body.is_empty());
276}
277
278#[tokio::test]
279async fn test_get_repo_not_found() {
280 let client = client();
281 let params = [("did", "did:plc:nonexistent12345")];
282 let res = client
283 .get(format!(
284 "{}/xrpc/com.atproto.sync.getRepo",
285 base_url().await
286 ))
287 .query(¶ms)
288 .send()
289 .await
290 .expect("Failed to send request");
291
292 assert_eq!(res.status(), StatusCode::NOT_FOUND);
293 let body: Value = res.json().await.expect("Response was not valid JSON");
294 assert_eq!(body["error"], "RepoNotFound");
295}
296
297#[tokio::test]
298async fn test_get_record_sync_success() {
299 let client = client();
300 let (access_jwt, did) = create_account_and_login(&client).await;
301
302 let post_payload = serde_json::json!({
303 "repo": did,
304 "collection": "app.bsky.feed.post",
305 "record": {
306 "$type": "app.bsky.feed.post",
307 "text": "Test post for sync getRecord",
308 "createdAt": chrono::Utc::now().to_rfc3339()
309 }
310 });
311 let create_res = client
312 .post(format!(
313 "{}/xrpc/com.atproto.repo.createRecord",
314 base_url().await
315 ))
316 .bearer_auth(&access_jwt)
317 .json(&post_payload)
318 .send()
319 .await
320 .expect("Failed to create record");
321
322 let create_body: Value = create_res.json().await.expect("Invalid JSON");
323 let uri = create_body["uri"].as_str().expect("No URI");
324 let rkey = uri.split('/').last().expect("Invalid URI");
325
326 let params = [
327 ("did", did.as_str()),
328 ("collection", "app.bsky.feed.post"),
329 ("rkey", rkey),
330 ];
331 let res = client
332 .get(format!(
333 "{}/xrpc/com.atproto.sync.getRecord",
334 base_url().await
335 ))
336 .query(¶ms)
337 .send()
338 .await
339 .expect("Failed to send request");
340
341 assert_eq!(res.status(), StatusCode::OK);
342 assert_eq!(
343 res.headers()
344 .get("content-type")
345 .and_then(|h| h.to_str().ok()),
346 Some("application/vnd.ipld.car")
347 );
348 let body = res.bytes().await.expect("Failed to get body");
349 assert!(!body.is_empty());
350}
351
352#[tokio::test]
353async fn test_get_record_sync_not_found() {
354 let client = client();
355 let (_, did) = create_account_and_login(&client).await;
356
357 let params = [
358 ("did", did.as_str()),
359 ("collection", "app.bsky.feed.post"),
360 ("rkey", "nonexistent12345"),
361 ];
362 let res = client
363 .get(format!(
364 "{}/xrpc/com.atproto.sync.getRecord",
365 base_url().await
366 ))
367 .query(¶ms)
368 .send()
369 .await
370 .expect("Failed to send request");
371
372 assert_eq!(res.status(), StatusCode::NOT_FOUND);
373 let body: Value = res.json().await.expect("Response was not valid JSON");
374 assert_eq!(body["error"], "RecordNotFound");
375}
376
377#[tokio::test]
378async fn test_get_blocks_success() {
379 let client = client();
380 let (_, did) = create_account_and_login(&client).await;
381
382 let params = [("did", did.as_str())];
383 let latest_res = client
384 .get(format!(
385 "{}/xrpc/com.atproto.sync.getLatestCommit",
386 base_url().await
387 ))
388 .query(¶ms)
389 .send()
390 .await
391 .expect("Failed to get latest commit");
392
393 let latest_body: Value = latest_res.json().await.expect("Invalid JSON");
394 let root_cid = latest_body["cid"].as_str().expect("No CID");
395
396 let url = format!(
397 "{}/xrpc/com.atproto.sync.getBlocks?did={}&cids={}",
398 base_url().await,
399 did,
400 root_cid
401 );
402 let res = client
403 .get(&url)
404 .send()
405 .await
406 .expect("Failed to send request");
407
408 assert_eq!(res.status(), StatusCode::OK);
409 assert_eq!(
410 res.headers()
411 .get("content-type")
412 .and_then(|h| h.to_str().ok()),
413 Some("application/vnd.ipld.car")
414 );
415}
416
417#[tokio::test]
418async fn test_get_blocks_not_found() {
419 let client = client();
420 let url = format!(
421 "{}/xrpc/com.atproto.sync.getBlocks?did=did:plc:nonexistent12345&cids=bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku",
422 base_url().await
423 );
424 let res = client
425 .get(&url)
426 .send()
427 .await
428 .expect("Failed to send request");
429
430 assert_eq!(res.status(), StatusCode::NOT_FOUND);
431}