From 6e234727eec5b0f7afeede79678131315fad5507 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Tue, 16 Jan 2024 11:19:54 +0530 Subject: [PATCH] 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. --- src/lib/types/core.test.ts | 80 ++++++++++++++++++++++++++++++++++++++ src/lib/types/core.ts | 5 +-- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/lib/types/core.test.ts diff --git a/src/lib/types/core.test.ts b/src/lib/types/core.test.ts new file mode 100644 index 0000000000..2420e73bf8 --- /dev/null +++ b/src/lib/types/core.test.ts @@ -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 { + 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); + }); +}); diff --git a/src/lib/types/core.ts b/src/lib/types/core.ts index 1475d07972..3967e5d0fb 100644 --- a/src/lib/types/core.ts +++ b/src/lib/types/core.ts @@ -22,12 +22,11 @@ export interface IUnleash { version: string; } -export const SYSTEM_USER: IUser = { - email: 'systemuser@getunleash.io', +export const SYSTEM_USER: Omit = { 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', };