this repo has no description
1use reqwest::{header, Client, StatusCode}; 2use serde_json::{json, Value}; 3use chrono::Utc; 4#[allow(unused_imports)] 5use std::collections::HashMap; 6#[allow(unused_imports)] 7use std::time::Duration; 8 9pub const BASE_URL: &str = "http://127.0.0.1:3000"; 10#[allow(dead_code)] 11pub const AUTH_TOKEN: &str = "test-token"; 12#[allow(dead_code)] 13pub const BAD_AUTH_TOKEN: &str = "bad-token"; 14#[allow(dead_code)] 15pub const AUTH_DID: &str = "did:plc:fake"; 16#[allow(dead_code)] 17pub const TARGET_DID: &str = "did:plc:target"; 18 19pub fn client() -> Client { 20 Client::new() 21} 22 23#[allow(dead_code)] 24pub async fn upload_test_blob(client: &Client, data: &'static str, mime: &'static str) -> Value { 25 let res = client.post(format!("{}/xrpc/com.atproto.repo.uploadBlob", BASE_URL)) 26 .header(header::CONTENT_TYPE, mime) 27 .bearer_auth(AUTH_TOKEN) 28 .body(data) 29 .send() 30 .await 31 .expect("Failed to send uploadBlob request"); 32 33 assert_eq!(res.status(), StatusCode::OK, "Failed to upload blob"); 34 let body: Value = res.json().await.expect("Blob upload response was not JSON"); 35 body["blob"].clone() 36} 37 38 39#[allow(dead_code)] 40pub async fn create_test_post( 41 client: &Client, 42 text: &str, 43 reply_to: Option<Value> 44) -> (String, String, String) { 45 let collection = "app.bsky.feed.post"; 46 let mut record = json!({ 47 "$type": collection, 48 "text": text, 49 "createdAt": Utc::now().to_rfc3339() 50 }); 51 52 if let Some(reply_obj) = reply_to { 53 record["reply"] = reply_obj; 54 } 55 56 let payload = json!({ 57 "repo": AUTH_DID, 58 "collection": collection, 59 "record": record 60 }); 61 62 let res = client.post(format!("{}/xrpc/com.atproto.repo.createRecord", BASE_URL)) 63 .bearer_auth(AUTH_TOKEN) 64 .json(&payload) 65 .send() 66 .await 67 .expect("Failed to send createRecord"); 68 69 assert_eq!(res.status(), StatusCode::OK, "Failed to create post record"); 70 let body: Value = res.json().await.expect("createRecord response was not JSON"); 71 72 let uri = body["uri"].as_str().expect("Response had no URI").to_string(); 73 let cid = body["cid"].as_str().expect("Response had no CID").to_string(); 74 let rkey = uri.split('/').last().expect("URI was malformed").to_string(); 75 76 (uri, cid, rkey) 77} 78 79pub async fn create_account_and_login(client: &Client) -> (String, String) { 80 let handle = format!("user_{}", uuid::Uuid::new_v4()); 81 let payload = json!({ 82 "handle": handle, 83 "email": format!("{}@example.com", handle), 84 "password": "password" 85 }); 86 87 let res = client.post(format!("{}/xrpc/com.atproto.server.createAccount", BASE_URL)) 88 .json(&payload) 89 .send() 90 .await 91 .expect("Failed to create account"); 92 93 if res.status() != StatusCode::OK { 94 panic!("Failed to create account: {:?}", res.text().await); 95 } 96 97 let body: Value = res.json().await.expect("Invalid JSON"); 98 let access_jwt = body["accessJwt"].as_str().expect("No accessJwt").to_string(); 99 let did = body["did"].as_str().expect("No did").to_string(); 100 (access_jwt, did) 101}