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