this repo has no description
1mod common;
2use common::{base_url, client, create_account_and_login, get_db_connection_string};
3use bspds::notifications::{NewNotification, NotificationType, enqueue_notification};
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 notification = NewNotification::email(
30 user_id,
31 NotificationType::Welcome,
32 "test@example.com".to_string(),
33 format!("Subject {}", i),
34 format!("Body {}", i),
35 );
36 enqueue_notification(&pool, notification).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 prefs = json!({
174 "email": "newemail@example.com"
175 });
176 let resp = client
177 .post(format!("{}/xrpc/com.bspds.account.updateNotificationPrefs", base))
178 .header("Authorization", format!("Bearer {}", token))
179 .json(&prefs)
180 .send()
181 .await
182 .unwrap();
183 assert_eq!(resp.status(), 200);
184 let body: Value = resp.json().await.unwrap();
185 assert!(body["verificationRequired"].as_array().unwrap().contains(&json!("email")));
186
187 let user_id: uuid::Uuid = sqlx::query_scalar!("SELECT id FROM users WHERE did = $1", did)
188 .fetch_one(&pool)
189 .await
190 .expect("User not found");
191
192 let code: String = sqlx::query_scalar!(
193 "SELECT code FROM channel_verifications WHERE user_id = $1 AND channel = 'email'",
194 user_id
195 )
196 .fetch_one(&pool)
197 .await
198 .expect("Verification code not found");
199
200 let input = json!({
201 "channel": "email",
202 "code": code
203 });
204 let resp = client
205 .post(format!("{}/xrpc/com.bspds.account.confirmChannelVerification", base))
206 .header("Authorization", format!("Bearer {}", token))
207 .json(&input)
208 .send()
209 .await
210 .unwrap();
211 assert_eq!(resp.status(), 200);
212
213 let resp = client
214 .get(format!("{}/xrpc/com.bspds.account.getNotificationPrefs", base))
215 .header("Authorization", format!("Bearer {}", token))
216 .send()
217 .await
218 .unwrap();
219 let body: Value = resp.json().await.unwrap();
220 assert_eq!(body["email"], "newemail@example.com");
221}