PDS software with bells & whistles you didn’t even know you needed. will move this to its own account when ready.
at main 7.7 kB view raw
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_no_auth() { 88 let client = client(); 89 let payload = json!({ 90 "account": "did:plc:test" 91 }); 92 let res = client 93 .post(format!( 94 "{}/xrpc/com.atproto.admin.disableAccountInvites", 95 base_url().await 96 )) 97 .json(&payload) 98 .send() 99 .await 100 .expect("Failed to send request"); 101 assert_eq!(res.status(), StatusCode::UNAUTHORIZED); 102} 103 104#[tokio::test] 105async fn test_disable_account_invites_not_found() { 106 let client = client(); 107 let (access_jwt, _did) = create_admin_account_and_login(&client).await; 108 let payload = json!({ 109 "account": "did:plc:nonexistent" 110 }); 111 let res = client 112 .post(format!( 113 "{}/xrpc/com.atproto.admin.disableAccountInvites", 114 base_url().await 115 )) 116 .bearer_auth(&access_jwt) 117 .json(&payload) 118 .send() 119 .await 120 .expect("Failed to send request"); 121 assert_eq!(res.status(), StatusCode::NOT_FOUND); 122} 123 124#[tokio::test] 125async fn test_disable_invite_codes_by_code() { 126 let client = client(); 127 let (access_jwt, admin_did) = create_admin_account_and_login(&client).await; 128 let create_payload = json!({ 129 "useCount": 5, 130 "forAccount": admin_did 131 }); 132 let create_res = client 133 .post(format!( 134 "{}/xrpc/com.atproto.server.createInviteCode", 135 base_url().await 136 )) 137 .bearer_auth(&access_jwt) 138 .json(&create_payload) 139 .send() 140 .await 141 .expect("Failed to create invite code"); 142 let create_body: Value = create_res.json().await.unwrap(); 143 let code = create_body["code"].as_str().unwrap(); 144 let disable_payload = json!({ 145 "codes": [code] 146 }); 147 let res = client 148 .post(format!( 149 "{}/xrpc/com.atproto.admin.disableInviteCodes", 150 base_url().await 151 )) 152 .bearer_auth(&access_jwt) 153 .json(&disable_payload) 154 .send() 155 .await 156 .expect("Failed to send request"); 157 assert_eq!(res.status(), StatusCode::OK); 158 159 let list_res = client 160 .get(format!( 161 "{}/xrpc/com.atproto.admin.getInviteCodes", 162 base_url().await 163 )) 164 .bearer_auth(&access_jwt) 165 .send() 166 .await 167 .expect("Failed to get invite codes"); 168 let list_body: Value = list_res.json().await.unwrap(); 169 let codes = list_body["codes"].as_array().unwrap(); 170 let disabled_code = codes.iter().find(|c| c["code"].as_str().unwrap() == code); 171 assert!(disabled_code.is_some()); 172 assert_eq!(disabled_code.unwrap()["disabled"], true); 173} 174 175#[tokio::test] 176async fn test_disable_invite_codes_by_account() { 177 let client = client(); 178 let (access_jwt, did) = create_admin_account_and_login(&client).await; 179 for _ in 0..3 { 180 let create_payload = json!({ 181 "useCount": 1, 182 "forAccount": did 183 }); 184 let _ = client 185 .post(format!( 186 "{}/xrpc/com.atproto.server.createInviteCode", 187 base_url().await 188 )) 189 .bearer_auth(&access_jwt) 190 .json(&create_payload) 191 .send() 192 .await; 193 } 194 let disable_payload = json!({ 195 "accounts": [did] 196 }); 197 let res = client 198 .post(format!( 199 "{}/xrpc/com.atproto.admin.disableInviteCodes", 200 base_url().await 201 )) 202 .bearer_auth(&access_jwt) 203 .json(&disable_payload) 204 .send() 205 .await 206 .expect("Failed to send request"); 207 assert_eq!(res.status(), StatusCode::OK); 208 209 let list_res = client 210 .get(format!( 211 "{}/xrpc/com.atproto.admin.getInviteCodes", 212 base_url().await 213 )) 214 .bearer_auth(&access_jwt) 215 .send() 216 .await 217 .expect("Failed to get invite codes"); 218 let list_body: Value = list_res.json().await.unwrap(); 219 let codes = list_body["codes"].as_array().unwrap(); 220 let admin_codes: Vec<_> = codes 221 .iter() 222 .filter(|c| c["forAccount"].as_str() == Some(&did)) 223 .collect(); 224 for code in admin_codes { 225 assert_eq!(code["disabled"], true); 226 } 227} 228 229#[tokio::test] 230async fn test_disable_invite_codes_no_auth() { 231 let client = client(); 232 let payload = json!({ 233 "codes": ["some-code"] 234 }); 235 let res = client 236 .post(format!( 237 "{}/xrpc/com.atproto.admin.disableInviteCodes", 238 base_url().await 239 )) 240 .json(&payload) 241 .send() 242 .await 243 .expect("Failed to send request"); 244 assert_eq!(res.status(), StatusCode::UNAUTHORIZED); 245} 246 247#[tokio::test] 248async fn test_admin_enable_account_invites_not_found() { 249 let client = client(); 250 let (access_jwt, _did) = create_admin_account_and_login(&client).await; 251 let payload = json!({ 252 "account": "did:plc:nonexistent" 253 }); 254 let res = client 255 .post(format!( 256 "{}/xrpc/com.atproto.admin.enableAccountInvites", 257 base_url().await 258 )) 259 .bearer_auth(&access_jwt) 260 .json(&payload) 261 .send() 262 .await 263 .expect("Failed to send request"); 264 assert_eq!(res.status(), StatusCode::NOT_FOUND); 265}