Move user exclusion test to user handler

This commit is contained in:
Henrik Hüttemann 2023-06-22 17:12:48 +02:00
parent 463daefeda
commit 3459bbecd3
No known key found for this signature in database
GPG Key ID: 9F7BD10E0A8A111E
5 changed files with 41 additions and 9 deletions

View File

@ -1,2 +1,2 @@
REGISTRATION_SHARED_SECRET='look in your synapses homeserver.yaml' 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

View File

@ -3,7 +3,7 @@ dotenv.config()
import lineByLine from 'n-readlines' import lineByLine from 'n-readlines'
import 'reflect-metadata' import 'reflect-metadata'
import { IdMapping } from './entity/IdMapping' import { IdMapping } from './entity/IdMapping'
import { RcUser, createUser } from './handlers/users' import { RcUser, createUser, userIsExcluded } from './handlers/users'
import log from './helpers/logger' import log from './helpers/logger'
import { getRoomId, getUserId, initStorage, save } from './helpers/storage' import { getRoomId, getUserId, initStorage, save } from './helpers/storage'
import { whoami } from './helpers/synapse' import { whoami } from './helpers/synapse'
@ -48,12 +48,7 @@ async function loadRcExport(entity: Entities) {
const rcUser: RcUser = item const rcUser: RcUser = item
log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`) log.info(`Parsing user: ${rcUser.name}: ${rcUser._id}`)
// Check for exclusion if (userIsExcluded(rcUser)) {
if (
rcUser.roles.some((e) => ['app', 'bot'].includes(e)) ||
(process.env.EXCLUDED_USERS || '').split(',').includes(rcUser._id)
) {
log.debug('User excluded. Skipping.')
break break
} }

View File

@ -1,4 +1,5 @@
process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow' process.env.REGISTRATION_SHARED_SECRET = 'ThisIsSoSecretWow'
process.env.EXCLUDED_USERS = 'excludedUser1,excludedUser2'
import { expect, jest, test } from '@jest/globals' import { expect, jest, test } from '@jest/globals'
import axios from 'axios' import axios from 'axios'
import * as storage from '../helpers/storage' import * as storage from '../helpers/storage'
@ -8,6 +9,7 @@ import {
createUser, createUser,
generateHmac, generateHmac,
mapUser, mapUser,
userIsExcluded,
} from '../handlers/users' } from '../handlers/users'
jest.mock('axios') jest.mock('axios')
@ -76,3 +78,21 @@ test('creating users', async () => {
) )
expect(mockedStorage.createMembership).toHaveBeenCalledTimes(2) 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()
})

View File

@ -74,6 +74,23 @@ async function parseUserMemberships(rcUser: RcUser): Promise<void> {
) )
} }
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<MatrixUser> { export async function createUser(rcUser: RcUser): Promise<MatrixUser> {
const user = mapUser(rcUser) const user = mapUser(rcUser)
const nonce = await getUserRegistrationNonce() const nonce = await getUserRegistrationNonce()

View File

@ -28,7 +28,7 @@ beforeAll(async () => {
await initStorage() await initStorage()
}) })
test('save mapping', async () => { test('create mapping', async () => {
await expect(save(mapping)).resolves.toBe(undefined) await expect(save(mapping)).resolves.toBe(undefined)
}) })