1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

chore: align with system user in db (#5893)

This change removes the system user's email from the definition, instead
setting it to `null`. It also changes the name to "Unleash System".

The IUser interface doesn't allow `null` email addresses, so we change
the type definition of the system user to get around it. However, using
`null` (instead of just removing the property entirely) is useful
because when you get the system user from the DB, it's email value will
be null (after it has been nulled out).

As of today, there is nowhere in the Unleash system (OSS or Enterprise)
where we use the system user as an IUser (we only use username and ID).
So this change shouldn't break anything.

This should follow https://github.com/Unleash/unleash/pull/5849.
This commit is contained in:
Thomas Heartman 2024-01-16 11:19:54 +05:30 committed by GitHub
parent 4564c97927
commit 6e234727ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 3 deletions

View File

@ -0,0 +1,80 @@
import { getDbConfig } from '../../test/e2e/helpers/database-config';
import { createTestConfig } from '../../test/config/test-config';
import { log } from 'db-migrate-shared';
import { Client } from 'pg';
import { IDBOption } from 'lib/types';
import { migrateDb } from '../../migrator';
import { SYSTEM_USER } from './core';
log.setLogLevel('error');
async function initSchema(db: IDBOption): Promise<void> {
const client = new Client(db);
await client.connect();
await client.query(`DROP SCHEMA IF EXISTS ${db.schema} CASCADE`);
await client.query(`CREATE SCHEMA IF NOT EXISTS ${db.schema}`);
await client.end();
}
describe('System user definitions in code and db', () => {
let dbDefinition: {
email: string | null;
username: string;
name: string;
id: number;
image_url: string | null;
};
beforeAll(async () => {
jest.setTimeout(15000);
const config = createTestConfig({
db: {
...getDbConfig(),
pool: { min: 1, max: 4 },
schema: 'system_user_alignment_test',
ssl: false,
},
});
await initSchema(config.db);
const e2e = {
...config.db,
connectionTimeoutMillis: 2000,
};
await migrateDb(config);
const client = new Client(config.db);
await client.connect();
const query = await client.query(
`select * from system_user_alignment_test.users where id = -1337;`,
);
dbDefinition = query.rows[0];
});
test('usernames match', () => {
expect(SYSTEM_USER.username).toBe(dbDefinition.username);
});
test('ids match', () => {
expect(SYSTEM_USER.id).toBe(dbDefinition.id);
});
test('names match', () => {
expect(SYSTEM_USER.name).toBe(dbDefinition.name);
});
test('emails match', () => {
expect('email' in SYSTEM_USER).toBe(false);
expect(dbDefinition.email).toBe(null);
});
test('image URLs are both falsy', () => {
expect(Boolean(SYSTEM_USER.imageUrl)).toBe(
Boolean(dbDefinition.image_url),
);
});
test('isApi is false on variable definition', () => {
// we don't set this in the DB, so let's just test the
// definition
expect(SYSTEM_USER.isAPI).toBe(false);
});
});

View File

@ -22,12 +22,11 @@ export interface IUnleash {
version: string;
}
export const SYSTEM_USER: IUser = {
email: 'systemuser@getunleash.io',
export const SYSTEM_USER: Omit<IUser, 'email'> = {
id: -1337,
imageUrl: '',
isAPI: false,
name: 'Used by unleash internally for performing system actions that have no user',
name: 'Unleash System',
permissions: [],
username: 'unleash_system_user',
};