Skip to content

Commit

Permalink
syn2mas: Add tests for reading and writing threepids (#3907)
Browse files Browse the repository at this point in the history
* Add `MasWriter` test for e-mail and unsupported threepids

* Add `SynapseReader` test for e-mail and unsupported threepids
  • Loading branch information
reivilibre authored Jan 29, 2025
1 parent 69bbcfc commit c016199
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 4 deletions.
69 changes: 68 additions & 1 deletion crates/syn2mas/src/mas_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,9 @@ mod test {
use uuid::Uuid;

use crate::{
mas_writer::{MasNewUser, MasNewUserPassword},
mas_writer::{
MasNewEmailThreepid, MasNewUnsupportedThreepid, MasNewUser, MasNewUserPassword,
},
LockedMasDatabase, MasWriter,
};

Expand Down Expand Up @@ -1018,4 +1020,69 @@ mod test {

assert_db_snapshot!(&mut conn);
}

/// Tests writing a single user, with an e-mail address associated.
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
async fn test_write_user_with_email(pool: PgPool) {
let mut conn = pool.acquire().await.unwrap();
let mut writer = make_mas_writer(&pool, &mut conn).await;

writer
.write_users(vec![MasNewUser {
user_id: Uuid::from_u128(1u128),
username: "alice".to_owned(),
created_at: DateTime::default(),
locked_at: None,
can_request_admin: false,
}])
.await
.expect("failed to write user");

writer
.write_email_threepids(vec![MasNewEmailThreepid {
user_email_id: Uuid::from_u128(2u128),
user_id: Uuid::from_u128(1u128),
email: "[email protected]".to_owned(),
created_at: DateTime::default(),
}])
.await
.expect("failed to write e-mail");

writer.finish().await.expect("failed to finish MasWriter");

assert_db_snapshot!(&mut conn);
}

/// Tests writing a single user, with a unsupported third-party ID
/// associated.
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
async fn test_write_user_with_unsupported_threepid(pool: PgPool) {
let mut conn = pool.acquire().await.unwrap();
let mut writer = make_mas_writer(&pool, &mut conn).await;

writer
.write_users(vec![MasNewUser {
user_id: Uuid::from_u128(1u128),
username: "alice".to_owned(),
created_at: DateTime::default(),
locked_at: None,
can_request_admin: false,
}])
.await
.expect("failed to write user");

writer
.write_unsupported_threepids(vec![MasNewUnsupportedThreepid {
user_id: Uuid::from_u128(1u128),
medium: "msisdn".to_owned(),
address: "441189998819991197253".to_owned(),
created_at: DateTime::default(),
}])
.await
.expect("failed to write phone number (unsupported threepid)");

writer.finish().await.expect("failed to finish MasWriter");

assert_db_snapshot!(&mut conn);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/syn2mas/src/mas_writer/mod.rs
expression: db_snapshot
---
user_emails:
- confirmed_at: "1970-01-01 00:00:00+00"
created_at: "1970-01-01 00:00:00+00"
email: alice@example.org
user_email_id: 00000000-0000-0000-0000-000000000002
user_id: 00000000-0000-0000-0000-000000000001
users:
- can_request_admin: "false"
created_at: "1970-01-01 00:00:00+00"
locked_at: ~
primary_user_email_id: ~
user_id: 00000000-0000-0000-0000-000000000001
username: alice
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/syn2mas/src/mas_writer/mod.rs
expression: db_snapshot
---
user_unsupported_third_party_ids:
- address: "441189998819991197253"
created_at: "1970-01-01 00:00:00+00"
medium: msisdn
user_id: 00000000-0000-0000-0000-000000000001
users:
- can_request_admin: "false"
created_at: "1970-01-01 00:00:00+00"
locked_at: ~
primary_user_email_id: ~
user_id: 00000000-0000-0000-0000-000000000001
username: alice
23 changes: 23 additions & 0 deletions crates/syn2mas/src/synapse_reader/fixtures/threepids_alice.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
INSERT INTO user_threepids
(
user_id,
medium,
address,
validated_at,
added_at
)
VALUES
(
'@alice:example.com',
'email',
'[email protected]',
1554228492026,
1554228549014
),
(
'@alice:example.com',
'msisdn',
'441189998819991197253',
1555228492026,
1555228549014
);
25 changes: 22 additions & 3 deletions crates/syn2mas/src/synapse_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl sqlx::Type<Postgres> for SecondsTimestamp {

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

impl From<MillisecondsTimestamp> for DateTime<Utc> {
Expand Down Expand Up @@ -185,7 +185,7 @@ pub struct SynapseUser {
}

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

use crate::{synapse_reader::SynapseUser, SynapseReader};
use crate::{
synapse_reader::{SynapseThreepid, SynapseUser},
SynapseReader,
};

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

assert_debug_snapshot!(users);
}

#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice", "threepids_alice"))]
async fn test_read_threepids(pool: PgPool) {
let mut conn = pool.acquire().await.expect("failed to get connection");
let mut reader = SynapseReader::new(&mut conn, false)
.await
.expect("failed to make SynapseReader");

let threepids: BTreeSet<SynapseThreepid> = reader
.read_threepids()
.try_collect()
.await
.expect("failed to read Synapse threepids");

assert_debug_snapshot!(threepids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: crates/syn2mas/src/synapse_reader/mod.rs
expression: threepids
---
{
SynapseThreepid {
user_id: FullUserId(
"@alice:example.com",
),
medium: "email",
address: "[email protected]",
added_at: MillisecondsTimestamp(
2019-04-02T18:09:09.014Z,
),
},
SynapseThreepid {
user_id: FullUserId(
"@alice:example.com",
),
medium: "msisdn",
address: "441189998819991197253",
added_at: MillisecondsTimestamp(
2019-04-14T07:55:49.014Z,
),
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Copyright 2025 New Vector Ltd.
--
-- SPDX-License-Identifier: AGPL-3.0-only
-- Please see LICENSE in the repository root for full details.

-- Brings in the `user_threepids` table from Synapse

CREATE TABLE user_threepids (
user_id text NOT NULL,
medium text NOT NULL,
address text NOT NULL,
validated_at bigint NOT NULL,
added_at bigint NOT NULL
);

0 comments on commit c016199

Please sign in to comment.