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