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