check for host

This commit is contained in:
Hazel K 2024-10-07 11:45:58 -04:00
parent adc7b91e3b
commit 39214431f4
2 changed files with 31 additions and 15 deletions

View File

@ -3,9 +3,14 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
interface UserLike {
readonly username: string;
readonly host: string | null;
}
/**
* Checks if the given user represents a system account, such as instance.actor.
*/
export function isSystemAccount(user: { readonly username: string }): boolean {
return user.username.includes('.');
export function isSystemAccount(user: UserLike): boolean {
return user.host == null && user.username.includes('.');
}

View File

@ -7,30 +7,41 @@ import { isSystemAccount } from '@/misc/is-system-account.js';
describe(isSystemAccount, () => {
it('should return true for instance.actor', () => {
expect(isSystemAccount({ username: 'instance.actor' })).toBeTruthy();
expect(isSystemAccount({ username: 'instance.actor', host: null })).toBeTruthy();
});
it('should return true for relay.actor', () => {
expect(isSystemAccount({ username: 'relay.actor' })).toBeTruthy();
expect(isSystemAccount({ username: 'relay.actor', host: null })).toBeTruthy();
});
it('should return true for any username with a dot', () => {
expect(isSystemAccount({ username: 'some.user' })).toBeTruthy();
expect(isSystemAccount({ username: 'some.' })).toBeTruthy();
expect(isSystemAccount({ username: '.user' })).toBeTruthy();
expect(isSystemAccount({ username: '.' })).toBeTruthy();
expect(isSystemAccount({ username: 'some.user', host: null })).toBeTruthy();
expect(isSystemAccount({ username: 'some.', host: null })).toBeTruthy();
expect(isSystemAccount({ username: '.user', host: null })).toBeTruthy();
expect(isSystemAccount({ username: '.', host: null })).toBeTruthy();
});
it('should return true for usernames with multiple dots', () => {
expect(isSystemAccount({ username: 'some.user.account' })).toBeTruthy();
expect(isSystemAccount({ username: '..' })).toBeTruthy();
expect(isSystemAccount({ username: 'some.user.account', host: null })).toBeTruthy();
expect(isSystemAccount({ username: '..', host: null })).toBeTruthy();
});
it('should return false for usernames without a dot', () => {
expect(isSystemAccount({ username: 'instance_actor' })).toBeFalsy();
expect(isSystemAccount({ username: 'instanceactor' })).toBeFalsy();
expect(isSystemAccount({ username: 'relay_actor' })).toBeFalsy();
expect(isSystemAccount({ username: 'relayactor' })).toBeFalsy();
expect(isSystemAccount({ username: '' })).toBeFalsy();
expect(isSystemAccount({ username: 'instance_actor', host: null })).toBeFalsy();
expect(isSystemAccount({ username: 'instanceactor', host: null })).toBeFalsy();
expect(isSystemAccount({ username: 'relay_actor', host: null })).toBeFalsy();
expect(isSystemAccount({ username: 'relayactor', host: null })).toBeFalsy();
expect(isSystemAccount({ username: '', host: null })).toBeFalsy();
});
it('should return false for users from another instance', () => {
expect(isSystemAccount({ username: 'instance.actor', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: 'relay.actor', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: 'some.user', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: 'some.', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: '.user', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: '.', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: 'some.user.account', host: 'example.com' })).toBeFalsy();
expect(isSystemAccount({ username: '..', host: 'example.com' })).toBeFalsy();
});
});