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