refactor(installation): improve allow installation guard
This commit is contained in:
@@ -2,6 +2,7 @@ import appConfig from '../../src/config/app.js';
|
|||||||
import logger from '../../src/helpers/logger.js';
|
import logger from '../../src/helpers/logger.js';
|
||||||
import client from './client.js';
|
import client from './client.js';
|
||||||
import User from '../../src/models/user.js';
|
import User from '../../src/models/user.js';
|
||||||
|
import Config from '../../src/models/config.js';
|
||||||
import Role from '../../src/models/role.js';
|
import Role from '../../src/models/role.js';
|
||||||
import '../../src/config/orm.js';
|
import '../../src/config/orm.js';
|
||||||
import process from 'process';
|
import process from 'process';
|
||||||
@@ -45,6 +46,8 @@ export async function createUser(
|
|||||||
if (userCount === 0) {
|
if (userCount === 0) {
|
||||||
const user = await User.query().insertAndFetch(userParams);
|
const user = await User.query().insertAndFetch(userParams);
|
||||||
logger.info(`User has been saved: ${user.email}`);
|
logger.info(`User has been saved: ${user.email}`);
|
||||||
|
|
||||||
|
await Config.markInstallationCompleted();
|
||||||
} else {
|
} else {
|
||||||
logger.info('No need to seed a user.');
|
logger.info('No need to seed a user.');
|
||||||
}
|
}
|
||||||
|
@@ -4,6 +4,7 @@ import app from '../../../../../app.js';
|
|||||||
import Config from '../../../../../models/config.js';
|
import Config from '../../../../../models/config.js';
|
||||||
import User from '../../../../../models/user.js';
|
import User from '../../../../../models/user.js';
|
||||||
import { createRole } from '../../../../../../test/factories/role';
|
import { createRole } from '../../../../../../test/factories/role';
|
||||||
|
import { createUser } from '../../../../../../test/factories/user';
|
||||||
import { createInstallationCompletedConfig } from '../../../../../../test/factories/config';
|
import { createInstallationCompletedConfig } from '../../../../../../test/factories/config';
|
||||||
|
|
||||||
describe('POST /api/v1/installation/users', () => {
|
describe('POST /api/v1/installation/users', () => {
|
||||||
@@ -17,7 +18,7 @@ describe('POST /api/v1/installation/users', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('for incomplete installations', () => {
|
describe('for incomplete installations', () => {
|
||||||
it('should respond with HTTP 204 with correct payload', async () => {
|
it('should respond with HTTP 204 with correct payload when no user', async () => {
|
||||||
expect(await Config.isInstallationCompleted()).toBe(false);
|
expect(await Config.isInstallationCompleted()).toBe(false);
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
@@ -34,6 +35,27 @@ describe('POST /api/v1/installation/users', () => {
|
|||||||
expect(user.roleId).toBe(adminRole.id);
|
expect(user.roleId).toBe(adminRole.id);
|
||||||
expect(await Config.isInstallationCompleted()).toBe(true);
|
expect(await Config.isInstallationCompleted()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should respond with HTTP 403 with correct payload when one user exists at least', async () => {
|
||||||
|
expect(await Config.isInstallationCompleted()).toBe(false);
|
||||||
|
|
||||||
|
await createUser();
|
||||||
|
|
||||||
|
const usersCountBefore = await User.query().resultSize();
|
||||||
|
|
||||||
|
await request(app)
|
||||||
|
.post('/api/v1/installation/users')
|
||||||
|
.send({
|
||||||
|
email: 'user@automatisch.io',
|
||||||
|
password: 'password',
|
||||||
|
fullName: 'Initial admin'
|
||||||
|
})
|
||||||
|
.expect(403);
|
||||||
|
|
||||||
|
const usersCountAfter = await User.query().resultSize();
|
||||||
|
|
||||||
|
expect(usersCountBefore).toEqual(usersCountAfter);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('for completed installations', () => {
|
describe('for completed installations', () => {
|
||||||
|
16
packages/backend/src/helpers/allow-installation.js
Normal file
16
packages/backend/src/helpers/allow-installation.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import Config from '../models/config.js';
|
||||||
|
import User from '../models/user.js';
|
||||||
|
|
||||||
|
export async function allowInstallation(request, response, next) {
|
||||||
|
if (await Config.isInstallationCompleted()) {
|
||||||
|
return response.status(403).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasAnyUsers = await User.query().resultSize() > 0;
|
||||||
|
|
||||||
|
if (hasAnyUsers) {
|
||||||
|
return response.status(403).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
@@ -1,9 +0,0 @@
|
|||||||
import Config from '../models/config.js';
|
|
||||||
|
|
||||||
export async function authorizeInstallation(request, response, next) {
|
|
||||||
if (await Config.isInstallationCompleted()) {
|
|
||||||
return response.status(403).end();
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
};
|
|
@@ -1,13 +1,13 @@
|
|||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import asyncHandler from 'express-async-handler';
|
import asyncHandler from 'express-async-handler';
|
||||||
import { authorizeInstallation } from '../../../../helpers/authorize-installation.js';
|
import { allowInstallation } from '../../../../helpers/allow-installation.js';
|
||||||
import createUserAction from '../../../../controllers/api/v1/installation/users/create-user.js';
|
import createUserAction from '../../../../controllers/api/v1/installation/users/create-user.js';
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
'/',
|
'/',
|
||||||
authorizeInstallation,
|
allowInstallation,
|
||||||
asyncHandler(createUserAction)
|
asyncHandler(createUserAction)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user