Skip to content

Commit

Permalink
Add a test for the syn2mas SynapseReader
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Jan 27, 2025
1 parent 5eb0525 commit b55402c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
40 changes: 40 additions & 0 deletions crates/syn2mas/src/synapse_reader/fixtures/user_alice.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--
INSERT INTO users
(
name,
password_hash,
creation_ts,
admin,
upgrade_ts,
is_guest,
appservice_id,
consent_version,
consent_server_notice_sent,
user_type,
deactivated,
shadow_banned,
consent_ts,
approved,
locked,
suspended
)
VALUES
(
'@alice:example.com',
'$2b$12$aaa/aaaaaaaaaa.aaaaaaaaaaaaaaa./aaaaaaaaaaaaaaaaaaa/A',
1530393962,
0,
NULL,
0,
NULL,
'1.0',
'1.0',
NULL,
0,
NULL,
NULL,
NULL,
false,
false
);

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Error {
},
}

#[derive(Clone, Debug, sqlx::Decode)]
#[derive(Clone, Debug, sqlx::Decode, PartialEq, Eq, PartialOrd, Ord)]
pub struct FullUserId(pub String);

impl Type<Postgres> for FullUserId {
Expand Down Expand Up @@ -82,7 +82,7 @@ impl FullUserId {
/// A Synapse boolean.
/// Synapse stores booleans as 0 or 1, due to compatibility with old SQLite versions
/// that did not have native boolean support.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct SynapseBool(bool);

impl<'r> sqlx::Decode<'r, Postgres> for SynapseBool {
Expand All @@ -109,7 +109,7 @@ impl From<SynapseBool> for bool {
/// A timestamp stored as the number of seconds since the Unix epoch.
/// Note that Synapse stores MOST timestamps as numbers of **milliseconds** since the Unix epoch.
/// But some timestamps are still stored in seconds.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct SecondsTimestamp(DateTime<Utc>);

impl From<SecondsTimestamp> for DateTime<Utc> {
Expand All @@ -136,7 +136,7 @@ impl sqlx::Type<Postgres> for SecondsTimestamp {
}
}

#[derive(Clone, Debug, FromRow)]
#[derive(Clone, Debug, FromRow, PartialEq, Eq, PartialOrd, Ord)]
pub struct SynapseUser {
/// Full User ID of the user
pub name: FullUserId,
Expand Down Expand Up @@ -264,5 +264,30 @@ impl<'conn> SynapseReader<'conn> {

#[cfg(test)]
mod test {
use std::collections::BTreeSet;

use futures_util::TryStreamExt;
use insta::assert_debug_snapshot;
use sqlx::{migrate::Migrator, PgPool};

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

// TODO test me
static MIGRATOR: Migrator = sqlx::migrate!("./test_synapse_migrations");

#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice"))]
async fn test_read_users(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 users: BTreeSet<SynapseUser> = reader
.read_users()
.try_collect()
.await
.expect("failed to read Synapse users");

assert_debug_snapshot!(users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/syn2mas/src/synapse_reader/mod.rs
expression: users
---
{
SynapseUser {
name: FullUserId(
"@alice:example.com",
),
password_hash: Some(
"$2b$12$aaa/aaaaaaaaaa.aaaaaaaaaaaaaaa./aaaaaaaaaaaaaaaaaaa/A",
),
admin: SynapseBool(
false,
),
deactivated: SynapseBool(
false,
),
creation_ts: SecondsTimestamp(
2018-06-30T21:26:02Z,
),
},
}
26 changes: 26 additions & 0 deletions crates/syn2mas/test_synapse_migrations/20250117064958_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- 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 `users` table from Synapse

CREATE TABLE users (
name text,
password_hash text,
creation_ts bigint,
admin smallint DEFAULT 0 NOT NULL,
upgrade_ts bigint,
is_guest smallint DEFAULT 0 NOT NULL,
appservice_id text,
consent_version text,
consent_server_notice_sent text,
user_type text,
deactivated smallint DEFAULT 0 NOT NULL,
shadow_banned boolean,
consent_ts bigint,
approved boolean,
locked boolean DEFAULT false NOT NULL,
suspended boolean DEFAULT false NOT NULL
);

0 comments on commit b55402c

Please sign in to comment.