From c0161995a7ddda63f71d55f8c569bb1af2c1839c Mon Sep 17 00:00:00 2001 From: reivilibre Date: Wed, 29 Jan 2025 14:15:58 +0000 Subject: [PATCH] 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 --- crates/syn2mas/src/mas_writer/mod.rs | 69 ++++++++++++++++++- ...s_writer__test__write_user_with_email.snap | 17 +++++ ..._write_user_with_unsupported_threepid.snap | 16 +++++ .../fixtures/threepids_alice.sql | 23 +++++++ crates/syn2mas/src/synapse_reader/mod.rs | 25 ++++++- ..._synapse_reader__test__read_threepids.snap | 26 +++++++ .../20250128141011_threepids.sql | 14 ++++ 7 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_email.snap create mode 100644 crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_unsupported_threepid.snap create mode 100644 crates/syn2mas/src/synapse_reader/fixtures/threepids_alice.sql create mode 100644 crates/syn2mas/src/synapse_reader/snapshots/syn2mas__synapse_reader__test__read_threepids.snap create mode 100644 crates/syn2mas/test_synapse_migrations/20250128141011_threepids.sql diff --git a/crates/syn2mas/src/mas_writer/mod.rs b/crates/syn2mas/src/mas_writer/mod.rs index 0d0528aff..853a6f5fc 100644 --- a/crates/syn2mas/src/mas_writer/mod.rs +++ b/crates/syn2mas/src/mas_writer/mod.rs @@ -854,7 +854,9 @@ mod test { use uuid::Uuid; use crate::{ - mas_writer::{MasNewUser, MasNewUserPassword}, + mas_writer::{ + MasNewEmailThreepid, MasNewUnsupportedThreepid, MasNewUser, MasNewUserPassword, + }, LockedMasDatabase, MasWriter, }; @@ -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: "alice@example.org".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); + } } diff --git a/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_email.snap b/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_email.snap new file mode 100644 index 000000000..6d0e5b6a9 --- /dev/null +++ b/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_email.snap @@ -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 diff --git a/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_unsupported_threepid.snap b/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_unsupported_threepid.snap new file mode 100644 index 000000000..79805555a --- /dev/null +++ b/crates/syn2mas/src/mas_writer/snapshots/syn2mas__mas_writer__test__write_user_with_unsupported_threepid.snap @@ -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 diff --git a/crates/syn2mas/src/synapse_reader/fixtures/threepids_alice.sql b/crates/syn2mas/src/synapse_reader/fixtures/threepids_alice.sql new file mode 100644 index 000000000..526c00c2c --- /dev/null +++ b/crates/syn2mas/src/synapse_reader/fixtures/threepids_alice.sql @@ -0,0 +1,23 @@ +INSERT INTO user_threepids + ( + user_id, + medium, + address, + validated_at, + added_at + ) + VALUES + ( + '@alice:example.com', + 'email', + 'alice@example.com', + 1554228492026, + 1554228549014 + ), + ( + '@alice:example.com', + 'msisdn', + '441189998819991197253', + 1555228492026, + 1555228549014 + ); diff --git a/crates/syn2mas/src/synapse_reader/mod.rs b/crates/syn2mas/src/synapse_reader/mod.rs index 4f1ed0af2..4af714319 100644 --- a/crates/syn2mas/src/synapse_reader/mod.rs +++ b/crates/syn2mas/src/synapse_reader/mod.rs @@ -139,7 +139,7 @@ impl sqlx::Type 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); impl From for DateTime { @@ -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, @@ -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"); @@ -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 = reader + .read_threepids() + .try_collect() + .await + .expect("failed to read Synapse threepids"); + + assert_debug_snapshot!(threepids); + } } diff --git a/crates/syn2mas/src/synapse_reader/snapshots/syn2mas__synapse_reader__test__read_threepids.snap b/crates/syn2mas/src/synapse_reader/snapshots/syn2mas__synapse_reader__test__read_threepids.snap new file mode 100644 index 000000000..b89874956 --- /dev/null +++ b/crates/syn2mas/src/synapse_reader/snapshots/syn2mas__synapse_reader__test__read_threepids.snap @@ -0,0 +1,26 @@ +--- +source: crates/syn2mas/src/synapse_reader/mod.rs +expression: threepids +--- +{ + SynapseThreepid { + user_id: FullUserId( + "@alice:example.com", + ), + medium: "email", + address: "alice@example.com", + 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, + ), + }, +} diff --git a/crates/syn2mas/test_synapse_migrations/20250128141011_threepids.sql b/crates/syn2mas/test_synapse_migrations/20250128141011_threepids.sql new file mode 100644 index 000000000..2ff655979 --- /dev/null +++ b/crates/syn2mas/test_synapse_migrations/20250128141011_threepids.sql @@ -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 +);