feat: add POST /api/v1/installation/users to seed user
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import User from '../../../../../models/user.js';
|
||||
import Config from '../../../../../models/config.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const { email, password, fullName } = request.body;
|
||||
|
||||
await User.createAdminUser({ email, password, fullName });
|
||||
|
||||
await Config.markInstallationCompleted();
|
||||
|
||||
response.status(204).end();
|
||||
};
|
@@ -0,0 +1,62 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import app from '../../../../../app.js';
|
||||
import Config from '../../../../../models/config.js';
|
||||
import User from '../../../../../models/user.js';
|
||||
import { createRole } from '../../../../../../test/factories/role';
|
||||
import { createInstallationCompletedConfig } from '../../../../../../test/factories/config';
|
||||
|
||||
describe('POST /api/v1/installation/users', () => {
|
||||
let adminRole;
|
||||
|
||||
beforeEach(async () => {
|
||||
adminRole = await createRole({
|
||||
name: 'Admin',
|
||||
key: 'admin',
|
||||
})
|
||||
});
|
||||
|
||||
describe('for incomplete installations', () => {
|
||||
it('should respond with HTTP 204 with correct payload', async () => {
|
||||
expect(await Config.isInstallationCompleted()).toBe(false);
|
||||
|
||||
await request(app)
|
||||
.post('/api/v1/installation/users')
|
||||
.send({
|
||||
email: 'user@automatisch.io',
|
||||
password: 'password',
|
||||
fullName: 'Initial admin'
|
||||
})
|
||||
.expect(204);
|
||||
|
||||
const user = await User.query().findOne({ email: 'user@automatisch.io' });
|
||||
|
||||
expect(user.roleId).toBe(adminRole.id);
|
||||
expect(await Config.isInstallationCompleted()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('for completed installations', () => {
|
||||
beforeEach(async () => {
|
||||
await createInstallationCompletedConfig();
|
||||
});
|
||||
|
||||
it('should respond with HTTP 403 when installation completed', async () => {
|
||||
expect(await Config.isInstallationCompleted()).toBe(true);
|
||||
|
||||
await request(app)
|
||||
.post('/api/v1/installation/users')
|
||||
.send({
|
||||
email: 'user@automatisch.io',
|
||||
password: 'password',
|
||||
fullName: 'Initial admin'
|
||||
})
|
||||
.expect(403);
|
||||
|
||||
const user = await User.query().findOne({ email: 'user@automatisch.io' });
|
||||
|
||||
expect(user).toBeUndefined();
|
||||
expect(await Config.isInstallationCompleted()).toBe(true);
|
||||
});
|
||||
})
|
||||
});
|
Reference in New Issue
Block a user