1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +02:00
unleash.unleash/src/lib/util/extract-user.test.ts
Gastón Fournier ceaaf3d0f3
feat: admin token calls get an admin token user (#5924)
## About the changes
Whenever we get a call from an admin token we want to associate it with
the [admin token
user](4d42093a07/src/lib/types/core.ts (L34-L41)).
This should give us the needed audit for this type of calls that
currently were lacking a user id (we only stored a string with the token
name in the event log).

We consciously decided not to use `id` as the property to prevent any
unforeseen side effects. The reason is that only `IUser` type has an id
and adding an id to `IApiUser` might lead to confusion.
2024-01-17 16:55:59 +01:00

36 lines
1.2 KiB
TypeScript

import { SYSTEM_USER } from '../../lib/types';
import { IUser } from '../server-impl';
import { extractUserIdFromUser, extractUsernameFromUser } from './extract-user';
describe('extractUsernameFromUser', () => {
test('Should return the email if it exists', () => {
const user = {
email: 'ratatoskr@yggdrasil.com',
username: 'ratatoskr',
} as IUser;
expect(extractUsernameFromUser(user)).toBe(user.email);
});
test('Should return the username if it exists and email does not', () => {
const user = {
username: 'ratatoskr',
} as IUser;
expect(extractUsernameFromUser(user)).toBe(user.username);
});
test('Should return the system user if neither email nor username exists', () => {
const user = {} as IUser;
expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username);
expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id);
});
test('Should return the system user if user is null', () => {
const user = null as unknown as IUser;
expect(extractUsernameFromUser(user)).toBe(SYSTEM_USER.username);
expect(extractUserIdFromUser(user)).toBe(SYSTEM_USER.id);
});
});