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