mirror of
https://tangled.org/tranquil.farm/tranquil-pds
synced 2026-02-08 21:30:08 +00:00
189 lines
5.7 KiB
Rust
189 lines
5.7 KiB
Rust
mod common;
|
|
|
|
use reqwest::StatusCode;
|
|
use serde_json::{json, Value};
|
|
use sqlx::PgPool;
|
|
|
|
async fn get_pool() -> PgPool {
|
|
let conn_str = common::get_db_connection_string().await;
|
|
sqlx::postgres::PgPoolOptions::new()
|
|
.max_connections(5)
|
|
.connect(&conn_str)
|
|
.await
|
|
.expect("Failed to connect to test database")
|
|
}
|
|
|
|
#[tokio::test]
|
|
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 res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.bearer_auth(&access_jwt)
|
|
.json(&json!({
|
|
"recipientDid": did,
|
|
"senderDid": "did:plc:admin",
|
|
"content": "Hello, this is a test email from the admin.",
|
|
"subject": "Test Admin Email"
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await.expect("Invalid JSON");
|
|
assert_eq!(body["sent"], true);
|
|
|
|
let user = sqlx::query!("SELECT id FROM users WHERE did = $1", did)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("User not found");
|
|
|
|
let notification = sqlx::query!(
|
|
"SELECT subject, body, notification_type as \"notification_type: String\" FROM notification_queue WHERE user_id = $1 AND notification_type = 'admin_email' ORDER BY created_at DESC LIMIT 1",
|
|
user.id
|
|
)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("Notification not found");
|
|
|
|
assert_eq!(notification.subject.as_deref(), Some("Test Admin Email"));
|
|
assert!(notification.body.contains("Hello, this is a test email from the admin."));
|
|
}
|
|
|
|
#[tokio::test]
|
|
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 res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.bearer_auth(&access_jwt)
|
|
.json(&json!({
|
|
"recipientDid": did,
|
|
"senderDid": "did:plc:admin",
|
|
"content": "Email without subject"
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
let body: Value = res.json().await.expect("Invalid JSON");
|
|
assert_eq!(body["sent"], true);
|
|
|
|
let user = sqlx::query!("SELECT id FROM users WHERE did = $1", did)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("User not found");
|
|
|
|
let notification = sqlx::query!(
|
|
"SELECT subject FROM notification_queue WHERE user_id = $1 AND notification_type = 'admin_email' AND body = 'Email without subject' LIMIT 1",
|
|
user.id
|
|
)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.expect("Notification not found");
|
|
|
|
assert!(notification.subject.is_some());
|
|
assert!(notification.subject.unwrap().contains("Message from"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
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 res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.bearer_auth(&access_jwt)
|
|
.json(&json!({
|
|
"recipientDid": "did:plc:nonexistent",
|
|
"senderDid": "did:plc:admin",
|
|
"content": "Test content"
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
|
let body: Value = res.json().await.expect("Invalid JSON");
|
|
assert_eq!(body["error"], "AccountNotFound");
|
|
}
|
|
|
|
#[tokio::test]
|
|
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 res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.bearer_auth(&access_jwt)
|
|
.json(&json!({
|
|
"recipientDid": did,
|
|
"senderDid": "did:plc:admin",
|
|
"content": ""
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await.expect("Invalid JSON");
|
|
assert_eq!(body["error"], "InvalidRequest");
|
|
}
|
|
|
|
#[tokio::test]
|
|
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 res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.bearer_auth(&access_jwt)
|
|
.json(&json!({
|
|
"recipientDid": "",
|
|
"senderDid": "did:plc:admin",
|
|
"content": "Test content"
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
|
|
let body: Value = res.json().await.expect("Invalid JSON");
|
|
assert_eq!(body["error"], "InvalidRequest");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_send_email_requires_auth() {
|
|
let client = common::client();
|
|
let base_url = common::base_url().await;
|
|
|
|
let res = client
|
|
.post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url))
|
|
.json(&json!({
|
|
"recipientDid": "did:plc:test",
|
|
"senderDid": "did:plc:admin",
|
|
"content": "Test content"
|
|
}))
|
|
.send()
|
|
.await
|
|
.expect("Failed to send email");
|
|
|
|
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
|
|
}
|