this repo has no description
1mod common;
2use common::{base_url, client, create_account_and_login, get_test_db_pool};
3use serde_json::{Value, json};
4use tranquil_pds::comms::{CommsType, NewComms, enqueue_comms};
5
6#[tokio::test]
7async fn test_get_notification_history() {
8 let client = client();
9 let base = base_url().await;
10 let pool = get_test_db_pool().await;
11 let (token, did) = create_account_and_login(&client).await;
12
13 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did)
14 .fetch_one(pool)
15 .await
16 .expect("User not found");
17
18 for i in 0..3 {
19 let comms = NewComms::email(
20 user_id,
21 CommsType::Welcome,
22 "test@example.com".to_string(),
23 format!("Subject {}", i),
24 format!("Body {}", i),
25 );
26 enqueue_comms(pool, comms)
27 .await
28 .expect("Failed to enqueue");
29 }
30
31 let resp = client
32 .get(format!(
33 "{}/xrpc/com.tranquil.account.getNotificationHistory",
34 base
35 ))
36 .header("Authorization", format!("Bearer {}", token))
37 .send()
38 .await
39 .unwrap();
40
41 assert_eq!(resp.status(), 200);
42 let body: Value = resp.json().await.unwrap();
43 let notifications = body["notifications"].as_array().unwrap();
44 assert_eq!(notifications.len(), 5);
45
46 assert_eq!(notifications[0]["subject"], "Subject 2");
47 assert_eq!(notifications[1]["subject"], "Subject 1");
48 assert_eq!(notifications[2]["subject"], "Subject 0");
49}
50
51#[tokio::test]
52async fn test_verify_channel_discord() {
53 let client = client();
54 let base = base_url().await;
55 let (token, did) = create_account_and_login(&client).await;
56
57 let prefs = json!({
58 "discordId": "123456789"
59 });
60 let resp = client
61 .post(format!(
62 "{}/xrpc/com.tranquil.account.updateNotificationPrefs",
63 base
64 ))
65 .header("Authorization", format!("Bearer {}", token))
66 .json(&prefs)
67 .send()
68 .await
69 .unwrap();
70 assert_eq!(resp.status(), 200);
71 let body: Value = resp.json().await.unwrap();
72 assert!(
73 body["verificationRequired"]
74 .as_array()
75 .unwrap()
76 .contains(&json!("discord"))
77 );
78
79 let pool = get_test_db_pool().await;
80 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did)
81 .fetch_one(pool)
82 .await
83 .expect("User not found");
84
85 let row = sqlx::query!(
86 "SELECT body, metadata FROM comms_queue WHERE user_id = $1 AND comms_type = 'channel_verification' ORDER BY created_at DESC LIMIT 1",
87 user_id
88 )
89 .fetch_one(pool)
90 .await
91 .expect("Verification code not found");
92
93 let code = row
94 .metadata
95 .as_ref()
96 .and_then(|m| m.get("code"))
97 .and_then(|c| c.as_str())
98 .expect("No code in metadata");
99
100 let input = json!({
101 "channel": "discord",
102 "identifier": "123456789",
103 "code": code
104 });
105 let resp = client
106 .post(format!(
107 "{}/xrpc/com.tranquil.account.confirmChannelVerification",
108 base
109 ))
110 .header("Authorization", format!("Bearer {}", token))
111 .json(&input)
112 .send()
113 .await
114 .unwrap();
115 assert_eq!(resp.status(), 200);
116
117 let resp = client
118 .get(format!(
119 "{}/xrpc/com.tranquil.account.getNotificationPrefs",
120 base
121 ))
122 .header("Authorization", format!("Bearer {}", token))
123 .send()
124 .await
125 .unwrap();
126 let body: Value = resp.json().await.unwrap();
127 assert_eq!(body["discordVerified"], true);
128 assert_eq!(body["discordId"], "123456789");
129}
130
131#[tokio::test]
132async fn test_verify_channel_invalid_code() {
133 let client = client();
134 let base = base_url().await;
135 let (token, _did) = create_account_and_login(&client).await;
136
137 let prefs = json!({
138 "telegramUsername": "testuser"
139 });
140 let resp = client
141 .post(format!(
142 "{}/xrpc/com.tranquil.account.updateNotificationPrefs",
143 base
144 ))
145 .header("Authorization", format!("Bearer {}", token))
146 .json(&prefs)
147 .send()
148 .await
149 .unwrap();
150 assert_eq!(resp.status(), 200);
151
152 let input = json!({
153 "channel": "telegram",
154 "identifier": "testuser",
155 "code": "XXXX-XXXX-XXXX-XXXX"
156 });
157 let resp = client
158 .post(format!(
159 "{}/xrpc/com.tranquil.account.confirmChannelVerification",
160 base
161 ))
162 .header("Authorization", format!("Bearer {}", token))
163 .json(&input)
164 .send()
165 .await
166 .unwrap();
167 assert!(
168 resp.status() == 400 || resp.status() == 422,
169 "Expected 400 or 422, got {}",
170 resp.status()
171 );
172}
173
174#[tokio::test]
175async fn test_verify_channel_not_set() {
176 let client = client();
177 let base = base_url().await;
178 let (token, _did) = create_account_and_login(&client).await;
179
180 let input = json!({
181 "channel": "signal",
182 "identifier": "123456",
183 "code": "XXXX-XXXX-XXXX-XXXX"
184 });
185 let resp = client
186 .post(format!(
187 "{}/xrpc/com.tranquil.account.confirmChannelVerification",
188 base
189 ))
190 .header("Authorization", format!("Bearer {}", token))
191 .json(&input)
192 .send()
193 .await
194 .unwrap();
195 assert!(
196 resp.status() == 400 || resp.status() == 422,
197 "Expected 400 or 422, got {}",
198 resp.status()
199 );
200}
201
202#[tokio::test]
203async fn test_update_email_via_notification_prefs() {
204 let client = client();
205 let base = base_url().await;
206 let pool = get_test_db_pool().await;
207 let (token, did) = create_account_and_login(&client).await;
208
209 let unique_email = format!("newemail_{}@example.com", uuid::Uuid::new_v4());
210 let prefs = json!({
211 "email": unique_email
212 });
213 let resp = client
214 .post(format!(
215 "{}/xrpc/com.tranquil.account.updateNotificationPrefs",
216 base
217 ))
218 .header("Authorization", format!("Bearer {}", token))
219 .json(&prefs)
220 .send()
221 .await
222 .unwrap();
223 assert_eq!(resp.status(), 200);
224 let body: Value = resp.json().await.unwrap();
225 assert!(
226 body["verificationRequired"]
227 .as_array()
228 .unwrap()
229 .contains(&json!("email"))
230 );
231
232 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did)
233 .fetch_one(pool)
234 .await
235 .expect("User not found");
236
237 let body_text: String = sqlx::query_scalar!(
238 "SELECT body FROM comms_queue WHERE user_id = $1 AND comms_type = 'email_update' ORDER BY created_at DESC LIMIT 1",
239 user_id
240 )
241 .fetch_one(pool)
242 .await
243 .expect("Verification code not found");
244
245 let code = body_text
246 .lines()
247 .skip_while(|line| !line.contains("verification code"))
248 .nth(1)
249 .map(|line| line.trim().to_string())
250 .filter(|line| !line.is_empty() && line.contains('-'))
251 .unwrap_or_else(|| {
252 body_text
253 .lines()
254 .find(|line| {
255 let trimmed = line.trim();
256 trimmed.starts_with("MX") && trimmed.contains('-')
257 })
258 .map(|s| s.trim().to_string())
259 .unwrap_or_default()
260 });
261
262 let input = json!({
263 "channel": "email",
264 "identifier": unique_email,
265 "code": code
266 });
267 let resp = client
268 .post(format!(
269 "{}/xrpc/com.tranquil.account.confirmChannelVerification",
270 base
271 ))
272 .header("Authorization", format!("Bearer {}", token))
273 .json(&input)
274 .send()
275 .await
276 .unwrap();
277 assert_eq!(resp.status(), 200);
278
279 let resp = client
280 .get(format!(
281 "{}/xrpc/com.tranquil.account.getNotificationPrefs",
282 base
283 ))
284 .header("Authorization", format!("Bearer {}", token))
285 .send()
286 .await
287 .unwrap();
288 let body: Value = resp.json().await.unwrap();
289 assert_eq!(body["email"], unique_email);
290}