this repo has no description
1use chrono::Utc;
2use reqwest::StatusCode;
3use serde_json::{Value, json};
4
5pub use crate::common::*;
6
7#[allow(dead_code)]
8pub async fn setup_new_user(handle_prefix: &str) -> (String, String) {
9 let client = client();
10 let ts = Utc::now().timestamp_millis();
11 let handle = format!("{}-{}.test", handle_prefix, ts);
12 let email = format!("{}-{}@test.com", handle_prefix, ts);
13 let password = "e2e-password-123";
14
15 let create_account_payload = json!({
16 "handle": handle,
17 "email": email,
18 "password": password
19 });
20 let create_res = client
21 .post(format!(
22 "{}/xrpc/com.atproto.server.createAccount",
23 base_url().await
24 ))
25 .json(&create_account_payload)
26 .send()
27 .await
28 .expect("setup_new_user: Failed to send createAccount");
29
30 if create_res.status() != reqwest::StatusCode::OK {
31 panic!(
32 "setup_new_user: Failed to create account: {:?}",
33 create_res.text().await
34 );
35 }
36
37 let create_body: Value = create_res
38 .json()
39 .await
40 .expect("setup_new_user: createAccount response was not JSON");
41
42 let new_did = create_body["did"]
43 .as_str()
44 .expect("setup_new_user: Response had no DID")
45 .to_string();
46
47 let new_jwt = verify_new_account(&client, &new_did).await;
48
49 (new_did, new_jwt)
50}
51
52#[allow(dead_code)]
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}