Skip to content

Commit

Permalink
chore: align with system user in db (#5893)
Browse files Browse the repository at this point in the history
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 #5849.
  • Loading branch information
thomasheartman committed Jan 16, 2024
1 parent 4564c97 commit 6e23472
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
80 changes: 80 additions & 0 deletions 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<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);
});
});
5 changes: 2 additions & 3 deletions src/lib/types/core.ts
Expand Up @@ -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',
};
Expand Down

0 comments on commit 6e23472

Please sign in to comment.