Skip to content

Commit 0a6048b

Browse files
committed
Add a test for the syn2mas SynapseReader
1 parent 10883df commit 0a6048b

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--
2+
INSERT INTO users
3+
(
4+
name,
5+
password_hash,
6+
creation_ts,
7+
admin,
8+
upgrade_ts,
9+
is_guest,
10+
appservice_id,
11+
consent_version,
12+
consent_server_notice_sent,
13+
user_type,
14+
deactivated,
15+
shadow_banned,
16+
consent_ts,
17+
approved,
18+
locked,
19+
suspended
20+
)
21+
VALUES
22+
(
23+
'@alice:example.com',
24+
'$2b$12$aaa/aaaaaaaaaa.aaaaaaaaaaaaaaa./aaaaaaaaaaaaaaaaaaa/A',
25+
1530393962,
26+
0,
27+
NULL,
28+
0,
29+
NULL,
30+
'1.0',
31+
'1.0',
32+
NULL,
33+
0,
34+
NULL,
35+
NULL,
36+
NULL,
37+
false,
38+
false
39+
);
40+

crates/syn2mas/src/synapse_reader.rs renamed to crates/syn2mas/src/synapse_reader/mod.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub enum Error {
2626
},
2727
}
2828

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

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

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

115115
impl From<SecondsTimestamp> for DateTime<Utc> {
@@ -136,7 +136,7 @@ impl sqlx::Type<Postgres> for SecondsTimestamp {
136136
}
137137
}
138138

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

265265
#[cfg(test)]
266266
mod test {
267+
use std::collections::BTreeSet;
268+
269+
use futures_util::TryStreamExt;
270+
use insta::assert_debug_snapshot;
271+
use sqlx::{migrate::Migrator, PgPool};
272+
273+
use crate::{synapse_reader::SynapseUser, SynapseReader};
274+
267275
// TODO test me
276+
static MIGRATOR: Migrator = sqlx::migrate!("./test_synapse_migrations");
277+
278+
#[sqlx::test(migrator = "MIGRATOR", fixtures("user_alice"))]
279+
async fn test_read_users(pool: PgPool) {
280+
let mut conn = pool.acquire().await.expect("failed to get connection");
281+
let mut reader = SynapseReader::new(&mut conn, false)
282+
.await
283+
.expect("failed to make SynapseReader");
284+
285+
let users: BTreeSet<SynapseUser> = reader
286+
.read_users()
287+
.try_collect()
288+
.await
289+
.expect("failed to read Synapse users");
290+
291+
assert_debug_snapshot!(users);
292+
}
268293
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
source: crates/syn2mas/src/synapse_reader/mod.rs
3+
expression: users
4+
---
5+
{
6+
SynapseUser {
7+
name: FullUserId(
8+
"@alice:example.com",
9+
),
10+
password_hash: Some(
11+
"$2b$12$aaa/aaaaaaaaaa.aaaaaaaaaaaaaaa./aaaaaaaaaaaaaaaaaaa/A",
12+
),
13+
admin: SynapseBool(
14+
false,
15+
),
16+
deactivated: SynapseBool(
17+
false,
18+
),
19+
creation_ts: SecondsTimestamp(
20+
2018-06-30T21:26:02Z,
21+
),
22+
},
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 `users` table from Synapse
7+
8+
CREATE TABLE users (
9+
name text,
10+
password_hash text,
11+
creation_ts bigint,
12+
admin smallint DEFAULT 0 NOT NULL,
13+
upgrade_ts bigint,
14+
is_guest smallint DEFAULT 0 NOT NULL,
15+
appservice_id text,
16+
consent_version text,
17+
consent_server_notice_sent text,
18+
user_type text,
19+
deactivated smallint DEFAULT 0 NOT NULL,
20+
shadow_banned boolean,
21+
consent_ts bigint,
22+
approved boolean,
23+
locked boolean DEFAULT false NOT NULL,
24+
suspended boolean DEFAULT false NOT NULL
25+
);
26+

0 commit comments

Comments
 (0)