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