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