From 3459bbecd3177a75fe70bb69ecfb5340409e1819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20H=C3=BCttemann?= Date: Thu, 22 Jun 2023 17:12:48 +0200 Subject: [PATCH] Move user exclusion test to user handler --- .env.example | 2 +- src/app.ts | 9 ++------- src/handlers/users.test.ts | 20 ++++++++++++++++++++ src/handlers/users.ts | 17 +++++++++++++++++ src/helpers/storage.test.ts | 2 +- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index c447e32..52181c2 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,2 @@ REGISTRATION_SHARED_SECRET='look in your synapses homeserver.yaml' -EXCLUDED_USERS='rocket.cat' # Comma-separated list +EXCLUDED_USERS='rocket.cat' # Comma-separated list of usernames or IDs diff --git a/src/app.ts b/src/app.ts index f4630b1..8f91a84 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ dotenv.config() import lineByLine from 'n-readlines' import 'reflect-metadata' import { IdMapping } from './entity/IdMapping' -import { RcUser, createUser } from './handlers/users' +import { RcUser, createUser, userIsExcluded } from './handlers/users' import log from './helpers/logger' import { getRoomId, getUserId, initStorage, save } from './helpers/storage' import { whoami } from './helpers/synapse' @@ -48,12 +48,7 @@ async function loadRcExport(entity: Entities) { const rcUser: RcUser = item log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`) - // Check for exclusion - if ( - rcUser.roles.some((e) => ['app', 'bot'].includes(e)) || - (process.env.EXCLUDED_USERS || '').split(',').includes(rcUser._id) - ) { - log.debug('User excluded. Skipping.') + if (userIsExcluded(rcUser)) { break } diff --git a/src/handlers/users.test.ts b/src/handlers/users.test.ts index b15b354..811ca78 100644 --- a/src/handlers/users.test.ts +++ b/src/handlers/users.test.ts @@ -1,4 +1,5 @@ process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow' +process.env.EXCLUDED_USERS = 'excludedUser1,excludedUser2' import { expect, jest, test } from '@jest/globals' import axios from 'axios' import * as storage from '../helpers/storage' @@ -8,6 +9,7 @@ import { createUser, generateHmac, mapUser, + userIsExcluded, } from '../handlers/users' jest.mock('axios') @@ -76,3 +78,21 @@ test('creating users', async () => { ) expect(mockedStorage.createMembership).toHaveBeenCalledTimes(2) }) + +test('users are excluded', () => { + expect(userIsExcluded(rcUser)).toBeFalsy() + expect(userIsExcluded({ ...rcUser, _id: 'excludedUser1' })).toBeTruthy() + expect(userIsExcluded({ ...rcUser, username: 'excludedUser2' })).toBeTruthy() + expect(userIsExcluded({ ...rcUser, roles: ['bot'] })).toBeTruthy() + expect( + userIsExcluded({ ...rcUser, roles: [...rcUser.__rooms, 'app'] }) + ).toBeTruthy() + expect( + userIsExcluded({ + ...rcUser, + _id: 'excludedUser2', + username: 'excludedUser1', + roles: [...rcUser.__rooms, 'app', 'bot'], + }) + ).toBeTruthy() +}) diff --git a/src/handlers/users.ts b/src/handlers/users.ts index 85680fb..74b5566 100644 --- a/src/handlers/users.ts +++ b/src/handlers/users.ts @@ -74,6 +74,23 @@ async function parseUserMemberships(rcUser: RcUser): Promise { ) } +export function userIsExcluded(rcUser: RcUser): boolean { + const reasons: string[] = [] + const excludedUsers = (process.env.EXCLUDED_USERS || '').split(',') + if (rcUser.roles.includes('app')) reasons.push('has role "app"') + if (rcUser.roles.includes('bot')) reasons.push('has role "bot"') + if (excludedUsers.includes(rcUser._id)) + reasons.push(`id "${rcUser._id}" is on exclusion list`) + if (excludedUsers.includes(rcUser.username)) + reasons.push(`username "${rcUser.username}" is on exclusion list`) + + if (reasons.length > 0) { + log.debug(`User ${rcUser.name} is excluded: ${reasons.join(', ')}`) + return true + } + return false +} + export async function createUser(rcUser: RcUser): Promise { const user = mapUser(rcUser) const nonce = await getUserRegistrationNonce() diff --git a/src/helpers/storage.test.ts b/src/helpers/storage.test.ts index f909dbe..d579979 100644 --- a/src/helpers/storage.test.ts +++ b/src/helpers/storage.test.ts @@ -28,7 +28,7 @@ beforeAll(async () => { await initStorage() }) -test('save mapping', async () => { +test('create mapping', async () => { await expect(save(mapping)).resolves.toBe(undefined) })