this repo has no description
1mod common; 2use reqwest::StatusCode; 3use serde_json::{json, Value}; 4use sqlx::PgPool; 5async fn get_pool() -> PgPool { 6 let conn_str = common::get_db_connection_string().await; 7 sqlx::postgres::PgPoolOptions::new() 8 .max_connections(5) 9 .connect(&conn_str) 10 .await 11 .expect("Failed to connect to test database") 12} 13#[tokio::test] 14async fn test_send_email_success() { 15 let client = common::client(); 16 let base_url = common::base_url().await; 17 let pool = get_pool().await; 18 let (access_jwt, did) = common::create_account_and_login(&client).await; 19 let res = client 20 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 21 .bearer_auth(&access_jwt) 22 .json(&json!({ 23 "recipientDid": did, 24 "senderDid": "did:plc:admin", 25 "content": "Hello, this is a test email from the admin.", 26 "subject": "Test Admin Email" 27 })) 28 .send() 29 .await 30 .expect("Failed to send email"); 31 assert_eq!(res.status(), StatusCode::OK); 32 let body: Value = res.json().await.expect("Invalid JSON"); 33 assert_eq!(body["sent"], true); 34 let user = sqlx::query!("SELECT id FROM users WHERE did = $1", did) 35 .fetch_one(&pool) 36 .await 37 .expect("User not found"); 38 let notification = sqlx::query!( 39 "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", 40 user.id 41 ) 42 .fetch_one(&pool) 43 .await 44 .expect("Notification not found"); 45 assert_eq!(notification.subject.as_deref(), Some("Test Admin Email")); 46 assert!(notification.body.contains("Hello, this is a test email from the admin.")); 47} 48#[tokio::test] 49async fn test_send_email_default_subject() { 50 let client = common::client(); 51 let base_url = common::base_url().await; 52 let pool = get_pool().await; 53 let (access_jwt, did) = common::create_account_and_login(&client).await; 54 let res = client 55 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 56 .bearer_auth(&access_jwt) 57 .json(&json!({ 58 "recipientDid": did, 59 "senderDid": "did:plc:admin", 60 "content": "Email without subject" 61 })) 62 .send() 63 .await 64 .expect("Failed to send email"); 65 assert_eq!(res.status(), StatusCode::OK); 66 let body: Value = res.json().await.expect("Invalid JSON"); 67 assert_eq!(body["sent"], true); 68 let user = sqlx::query!("SELECT id FROM users WHERE did = $1", did) 69 .fetch_one(&pool) 70 .await 71 .expect("User not found"); 72 let notification = sqlx::query!( 73 "SELECT subject FROM notification_queue WHERE user_id = $1 AND notification_type = 'admin_email' AND body = 'Email without subject' LIMIT 1", 74 user.id 75 ) 76 .fetch_one(&pool) 77 .await 78 .expect("Notification not found"); 79 assert!(notification.subject.is_some()); 80 assert!(notification.subject.unwrap().contains("Message from")); 81} 82#[tokio::test] 83async fn test_send_email_recipient_not_found() { 84 let client = common::client(); 85 let base_url = common::base_url().await; 86 let (access_jwt, _) = common::create_account_and_login(&client).await; 87 let res = client 88 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 89 .bearer_auth(&access_jwt) 90 .json(&json!({ 91 "recipientDid": "did:plc:nonexistent", 92 "senderDid": "did:plc:admin", 93 "content": "Test content" 94 })) 95 .send() 96 .await 97 .expect("Failed to send email"); 98 assert_eq!(res.status(), StatusCode::NOT_FOUND); 99 let body: Value = res.json().await.expect("Invalid JSON"); 100 assert_eq!(body["error"], "AccountNotFound"); 101} 102#[tokio::test] 103async fn test_send_email_missing_content() { 104 let client = common::client(); 105 let base_url = common::base_url().await; 106 let (access_jwt, did) = common::create_account_and_login(&client).await; 107 let res = client 108 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 109 .bearer_auth(&access_jwt) 110 .json(&json!({ 111 "recipientDid": did, 112 "senderDid": "did:plc:admin", 113 "content": "" 114 })) 115 .send() 116 .await 117 .expect("Failed to send email"); 118 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 119 let body: Value = res.json().await.expect("Invalid JSON"); 120 assert_eq!(body["error"], "InvalidRequest"); 121} 122#[tokio::test] 123async fn test_send_email_missing_recipient() { 124 let client = common::client(); 125 let base_url = common::base_url().await; 126 let (access_jwt, _) = common::create_account_and_login(&client).await; 127 let res = client 128 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 129 .bearer_auth(&access_jwt) 130 .json(&json!({ 131 "recipientDid": "", 132 "senderDid": "did:plc:admin", 133 "content": "Test content" 134 })) 135 .send() 136 .await 137 .expect("Failed to send email"); 138 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 139 let body: Value = res.json().await.expect("Invalid JSON"); 140 assert_eq!(body["error"], "InvalidRequest"); 141} 142#[tokio::test] 143async fn test_send_email_requires_auth() { 144 let client = common::client(); 145 let base_url = common::base_url().await; 146 let res = client 147 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 148 .json(&json!({ 149 "recipientDid": "did:plc:test", 150 "senderDid": "did:plc:admin", 151 "content": "Test content" 152 })) 153 .send() 154 .await 155 .expect("Failed to send email"); 156 assert_eq!(res.status(), StatusCode::UNAUTHORIZED); 157}