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

fix: create admin users if enabled and zero users already

This commit is contained in:
Ivar Conradi Østhus 2021-10-12 21:27:06 +02:00
parent 9ce3df8086
commit 3a35e16f1f
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
4 changed files with 28 additions and 2 deletions

View File

@ -87,6 +87,13 @@ class UserStore {
this.logger = getLogger('user-store.js');
}
async count(): Promise<number> {
return this.db
.count('*')
.from(TABLE)
.then((res) => Number(res[0].count));
}
async update(id: number, fields: IUserUpdateFields): Promise<User> {
await this.db(TABLE)
.where('id', id)

View File

@ -114,9 +114,9 @@ class UserService {
}
async initAdminUser(): Promise<void> {
const hasAdminUser = await this.store.hasUser({ username: 'admin' });
const userCount = await this.store.count();
if (!hasAdminUser) {
if (userCount === 0) {
// create default admin user
try {
const pwd = 'unleash4all';

View File

@ -55,6 +55,21 @@ test('should create initial admin user', async () => {
).toBeTruthy();
});
test('should not init default user if we already have users', async () => {
await userService.createUser({
username: 'test',
password: 'A very strange P4ssw0rd_',
rootRole: adminRole.id,
});
await userService.initAdminUser();
const users = await userService.getAll();
expect(users).toHaveLength(1);
expect(users[0].username).toBe('test');
await expect(async () =>
userService.loginUser('admin', 'unleash4all'),
).rejects.toThrow(Error);
});
test('should not be allowed to create existing user', async () => {
await userStore.insert({ username: 'test', name: 'Hans Mola' });
await expect(async () =>

View File

@ -27,6 +27,10 @@ class UserStoreMock extends UserStore {
return user;
}
async count(): Promise<number> {
return Promise.resolve(this.data.length);
}
async insert(user: User): Promise<User> {
// eslint-disable-next-line no-param-reassign
user.id = this.idSeq;