this repo has no description
1mod common; 2use bspds::notifications::{ 3 enqueue_notification, enqueue_welcome, NewNotification, NotificationChannel, 4 NotificationStatus, NotificationType, 5}; 6use sqlx::PgPool; 7async fn get_pool() -> PgPool { 8 let conn_str = common::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#[tokio::test] 16async fn test_enqueue_notification() { 17 let pool = get_pool().await; 18 let (_, did) = common::create_account_and_login(&common::client()).await; 19 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did) 20 .fetch_one(&pool) 21 .await 22 .expect("User not found"); 23 let notification = NewNotification::email( 24 user_id, 25 NotificationType::Welcome, 26 "test@example.com".to_string(), 27 "Test Subject".to_string(), 28 "Test body".to_string(), 29 ); 30 let notification_id = enqueue_notification(&pool, notification) 31 .await 32 .expect("Failed to enqueue notification"); 33 let row = sqlx::query!( 34 r#" 35 SELECT 36 id, user_id, recipient, subject, body, 37 channel as "channel: NotificationChannel", 38 notification_type as "notification_type: NotificationType", 39 status as "status: NotificationStatus" 40 FROM notification_queue 41 WHERE id = $1 42 "#, 43 notification_id 44 ) 45 .fetch_one(&pool) 46 .await 47 .expect("Notification not found"); 48 assert_eq!(row.user_id, user_id); 49 assert_eq!(row.recipient, "test@example.com"); 50 assert_eq!(row.subject.as_deref(), Some("Test Subject")); 51 assert_eq!(row.body, "Test body"); 52 assert_eq!(row.channel, NotificationChannel::Email); 53 assert_eq!(row.notification_type, NotificationType::Welcome); 54 assert_eq!(row.status, NotificationStatus::Pending); 55} 56#[tokio::test] 57async fn test_enqueue_welcome() { 58 let pool = get_pool().await; 59 let (_, did) = common::create_account_and_login(&common::client()).await; 60 let user_row = sqlx::query!("SELECT id, email, handle FROM users WHERE did = $1", did) 61 .fetch_one(&pool) 62 .await 63 .expect("User not found"); 64 let notification_id = enqueue_welcome(&pool, user_row.id, "example.com") 65 .await 66 .expect("Failed to enqueue welcome notification"); 67 let row = sqlx::query!( 68 r#" 69 SELECT 70 recipient, subject, body, 71 notification_type as "notification_type: NotificationType" 72 FROM notification_queue 73 WHERE id = $1 74 "#, 75 notification_id 76 ) 77 .fetch_one(&pool) 78 .await 79 .expect("Notification not found"); 80 assert_eq!(Some(row.recipient), user_row.email); 81 assert_eq!(row.subject.as_deref(), Some("Welcome to example.com")); 82 assert!(row.body.contains(&format!("@{}", user_row.handle))); 83 assert_eq!(row.notification_type, NotificationType::Welcome); 84} 85#[tokio::test] 86async fn test_notification_queue_status_index() { 87 let pool = get_pool().await; 88 let (_, did) = common::create_account_and_login(&common::client()).await; 89 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did) 90 .fetch_one(&pool) 91 .await 92 .expect("User not found"); 93 let initial_count: i64 = sqlx::query_scalar!( 94 "SELECT COUNT(*) FROM notification_queue WHERE status = 'pending' AND user_id = $1", 95 user_id 96 ) 97 .fetch_one(&pool) 98 .await 99 .expect("Failed to count") 100 .unwrap_or(0); 101 for i in 0..5 { 102 let notification = NewNotification::email( 103 user_id, 104 NotificationType::PasswordReset, 105 format!("test{}@example.com", i), 106 "Test".to_string(), 107 "Body".to_string(), 108 ); 109 enqueue_notification(&pool, notification) 110 .await 111 .expect("Failed to enqueue"); 112 } 113 let final_count: i64 = sqlx::query_scalar!( 114 "SELECT COUNT(*) FROM notification_queue WHERE status = 'pending' AND user_id = $1", 115 user_id 116 ) 117 .fetch_one(&pool) 118 .await 119 .expect("Failed to count") 120 .unwrap_or(0); 121 assert_eq!(final_count - initial_count, 5); 122}