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