Add admin functionality

This commit is contained in:
lewis
2025-12-16 18:28:20 +02:00
parent e2bfcdb74f
commit dea6c09aa0
24 changed files with 257 additions and 288 deletions
+5 -5
View File
@@ -18,7 +18,7 @@ async fn test_send_email_success() {
let client = common::client();
let base_url = common::base_url().await;
let pool = get_pool().await;
let (access_jwt, did) = common::create_account_and_login(&client).await;
let (access_jwt, did) = common::create_admin_account_and_login(&client).await;
let res = client
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
.bearer_auth(&access_jwt)
@@ -58,7 +58,7 @@ async fn test_send_email_default_subject() {
let client = common::client();
let base_url = common::base_url().await;
let pool = get_pool().await;
let (access_jwt, did) = common::create_account_and_login(&client).await;
let (access_jwt, did) = common::create_admin_account_and_login(&client).await;
let res = client
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
.bearer_auth(&access_jwt)
@@ -92,7 +92,7 @@ async fn test_send_email_default_subject() {
async fn test_send_email_recipient_not_found() {
let client = common::client();
let base_url = common::base_url().await;
let (access_jwt, _) = common::create_account_and_login(&client).await;
let (access_jwt, _) = common::create_admin_account_and_login(&client).await;
let res = client
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
.bearer_auth(&access_jwt)
@@ -113,7 +113,7 @@ async fn test_send_email_recipient_not_found() {
async fn test_send_email_missing_content() {
let client = common::client();
let base_url = common::base_url().await;
let (access_jwt, did) = common::create_account_and_login(&client).await;
let (access_jwt, did) = common::create_admin_account_and_login(&client).await;
let res = client
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
.bearer_auth(&access_jwt)
@@ -134,7 +134,7 @@ async fn test_send_email_missing_content() {
async fn test_send_email_missing_recipient() {
let client = common::client();
let base_url = common::base_url().await;
let (access_jwt, _) = common::create_account_and_login(&client).await;
let (access_jwt, _) = common::create_admin_account_and_login(&client).await;
let res = client
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
.bearer_auth(&access_jwt)
+8 -8
View File
@@ -7,7 +7,7 @@ use serde_json::{Value, json};
#[tokio::test]
async fn test_admin_get_invite_codes_success() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let create_payload = json!({
"useCount": 3
});
@@ -38,7 +38,7 @@ async fn test_admin_get_invite_codes_success() {
#[tokio::test]
async fn test_admin_get_invite_codes_with_limit() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
for _ in 0..5 {
let create_payload = json!({
"useCount": 1
@@ -86,7 +86,7 @@ async fn test_admin_get_invite_codes_no_auth() {
#[tokio::test]
async fn test_disable_account_invites_success() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (access_jwt, did) = create_admin_account_and_login(&client).await;
let payload = json!({
"account": did
});
@@ -122,7 +122,7 @@ async fn test_disable_account_invites_success() {
#[tokio::test]
async fn test_enable_account_invites_success() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (access_jwt, did) = create_admin_account_and_login(&client).await;
let disable_payload = json!({
"account": did
});
@@ -186,7 +186,7 @@ async fn test_disable_account_invites_no_auth() {
#[tokio::test]
async fn test_disable_account_invites_not_found() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let payload = json!({
"account": "did:plc:nonexistent"
});
@@ -206,7 +206,7 @@ async fn test_disable_account_invites_not_found() {
#[tokio::test]
async fn test_disable_invite_codes_by_code() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let create_payload = json!({
"useCount": 5
});
@@ -255,7 +255,7 @@ async fn test_disable_invite_codes_by_code() {
#[tokio::test]
async fn test_disable_invite_codes_by_account() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (access_jwt, did) = create_admin_account_and_login(&client).await;
for _ in 0..3 {
let create_payload = json!({
"useCount": 1
@@ -321,7 +321,7 @@ async fn test_disable_invite_codes_no_auth() {
#[tokio::test]
async fn test_admin_enable_account_invites_not_found() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let payload = json!({
"account": "did:plc:nonexistent"
});
+24 -21
View File
@@ -7,7 +7,7 @@ use serde_json::{Value, json};
#[tokio::test]
async fn test_get_subject_status_user_success() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (access_jwt, did) = create_admin_account_and_login(&client).await;
let res = client
.get(format!(
"{}/xrpc/com.atproto.admin.getSubjectStatus",
@@ -28,7 +28,7 @@ async fn test_get_subject_status_user_success() {
#[tokio::test]
async fn test_get_subject_status_not_found() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let res = client
.get(format!(
"{}/xrpc/com.atproto.admin.getSubjectStatus",
@@ -47,7 +47,7 @@ async fn test_get_subject_status_not_found() {
#[tokio::test]
async fn test_get_subject_status_no_param() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let res = client
.get(format!(
"{}/xrpc/com.atproto.admin.getSubjectStatus",
@@ -80,11 +80,12 @@ async fn test_get_subject_status_no_auth() {
#[tokio::test]
async fn test_update_subject_status_takedown_user() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (admin_jwt, _) = create_admin_account_and_login(&client).await;
let (_, target_did) = create_account_and_login(&client).await;
let payload = json!({
"subject": {
"$type": "com.atproto.admin.defs#repoRef",
"did": did
"did": target_did
},
"takedown": {
"apply": true,
@@ -96,7 +97,7 @@ async fn test_update_subject_status_takedown_user() {
"{}/xrpc/com.atproto.admin.updateSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.bearer_auth(&admin_jwt)
.json(&payload)
.send()
.await
@@ -111,8 +112,8 @@ async fn test_update_subject_status_takedown_user() {
"{}/xrpc/com.atproto.admin.getSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.query(&[("did", did.as_str())])
.bearer_auth(&admin_jwt)
.query(&[("did", target_did.as_str())])
.send()
.await
.expect("Failed to send request");
@@ -125,11 +126,12 @@ async fn test_update_subject_status_takedown_user() {
#[tokio::test]
async fn test_update_subject_status_remove_takedown() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (admin_jwt, _) = create_admin_account_and_login(&client).await;
let (_, target_did) = create_account_and_login(&client).await;
let takedown_payload = json!({
"subject": {
"$type": "com.atproto.admin.defs#repoRef",
"did": did
"did": target_did
},
"takedown": {
"apply": true,
@@ -141,14 +143,14 @@ async fn test_update_subject_status_remove_takedown() {
"{}/xrpc/com.atproto.admin.updateSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.bearer_auth(&admin_jwt)
.json(&takedown_payload)
.send()
.await;
let remove_payload = json!({
"subject": {
"$type": "com.atproto.admin.defs#repoRef",
"did": did
"did": target_did
},
"takedown": {
"apply": false
@@ -159,7 +161,7 @@ async fn test_update_subject_status_remove_takedown() {
"{}/xrpc/com.atproto.admin.updateSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.bearer_auth(&admin_jwt)
.json(&remove_payload)
.send()
.await
@@ -170,8 +172,8 @@ async fn test_update_subject_status_remove_takedown() {
"{}/xrpc/com.atproto.admin.getSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.query(&[("did", did.as_str())])
.bearer_auth(&admin_jwt)
.query(&[("did", target_did.as_str())])
.send()
.await
.expect("Failed to send request");
@@ -187,11 +189,12 @@ async fn test_update_subject_status_remove_takedown() {
#[tokio::test]
async fn test_update_subject_status_deactivate_user() {
let client = client();
let (access_jwt, did) = create_account_and_login(&client).await;
let (admin_jwt, _) = create_admin_account_and_login(&client).await;
let (_, target_did) = create_account_and_login(&client).await;
let payload = json!({
"subject": {
"$type": "com.atproto.admin.defs#repoRef",
"did": did
"did": target_did
},
"deactivated": {
"apply": true
@@ -202,7 +205,7 @@ async fn test_update_subject_status_deactivate_user() {
"{}/xrpc/com.atproto.admin.updateSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.bearer_auth(&admin_jwt)
.json(&payload)
.send()
.await
@@ -213,8 +216,8 @@ async fn test_update_subject_status_deactivate_user() {
"{}/xrpc/com.atproto.admin.getSubjectStatus",
base_url().await
))
.bearer_auth(&access_jwt)
.query(&[("did", did.as_str())])
.bearer_auth(&admin_jwt)
.query(&[("did", target_did.as_str())])
.send()
.await
.expect("Failed to send request");
@@ -226,7 +229,7 @@ async fn test_update_subject_status_deactivate_user() {
#[tokio::test]
async fn test_update_subject_status_invalid_type() {
let client = client();
let (access_jwt, _did) = create_account_and_login(&client).await;
let (access_jwt, _did) = create_admin_account_and_login(&client).await;
let payload = json!({
"subject": {
"$type": "invalid.type",
+3 -3
View File
@@ -1,14 +1,14 @@
mod common;
use common::{base_url, client, create_account_and_login};
use common::{base_url, client, create_admin_account_and_login};
use serde_json::Value;
#[tokio::test]
async fn test_get_server_stats() {
let client = client();
let base = base_url().await;
let (token1, _) = create_account_and_login(&client).await;
let (token1, _) = create_admin_account_and_login(&client).await;
let (_, _) = create_account_and_login(&client).await;
let (_, _) = create_admin_account_and_login(&client).await;
let resp = client
.get(format!("{}/xrpc/com.bspds.admin.getServerStats", base))
+18 -4
View File
@@ -511,6 +511,15 @@ pub async fn create_test_post(
#[allow(dead_code)]
pub async fn create_account_and_login(client: &Client) -> (String, String) {
create_account_and_login_internal(client, false).await
}
#[allow(dead_code)]
pub async fn create_admin_account_and_login(client: &Client) -> (String, String) {
create_account_and_login_internal(client, true).await
}
async fn create_account_and_login_internal(client: &Client, make_admin: bool) -> (String, String) {
let mut last_error = String::new();
for attempt in 0..3 {
if attempt > 0 {
@@ -539,10 +548,6 @@ pub async fn create_account_and_login(client: &Client) -> (String, String) {
};
if res.status() == StatusCode::OK {
let body: Value = res.json().await.expect("Invalid JSON");
if let Some(access_jwt) = body["accessJwt"].as_str() {
let did = body["did"].as_str().expect("No did").to_string();
return (access_jwt.to_string(), did);
}
let did = body["did"].as_str().expect("No did").to_string();
let conn_str = get_db_connection_string().await;
let pool = sqlx::postgres::PgPoolOptions::new()
@@ -550,6 +555,15 @@ pub async fn create_account_and_login(client: &Client) -> (String, String) {
.connect(&conn_str)
.await
.expect("Failed to connect to test database");
if make_admin {
sqlx::query!("UPDATE users SET is_admin = TRUE WHERE did = $1", &did)
.execute(&pool)
.await
.expect("Failed to mark user as admin");
}
if let Some(access_jwt) = body["accessJwt"].as_str() {
return (access_jwt.to_string(), did);
}
let verification_code: String = sqlx::query_scalar!(
"SELECT code FROM channel_verifications WHERE user_id = (SELECT id FROM users WHERE did = $1) AND channel = 'email'",
&did