this repo has no description
1mod common;
2use common::*;
3
4use reqwest::StatusCode;
5use serde_json::{Value, json};
6
7#[tokio::test]
8async fn test_admin_get_invite_codes_success() {
9 let client = client();
10 let (access_jwt, _did) = create_account_and_login(&client).await;
11
12 let create_payload = json!({
13 "useCount": 3
14 });
15 let _ = client
16 .post(format!(
17 "{}/xrpc/com.atproto.server.createInviteCode",
18 base_url().await
19 ))
20 .bearer_auth(&access_jwt)
21 .json(&create_payload)
22 .send()
23 .await
24 .expect("Failed to create invite code");
25
26 let res = client
27 .get(format!(
28 "{}/xrpc/com.atproto.admin.getInviteCodes",
29 base_url().await
30 ))
31 .bearer_auth(&access_jwt)
32 .send()
33 .await
34 .expect("Failed to send request");
35
36 assert_eq!(res.status(), StatusCode::OK);
37 let body: Value = res.json().await.expect("Response was not valid JSON");
38 assert!(body["codes"].is_array());
39}
40
41#[tokio::test]
42async fn test_admin_get_invite_codes_with_limit() {
43 let client = client();
44 let (access_jwt, _did) = create_account_and_login(&client).await;
45
46 for _ in 0..5 {
47 let create_payload = json!({
48 "useCount": 1
49 });
50 let _ = client
51 .post(format!(
52 "{}/xrpc/com.atproto.server.createInviteCode",
53 base_url().await
54 ))
55 .bearer_auth(&access_jwt)
56 .json(&create_payload)
57 .send()
58 .await;
59 }
60
61 let res = client
62 .get(format!(
63 "{}/xrpc/com.atproto.admin.getInviteCodes",
64 base_url().await
65 ))
66 .bearer_auth(&access_jwt)
67 .query(&[("limit", "2")])
68 .send()
69 .await
70 .expect("Failed to send request");
71
72 assert_eq!(res.status(), StatusCode::OK);
73 let body: Value = res.json().await.expect("Response was not valid JSON");
74 let codes = body["codes"].as_array().unwrap();
75 assert!(codes.len() <= 2);
76}
77
78#[tokio::test]
79async fn test_admin_get_invite_codes_no_auth() {
80 let client = client();
81
82 let res = client
83 .get(format!(
84 "{}/xrpc/com.atproto.admin.getInviteCodes",
85 base_url().await
86 ))
87 .send()
88 .await
89 .expect("Failed to send request");
90
91 assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
92}
93
94#[tokio::test]
95async fn test_disable_account_invites_success() {
96 let client = client();
97 let (access_jwt, did) = create_account_and_login(&client).await;
98
99 let payload = json!({
100 "account": did
101 });
102
103 let res = client
104 .post(format!(
105 "{}/xrpc/com.atproto.admin.disableAccountInvites",
106 base_url().await
107 ))
108 .bearer_auth(&access_jwt)
109 .json(&payload)
110 .send()
111 .await
112 .expect("Failed to send request");
113
114 assert_eq!(res.status(), StatusCode::OK);
115
116 let create_payload = json!({
117 "useCount": 1
118 });
119 let res = client
120 .post(format!(
121 "{}/xrpc/com.atproto.server.createInviteCode",
122 base_url().await
123 ))
124 .bearer_auth(&access_jwt)
125 .json(&create_payload)
126 .send()
127 .await
128 .expect("Failed to send request");
129
130 assert_eq!(res.status(), StatusCode::FORBIDDEN);
131 let body: Value = res.json().await.expect("Response was not valid JSON");
132 assert_eq!(body["error"], "InvitesDisabled");
133}
134
135#[tokio::test]
136async fn test_enable_account_invites_success() {
137 let client = client();
138 let (access_jwt, did) = create_account_and_login(&client).await;
139
140 let disable_payload = json!({
141 "account": did
142 });
143 let _ = client
144 .post(format!(
145 "{}/xrpc/com.atproto.admin.disableAccountInvites",
146 base_url().await
147 ))
148 .bearer_auth(&access_jwt)
149 .json(&disable_payload)
150 .send()
151 .await;
152
153 let enable_payload = json!({
154 "account": did
155 });
156 let res = client
157 .post(format!(
158 "{}/xrpc/com.atproto.admin.enableAccountInvites",
159 base_url().await
160 ))
161 .bearer_auth(&access_jwt)
162 .json(&enable_payload)
163 .send()
164 .await
165 .expect("Failed to send request");
166
167 assert_eq!(res.status(), StatusCode::OK);
168
169 let create_payload = json!({
170 "useCount": 1
171 });
172 let res = client
173 .post(format!(
174 "{}/xrpc/com.atproto.server.createInviteCode",
175 base_url().await
176 ))
177 .bearer_auth(&access_jwt)
178 .json(&create_payload)
179 .send()
180 .await
181 .expect("Failed to send request");
182
183 assert_eq!(res.status(), StatusCode::OK);
184}
185
186#[tokio::test]
187async fn test_disable_account_invites_no_auth() {
188 let client = client();
189 let payload = json!({
190 "account": "did:plc:test"
191 });
192
193 let res = client
194 .post(format!(
195 "{}/xrpc/com.atproto.admin.disableAccountInvites",
196 base_url().await
197 ))
198 .json(&payload)
199 .send()
200 .await
201 .expect("Failed to send request");
202
203 assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
204}
205
206#[tokio::test]
207async fn test_disable_account_invites_not_found() {
208 let client = client();
209 let (access_jwt, _did) = create_account_and_login(&client).await;
210
211 let payload = json!({
212 "account": "did:plc:nonexistent"
213 });
214
215 let res = client
216 .post(format!(
217 "{}/xrpc/com.atproto.admin.disableAccountInvites",
218 base_url().await
219 ))
220 .bearer_auth(&access_jwt)
221 .json(&payload)
222 .send()
223 .await
224 .expect("Failed to send request");
225
226 assert_eq!(res.status(), StatusCode::NOT_FOUND);
227}
228
229#[tokio::test]
230async fn test_disable_invite_codes_by_code() {
231 let client = client();
232 let (access_jwt, _did) = create_account_and_login(&client).await;
233
234 let create_payload = json!({
235 "useCount": 5
236 });
237 let create_res = client
238 .post(format!(
239 "{}/xrpc/com.atproto.server.createInviteCode",
240 base_url().await
241 ))
242 .bearer_auth(&access_jwt)
243 .json(&create_payload)
244 .send()
245 .await
246 .expect("Failed to create invite code");
247
248 let create_body: Value = create_res.json().await.unwrap();
249 let code = create_body["code"].as_str().unwrap();
250
251 let disable_payload = json!({
252 "codes": [code]
253 });
254 let res = client
255 .post(format!(
256 "{}/xrpc/com.atproto.admin.disableInviteCodes",
257 base_url().await
258 ))
259 .bearer_auth(&access_jwt)
260 .json(&disable_payload)
261 .send()
262 .await
263 .expect("Failed to send request");
264
265 assert_eq!(res.status(), StatusCode::OK);
266
267 let list_res = client
268 .get(format!(
269 "{}/xrpc/com.atproto.server.getAccountInviteCodes",
270 base_url().await
271 ))
272 .bearer_auth(&access_jwt)
273 .send()
274 .await
275 .expect("Failed to get invite codes");
276
277 let list_body: Value = list_res.json().await.unwrap();
278 let codes = list_body["codes"].as_array().unwrap();
279 let disabled_code = codes.iter().find(|c| c["code"].as_str().unwrap() == code);
280 assert!(disabled_code.is_some());
281 assert_eq!(disabled_code.unwrap()["disabled"], true);
282}
283
284#[tokio::test]
285async fn test_disable_invite_codes_by_account() {
286 let client = client();
287 let (access_jwt, did) = create_account_and_login(&client).await;
288
289 for _ in 0..3 {
290 let create_payload = json!({
291 "useCount": 1
292 });
293 let _ = client
294 .post(format!(
295 "{}/xrpc/com.atproto.server.createInviteCode",
296 base_url().await
297 ))
298 .bearer_auth(&access_jwt)
299 .json(&create_payload)
300 .send()
301 .await;
302 }
303
304 let disable_payload = json!({
305 "accounts": [did]
306 });
307 let res = client
308 .post(format!(
309 "{}/xrpc/com.atproto.admin.disableInviteCodes",
310 base_url().await
311 ))
312 .bearer_auth(&access_jwt)
313 .json(&disable_payload)
314 .send()
315 .await
316 .expect("Failed to send request");
317
318 assert_eq!(res.status(), StatusCode::OK);
319
320 let list_res = client
321 .get(format!(
322 "{}/xrpc/com.atproto.server.getAccountInviteCodes",
323 base_url().await
324 ))
325 .bearer_auth(&access_jwt)
326 .send()
327 .await
328 .expect("Failed to get invite codes");
329
330 let list_body: Value = list_res.json().await.unwrap();
331 let codes = list_body["codes"].as_array().unwrap();
332 for code in codes {
333 assert_eq!(code["disabled"], true);
334 }
335}
336
337#[tokio::test]
338async fn test_disable_invite_codes_no_auth() {
339 let client = client();
340 let payload = json!({
341 "codes": ["some-code"]
342 });
343
344 let res = client
345 .post(format!(
346 "{}/xrpc/com.atproto.admin.disableInviteCodes",
347 base_url().await
348 ))
349 .json(&payload)
350 .send()
351 .await
352 .expect("Failed to send request");
353
354 assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
355}
356
357#[tokio::test]
358async fn test_admin_enable_account_invites_not_found() {
359 let client = client();
360 let (access_jwt, _did) = create_account_and_login(&client).await;
361
362 let payload = json!({
363 "account": "did:plc:nonexistent"
364 });
365
366 let res = client
367 .post(format!(
368 "{}/xrpc/com.atproto.admin.enableAccountInvites",
369 base_url().await
370 ))
371 .bearer_auth(&access_jwt)
372 .json(&payload)
373 .send()
374 .await
375 .expect("Failed to send request");
376
377 assert_eq!(res.status(), StatusCode::NOT_FOUND);
378}