this repo has no description
1mod common; 2use common::{base_url, client, create_account_and_login, get_db_connection_string}; 3use bspds::comms::{NewComms, CommsType, enqueue_comms}; 4use serde_json::{Value, json}; 5use sqlx::PgPool; 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).await.expect("Failed to enqueue"); 37 } 38 39 let resp = client 40 .get(format!("{}/xrpc/com.bspds.account.getNotificationHistory", base)) 41 .header("Authorization", format!("Bearer {}", token)) 42 .send() 43 .await 44 .unwrap(); 45 46 assert_eq!(resp.status(), 200); 47 let body: Value = resp.json().await.unwrap(); 48 let notifications = body["notifications"].as_array().unwrap(); 49 assert_eq!(notifications.len(), 5); 50 51 assert_eq!(notifications[0]["subject"], "Subject 2"); 52 assert_eq!(notifications[1]["subject"], "Subject 1"); 53 assert_eq!(notifications[2]["subject"], "Subject 0"); 54} 55 56#[tokio::test] 57async fn test_verify_channel_discord() { 58 let client = client(); 59 let base = base_url().await; 60 let (token, did) = create_account_and_login(&client).await; 61 62 let prefs = json!({ 63 "discordId": "123456789" 64 }); 65 let resp = client 66 .post(format!("{}/xrpc/com.bspds.account.updateNotificationPrefs", base)) 67 .header("Authorization", format!("Bearer {}", token)) 68 .json(&prefs) 69 .send() 70 .await 71 .unwrap(); 72 assert_eq!(resp.status(), 200); 73 let body: Value = resp.json().await.unwrap(); 74 assert!(body["verificationRequired"].as_array().unwrap().contains(&json!("discord"))); 75 76 let pool = get_pool().await; 77 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did) 78 .fetch_one(&pool) 79 .await 80 .expect("User not found"); 81 82 let code: String = sqlx::query_scalar!( 83 "SELECT code FROM channel_verifications WHERE user_id = $1 AND channel = 'discord'", 84 user_id 85 ) 86 .fetch_one(&pool) 87 .await 88 .expect("Verification code not found"); 89 90 let input = json!({ 91 "channel": "discord", 92 "code": code 93 }); 94 let resp = client 95 .post(format!("{}/xrpc/com.bspds.account.confirmChannelVerification", base)) 96 .header("Authorization", format!("Bearer {}", token)) 97 .json(&input) 98 .send() 99 .await 100 .unwrap(); 101 assert_eq!(resp.status(), 200); 102 103 let resp = client 104 .get(format!("{}/xrpc/com.bspds.account.getNotificationPrefs", base)) 105 .header("Authorization", format!("Bearer {}", token)) 106 .send() 107 .await 108 .unwrap(); 109 let body: Value = resp.json().await.unwrap(); 110 assert_eq!(body["discordVerified"], true); 111 assert_eq!(body["discordId"], "123456789"); 112} 113 114#[tokio::test] 115async fn test_verify_channel_invalid_code() { 116 let client = client(); 117 let base = base_url().await; 118 let (token, _did) = create_account_and_login(&client).await; 119 120 let prefs = json!({ 121 "telegramUsername": "testuser" 122 }); 123 let resp = client 124 .post(format!("{}/xrpc/com.bspds.account.updateNotificationPrefs", base)) 125 .header("Authorization", format!("Bearer {}", token)) 126 .json(&prefs) 127 .send() 128 .await 129 .unwrap(); 130 assert_eq!(resp.status(), 200); 131 132 let input = json!({ 133 "channel": "telegram", 134 "code": "000000" 135 }); 136 let resp = client 137 .post(format!("{}/xrpc/com.bspds.account.confirmChannelVerification", base)) 138 .header("Authorization", format!("Bearer {}", token)) 139 .json(&input) 140 .send() 141 .await 142 .unwrap(); 143 assert_eq!(resp.status(), 400); 144} 145 146#[tokio::test] 147async fn test_verify_channel_not_set() { 148 let client = client(); 149 let base = base_url().await; 150 let (token, _did) = create_account_and_login(&client).await; 151 152 let input = json!({ 153 "channel": "signal", 154 "code": "123456" 155 }); 156 let resp = client 157 .post(format!("{}/xrpc/com.bspds.account.confirmChannelVerification", base)) 158 .header("Authorization", format!("Bearer {}", token)) 159 .json(&input) 160 .send() 161 .await 162 .unwrap(); 163 assert_eq!(resp.status(), 400); 164} 165 166#[tokio::test] 167async fn test_update_email_via_notification_prefs() { 168 let client = client(); 169 let base = base_url().await; 170 let pool = get_pool().await; 171 let (token, did) = create_account_and_login(&client).await; 172 173 let unique_email = format!("newemail_{}@example.com", uuid::Uuid::new_v4()); 174 let prefs = json!({ 175 "email": unique_email 176 }); 177 let resp = client 178 .post(format!("{}/xrpc/com.bspds.account.updateNotificationPrefs", base)) 179 .header("Authorization", format!("Bearer {}", token)) 180 .json(&prefs) 181 .send() 182 .await 183 .unwrap(); 184 assert_eq!(resp.status(), 200); 185 let body: Value = resp.json().await.unwrap(); 186 assert!(body["verificationRequired"].as_array().unwrap().contains(&json!("email"))); 187 188 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did) 189 .fetch_one(&pool) 190 .await 191 .expect("User not found"); 192 193 let code: String = sqlx::query_scalar!( 194 "SELECT code FROM channel_verifications WHERE user_id = $1 AND channel = 'email'", 195 user_id 196 ) 197 .fetch_one(&pool) 198 .await 199 .expect("Verification code not found"); 200 201 let input = json!({ 202 "channel": "email", 203 "code": code 204 }); 205 let resp = client 206 .post(format!("{}/xrpc/com.bspds.account.confirmChannelVerification", base)) 207 .header("Authorization", format!("Bearer {}", token)) 208 .json(&input) 209 .send() 210 .await 211 .unwrap(); 212 assert_eq!(resp.status(), 200); 213 214 let resp = client 215 .get(format!("{}/xrpc/com.bspds.account.getNotificationPrefs", base)) 216 .header("Authorization", format!("Bearer {}", token)) 217 .send() 218 .await 219 .unwrap(); 220 let body: Value = resp.json().await.unwrap(); 221 assert_eq!(body["email"], unique_email); 222}