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