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
99pub async fn create_follow(
100 client: &reqwest::Client,
101 follower_did: &str,
102 follower_jwt: &str,
103 followee_did: &str,
104) -> (String, String) {
105 let collection = "app.bsky.graph.follow";
106 let rkey = format!("e2e_follow_{}", Utc::now().timestamp_millis());
107 let now = Utc::now().to_rfc3339();
108
109 let create_payload = json!({
110 "repo": follower_did,
111 "collection": collection,
112 "rkey": rkey,
113 "record": {
114 "$type": collection,
115 "subject": followee_did,
116 "createdAt": now
117 }
118 });
119
120 let create_res = client
121 .post(format!(
122 "{}/xrpc/com.atproto.repo.putRecord",
123 base_url().await
124 ))
125 .bearer_auth(follower_jwt)
126 .json(&create_payload)
127 .send()
128 .await
129 .expect("Failed to send create follow request");
130
131 assert_eq!(
132 create_res.status(),
133 reqwest::StatusCode::OK,
134 "Failed to create follow record"
135 );
136 let create_body: Value = create_res
137 .json()
138 .await
139 .expect("create follow response was not JSON");
140 let uri = create_body["uri"].as_str().unwrap().to_string();
141 let cid = create_body["cid"].as_str().unwrap().to_string();
142 (uri, cid)
143}
144
145pub async fn create_like(
146 client: &reqwest::Client,
147 liker_did: &str,
148 liker_jwt: &str,
149 subject_uri: &str,
150 subject_cid: &str,
151) -> (String, String) {
152 let collection = "app.bsky.feed.like";
153 let rkey = format!("e2e_like_{}", Utc::now().timestamp_millis());
154 let now = Utc::now().to_rfc3339();
155
156 let payload = json!({
157 "repo": liker_did,
158 "collection": collection,
159 "rkey": rkey,
160 "record": {
161 "$type": collection,
162 "subject": {
163 "uri": subject_uri,
164 "cid": subject_cid
165 },
166 "createdAt": now
167 }
168 });
169
170 let res = client
171 .post(format!(
172 "{}/xrpc/com.atproto.repo.putRecord",
173 base_url().await
174 ))
175 .bearer_auth(liker_jwt)
176 .json(&payload)
177 .send()
178 .await
179 .expect("Failed to create like");
180
181 assert_eq!(res.status(), StatusCode::OK, "Failed to create like");
182 let body: Value = res.json().await.expect("Like response not JSON");
183 (
184 body["uri"].as_str().unwrap().to_string(),
185 body["cid"].as_str().unwrap().to_string(),
186 )
187}
188
189pub async fn create_repost(
190 client: &reqwest::Client,
191 reposter_did: &str,
192 reposter_jwt: &str,
193 subject_uri: &str,
194 subject_cid: &str,
195) -> (String, String) {
196 let collection = "app.bsky.feed.repost";
197 let rkey = format!("e2e_repost_{}", Utc::now().timestamp_millis());
198 let now = Utc::now().to_rfc3339();
199
200 let payload = json!({
201 "repo": reposter_did,
202 "collection": collection,
203 "rkey": rkey,
204 "record": {
205 "$type": collection,
206 "subject": {
207 "uri": subject_uri,
208 "cid": subject_cid
209 },
210 "createdAt": now
211 }
212 });
213
214 let res = client
215 .post(format!(
216 "{}/xrpc/com.atproto.repo.putRecord",
217 base_url().await
218 ))
219 .bearer_auth(reposter_jwt)
220 .json(&payload)
221 .send()
222 .await
223 .expect("Failed to create repost");
224
225 assert_eq!(res.status(), StatusCode::OK, "Failed to create repost");
226 let body: Value = res.json().await.expect("Repost response not JSON");
227 (
228 body["uri"].as_str().unwrap().to_string(),
229 body["cid"].as_str().unwrap().to_string(),
230 )
231}