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