Move user exclusion test to user handler
This commit is contained in:
parent
463daefeda
commit
3459bbecd3
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
})
|
||||
|
||||
@ -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> {
|
||||
const user = mapUser(rcUser)
|
||||
const nonce = await getUserRegistrationNonce()
|
||||
|
||||
@ -28,7 +28,7 @@ beforeAll(async () => {
|
||||
await initStorage()
|
||||
})
|
||||
|
||||
test('save mapping', async () => {
|
||||
test('create mapping', async () => {
|
||||
await expect(save(mapping)).resolves.toBe(undefined)
|
||||
})
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user