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