Skip to content

Commit c016199

Browse files
authored
syn2mas: Add tests for reading and writing threepids (#3907)
* Add `MasWriter` test for e-mail and unsupported threepids * Add `SynapseReader` test for e-mail and unsupported threepids
1 parent 69bbcfc commit c016199

File tree

7 files changed

+186
-4
lines changed

7 files changed

+186
-4
lines changed

crates/syn2mas/src/mas_writer/mod.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,9 @@ mod test {
854854
use uuid::Uuid;
855855

856856
use crate::{
857-
mas_writer::{MasNewUser, MasNewUserPassword},
857+
mas_writer::{
858+
MasNewEmailThreepid, MasNewUnsupportedThreepid, MasNewUser, MasNewUserPassword,
859+
},
858860
LockedMasDatabase, MasWriter,
859861
};
860862

@@ -1018,4 +1020,69 @@ mod test {
10181020

10191021
assert_db_snapshot!(&mut conn);
10201022
}
1023+
1024+
/// Tests writing a single user, with an e-mail address associated.
1025+
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
1026+
async fn test_write_user_with_email(pool: PgPool) {
1027+
let mut conn = pool.acquire().await.unwrap();
1028+
let mut writer = make_mas_writer(&pool, &mut conn).await;
1029+
1030+
writer
1031+
.write_users(vec![MasNewUser {
1032+
user_id: Uuid::from_u128(1u128),
1033+
username: "alice".to_owned(),
1034+
created_at: DateTime::default(),
1035+
locked_at: None,
1036+
can_request_admin: false,
1037+
}])
1038+
.await
1039+
.expect("failed to write user");
1040+
1041+
writer
1042+
.write_email_threepids(vec![MasNewEmailThreepid {
1043+
user_email_id: Uuid::from_u128(2u128),
1044+
user_id: Uuid::from_u128(1u128),
1045+
email: "[email protected]".to_owned(),
1046+
created_at: DateTime::default(),
1047+
}])
1048+
.await
1049+
.expect("failed to write e-mail");
1050+
1051+
writer.finish().await.expect("failed to finish MasWriter");
1052+
1053+
assert_db_snapshot!(&mut conn);
1054+
}
1055+
1056+
/// Tests writing a single user, with a unsupported third-party ID
1057+
/// associated.
1058+
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
1059+
async fn test_write_user_with_unsupported_threepid(pool: PgPool) {
1060+
let mut conn = pool.acquire().await.unwrap();
1061+
let mut writer = make_mas_writer(&pool, &mut conn).await;
1062+
1063+
writer
1064+
.write_users(vec![MasNewUser {
1065+
user_id: Uuid::from_u128(1u128),
1066+
username: "alice".to_owned(),
1067+
created_at: DateTime::default(),
1068+
locked_at: None,
1069+
can_request_admin: false,
1070+
}])
1071+
.await
1072+
.expect("failed to write user");
1073+
1074+
writer
1075+
.write_unsupported_threepids(vec![MasNewUnsupportedThreepid {
1076+
user_id: Uuid::from_u128(1u128),
1077+
medium: "msisdn".to_owned(),
1078+
address: "441189998819991197253".to_owned(),
1079+
created_at: DateTime::default(),
1080+
}])
1081+
.await
1082+
.expect("failed to write phone number (unsupported threepid)");
1083+
1084+
writer.finish().await.expect("failed to finish MasWriter");
1085+
1086+
assert_db_snapshot!(&mut conn);
1087+
}
10211088
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: crates/syn2mas/src/mas_writer/mod.rs
3+
expression: db_snapshot
4+
---
5+
user_emails:
6+
- confirmed_at: "1970-01-01 00:00:00+00"
7+
created_at: "1970-01-01 00:00:00+00"
8+
email: alice@example.org
9+
user_email_id: 00000000-0000-0000-0000-000000000002
10+
user_id: 00000000-0000-0000-0000-000000000001
11+
users:
12+
- can_request_admin: "false"
13+
created_at: "1970-01-01 00:00:00+00"
14+
locked_at: ~
15+
primary_user_email_id: ~
16+
user_id: 00000000-0000-0000-0000-000000000001
17+
username: alice
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: crates/syn2mas/src/mas_writer/mod.rs
3+
expression: db_snapshot
4+
---
5+
user_unsupported_third_party_ids:
6+
- address: "441189998819991197253"
7+
created_at: "1970-01-01 00:00:00+00"
8+
medium: msisdn
9+
user_id: 00000000-0000-0000-0000-000000000001
10+
users:
11+
- can_request_admin: "false"
12+
created_at: "1970-01-01 00:00:00+00"
13+
locked_at: ~
14+
primary_user_email_id: ~
15+
user_id: 00000000-0000-0000-0000-000000000001
16+
username: alice
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
INSERT INTO user_threepids
2+
(
3+
user_id,
4+
medium,
5+
address,
6+
validated_at,
7+
added_at
8+
)
9+
VALUES
10+
(
11+
'@alice:example.com',
12+
'email',
13+
14+
1554228492026,
15+
1554228549014
16+
),
17+
(
18+
'@alice:example.com',
19+
'msisdn',
20+
'441189998819991197253',
21+
1555228492026,
22+
1555228549014
23+
);

crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl sqlx::Type<Postgres> for SecondsTimestamp {
139139

140140
/// A timestamp stored as the number of milliseconds since the Unix epoch.
141141
/// Note that Synapse stores some timestamps in seconds.
142-
#[derive(Copy, Clone, Debug)]
142+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
143143
pub struct MillisecondsTimestamp(DateTime<Utc>);
144144

145145
impl From<MillisecondsTimestamp> for DateTime<Utc> {
@@ -185,7 +185,7 @@ pub struct SynapseUser {
185185
}
186186

187187
/// Row of the `user_threepids` table in Synapse.
188-
#[derive(Clone, Debug, FromRow)]
188+
#[derive(Clone, Debug, FromRow, PartialEq, Eq, PartialOrd, Ord)]
189189
pub struct SynapseThreepid {
190190
pub user_id: FullUserId,
191191
pub medium: String,
@@ -329,7 +329,10 @@ mod test {
329329
use insta::assert_debug_snapshot;
330330
use sqlx::{migrate::Migrator, PgPool};
331331

332-
use crate::{synapse_reader::SynapseUser, SynapseReader};
332+
use crate::{
333+
synapse_reader::{SynapseThreepid, SynapseUser},
334+
SynapseReader,
335+
};
333336

334337
// TODO test me
335338
static MIGRATOR: Migrator = sqlx::migrate!("./test_synapse_migrations");
@@ -349,4 +352,20 @@ mod test {
349352

350353
assert_debug_snapshot!(users);
351354
}
355+
356+
#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice", "threepids_alice"))]
357+
async fn test_read_threepids(pool: PgPool) {
358+
let mut conn = pool.acquire().await.expect("failed to get connection");
359+
let mut reader = SynapseReader::new(&mut conn, false)
360+
.await
361+
.expect("failed to make SynapseReader");
362+
363+
let threepids: BTreeSet<SynapseThreepid> = reader
364+
.read_threepids()
365+
.try_collect()
366+
.await
367+
.expect("failed to read Synapse threepids");
368+
369+
assert_debug_snapshot!(threepids);
370+
}
352371
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: crates/syn2mas/src/synapse_reader/mod.rs
3+
expression: threepids
4+
---
5+
{
6+
SynapseThreepid {
7+
user_id: FullUserId(
8+
"@alice:example.com",
9+
),
10+
medium: "email",
11+
address: "[email protected]",
12+
added_at: MillisecondsTimestamp(
13+
2019-04-02T18:09:09.014Z,
14+
),
15+
},
16+
SynapseThreepid {
17+
user_id: FullUserId(
18+
"@alice:example.com",
19+
),
20+
medium: "msisdn",
21+
address: "441189998819991197253",
22+
added_at: MillisecondsTimestamp(
23+
2019-04-14T07:55:49.014Z,
24+
),
25+
},
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Copyright 2025 New Vector Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
-- Brings in the `user_threepids` table from Synapse
7+
8+
CREATE TABLE user_threepids (
9+
user_id text NOT NULL,
10+
medium text NOT NULL,
11+
address text NOT NULL,
12+
validated_at bigint NOT NULL,
13+
added_at bigint NOT NULL
14+
);

0 commit comments

Comments
 (0)