this repo has no description
1use chrono::Utc; 2use reqwest::StatusCode; 3use serde_json::{Value, json}; 4 5pub use crate::common::*; 6 7pub async fn setup_new_user(handle_prefix: &str) -> (String, String) { 8 let client = client(); 9 let ts = Utc::now().timestamp_millis(); 10 let handle = format!("{}-{}.test", handle_prefix, ts); 11 let email = format!("{}-{}@test.com", handle_prefix, ts); 12 let password = "e2e-password-123"; 13 14 let create_account_payload = json!({ 15 "handle": handle, 16 "email": email, 17 "password": password 18 }); 19 let create_res = client 20 .post(format!( 21 "{}/xrpc/com.atproto.server.createAccount", 22 base_url().await 23 )) 24 .json(&create_account_payload) 25 .send() 26 .await 27 .expect("setup_new_user: Failed to send createAccount"); 28 29 if create_res.status() != reqwest::StatusCode::OK { 30 panic!( 31 "setup_new_user: Failed to create account: {:?}", 32 create_res.text().await 33 ); 34 } 35 36 let create_body: Value = create_res 37 .json() 38 .await 39 .expect("setup_new_user: createAccount response was not JSON"); 40 41 let new_did = create_body["did"] 42 .as_str() 43 .expect("setup_new_user: Response had no DID") 44 .to_string(); 45 let new_jwt = create_body["accessJwt"] 46 .as_str() 47 .expect("setup_new_user: Response had no accessJwt") 48 .to_string(); 49 50 (new_did, new_jwt) 51} 52 53pub async fn create_post( 54 client: &reqwest::Client, 55 did: &str, 56 jwt: &str, 57 text: &str, 58) -> (String, String) { 59 let collection = "app.bsky.feed.post"; 60 let rkey = format!("e2e_social_{}", Utc::now().timestamp_millis()); 61 let now = Utc::now().to_rfc3339(); 62 63 let create_payload = json!({ 64 "repo": did, 65 "collection": collection, 66 "rkey": rkey, 67 "record": { 68 "$type": collection, 69 "text": text, 70 "createdAt": now 71 } 72 }); 73 74 let create_res = client 75 .post(format!( 76 "{}/xrpc/com.atproto.repo.putRecord", 77 base_url().await 78 )) 79 .bearer_auth(jwt) 80 .json(&create_payload) 81 .send() 82 .await 83 .expect("Failed to send create post request"); 84 85 assert_eq!( 86 create_res.status(), 87 reqwest::StatusCode::OK, 88 "Failed to create post record" 89 ); 90 let create_body: Value = create_res 91 .json() 92 .await 93 .expect("create post response was not JSON"); 94 let uri = create_body["uri"].as_str().unwrap().to_string(); 95 let cid = create_body["cid"].as_str().unwrap().to_string(); 96 (uri, cid) 97} 98 99#[allow(dead_code)] 100pub async fn create_follow( 101 client: &reqwest::Client, 102 follower_did: &str, 103 follower_jwt: &str, 104 followee_did: &str, 105) -> (String, String) { 106 let collection = "app.bsky.graph.follow"; 107 let rkey = format!("e2e_follow_{}", Utc::now().timestamp_millis()); 108 let now = Utc::now().to_rfc3339(); 109 110 let create_payload = json!({ 111 "repo": follower_did, 112 "collection": collection, 113 "rkey": rkey, 114 "record": { 115 "$type": collection, 116 "subject": followee_did, 117 "createdAt": now 118 } 119 }); 120 121 let create_res = client 122 .post(format!( 123 "{}/xrpc/com.atproto.repo.putRecord", 124 base_url().await 125 )) 126 .bearer_auth(follower_jwt) 127 .json(&create_payload) 128 .send() 129 .await 130 .expect("Failed to send create follow request"); 131 132 assert_eq!( 133 create_res.status(), 134 reqwest::StatusCode::OK, 135 "Failed to create follow record" 136 ); 137 let create_body: Value = create_res 138 .json() 139 .await 140 .expect("create follow response was not JSON"); 141 let uri = create_body["uri"].as_str().unwrap().to_string(); 142 let cid = create_body["cid"].as_str().unwrap().to_string(); 143 (uri, cid) 144} 145 146#[allow(dead_code)] 147pub async fn create_like( 148 client: &reqwest::Client, 149 liker_did: &str, 150 liker_jwt: &str, 151 subject_uri: &str, 152 subject_cid: &str, 153) -> (String, String) { 154 let collection = "app.bsky.feed.like"; 155 let rkey = format!("e2e_like_{}", Utc::now().timestamp_millis()); 156 let now = Utc::now().to_rfc3339(); 157 158 let payload = json!({ 159 "repo": liker_did, 160 "collection": collection, 161 "rkey": rkey, 162 "record": { 163 "$type": collection, 164 "subject": { 165 "uri": subject_uri, 166 "cid": subject_cid 167 }, 168 "createdAt": now 169 } 170 }); 171 172 let res = client 173 .post(format!( 174 "{}/xrpc/com.atproto.repo.putRecord", 175 base_url().await 176 )) 177 .bearer_auth(liker_jwt) 178 .json(&payload) 179 .send() 180 .await 181 .expect("Failed to create like"); 182 183 assert_eq!(res.status(), StatusCode::OK, "Failed to create like"); 184 let body: Value = res.json().await.expect("Like response not JSON"); 185 ( 186 body["uri"].as_str().unwrap().to_string(), 187 body["cid"].as_str().unwrap().to_string(), 188 ) 189} 190 191#[allow(dead_code)] 192pub async fn create_repost( 193 client: &reqwest::Client, 194 reposter_did: &str, 195 reposter_jwt: &str, 196 subject_uri: &str, 197 subject_cid: &str, 198) -> (String, String) { 199 let collection = "app.bsky.feed.repost"; 200 let rkey = format!("e2e_repost_{}", Utc::now().timestamp_millis()); 201 let now = Utc::now().to_rfc3339(); 202 203 let payload = json!({ 204 "repo": reposter_did, 205 "collection": collection, 206 "rkey": rkey, 207 "record": { 208 "$type": collection, 209 "subject": { 210 "uri": subject_uri, 211 "cid": subject_cid 212 }, 213 "createdAt": now 214 } 215 }); 216 217 let res = client 218 .post(format!( 219 "{}/xrpc/com.atproto.repo.putRecord", 220 base_url().await 221 )) 222 .bearer_auth(reposter_jwt) 223 .json(&payload) 224 .send() 225 .await 226 .expect("Failed to create repost"); 227 228 assert_eq!(res.status(), StatusCode::OK, "Failed to create repost"); 229 let body: Value = res.json().await.expect("Repost response not JSON"); 230 ( 231 body["uri"].as_str().unwrap().to_string(), 232 body["cid"].as_str().unwrap().to_string(), 233 ) 234}