this repo has no description
1mod common; 2 3use reqwest::StatusCode; 4use serde_json::{Value, json}; 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_admin_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, comms_type as \"comms_type: String\" FROM comms_queue WHERE user_id = $1 AND comms_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!( 50 notification 51 .body 52 .contains("Hello, this is a test email from the admin.") 53 ); 54} 55 56#[tokio::test] 57async fn test_send_email_default_subject() { 58 let client = common::client(); 59 let base_url = common::base_url().await; 60 let pool = get_pool().await; 61 let (access_jwt, did) = common::create_admin_account_and_login(&client).await; 62 let res = client 63 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 64 .bearer_auth(&access_jwt) 65 .json(&json!({ 66 "recipientDid": did, 67 "senderDid": "did:plc:admin", 68 "content": "Email without subject" 69 })) 70 .send() 71 .await 72 .expect("Failed to send email"); 73 assert_eq!(res.status(), StatusCode::OK); 74 let body: Value = res.json().await.expect("Invalid JSON"); 75 assert_eq!(body["sent"], true); 76 let user = sqlx::query!("SELECT id FROM users WHERE did = $1", did) 77 .fetch_one(&pool) 78 .await 79 .expect("User not found"); 80 let notification = sqlx::query!( 81 "SELECT subject FROM comms_queue WHERE user_id = $1 AND comms_type = 'admin_email' AND body = 'Email without subject' LIMIT 1", 82 user.id 83 ) 84 .fetch_one(&pool) 85 .await 86 .expect("Notification not found"); 87 assert!(notification.subject.is_some()); 88 assert!(notification.subject.unwrap().contains("Message from")); 89} 90 91#[tokio::test] 92async fn test_send_email_recipient_not_found() { 93 let client = common::client(); 94 let base_url = common::base_url().await; 95 let (access_jwt, _) = common::create_admin_account_and_login(&client).await; 96 let res = client 97 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 98 .bearer_auth(&access_jwt) 99 .json(&json!({ 100 "recipientDid": "did:plc:nonexistent", 101 "senderDid": "did:plc:admin", 102 "content": "Test content" 103 })) 104 .send() 105 .await 106 .expect("Failed to send email"); 107 assert_eq!(res.status(), StatusCode::NOT_FOUND); 108 let body: Value = res.json().await.expect("Invalid JSON"); 109 assert_eq!(body["error"], "AccountNotFound"); 110} 111 112#[tokio::test] 113async fn test_send_email_missing_content() { 114 let client = common::client(); 115 let base_url = common::base_url().await; 116 let (access_jwt, did) = common::create_admin_account_and_login(&client).await; 117 let res = client 118 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 119 .bearer_auth(&access_jwt) 120 .json(&json!({ 121 "recipientDid": did, 122 "senderDid": "did:plc:admin", 123 "content": "" 124 })) 125 .send() 126 .await 127 .expect("Failed to send email"); 128 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 129 let body: Value = res.json().await.expect("Invalid JSON"); 130 assert_eq!(body["error"], "InvalidRequest"); 131} 132 133#[tokio::test] 134async fn test_send_email_missing_recipient() { 135 let client = common::client(); 136 let base_url = common::base_url().await; 137 let (access_jwt, _) = common::create_admin_account_and_login(&client).await; 138 let res = client 139 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 140 .bearer_auth(&access_jwt) 141 .json(&json!({ 142 "recipientDid": "", 143 "senderDid": "did:plc:admin", 144 "content": "Test content" 145 })) 146 .send() 147 .await 148 .expect("Failed to send email"); 149 assert_eq!(res.status(), StatusCode::BAD_REQUEST); 150 let body: Value = res.json().await.expect("Invalid JSON"); 151 assert_eq!(body["error"], "InvalidRequest"); 152} 153 154#[tokio::test] 155async fn test_send_email_requires_auth() { 156 let client = common::client(); 157 let base_url = common::base_url().await; 158 let res = client 159 .post(format!("{}/xrpc/com.atproto.admin.sendEmail", base_url)) 160 .json(&json!({ 161 "recipientDid": "did:plc:test", 162 "senderDid": "did:plc:admin", 163 "content": "Test content" 164 })) 165 .send() 166 .await 167 .expect("Failed to send email"); 168 assert_eq!(res.status(), StatusCode::UNAUTHORIZED); 169}