-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1867 from automatisch/custom-user-seed
add POST /api/v1/installation/users to seed user
- Loading branch information
Showing
13 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/backend/src/controllers/api/v1/installation/users/create-user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import User from '../../../../../models/user.js'; | ||
|
||
export default async (request, response) => { | ||
const { email, password, fullName } = request.body; | ||
|
||
await User.createAdmin({ email, password, fullName }); | ||
|
||
response.status(204).end(); | ||
}; |
84 changes: 84 additions & 0 deletions
84
packages/backend/src/controllers/api/v1/installation/users/create-user.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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 { createUser } from '../../../../../../test/factories/user'; | ||
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 when no user', 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); | ||
}); | ||
|
||
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', () => { | ||
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); | ||
}); | ||
}) | ||
}); |
17 changes: 17 additions & 0 deletions
17
...ages/backend/src/db/migrations/20240507135944_update_installation_completed_for_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export async function up(knex) { | ||
const users = await knex('users').limit(1); | ||
|
||
// no user implies installation is not completed yet. | ||
if (users.length === 0) return; | ||
|
||
await knex('config').insert({ | ||
key: 'installation.completed', | ||
value: { | ||
data: true | ||
} | ||
}); | ||
}; | ||
|
||
export async function down(knex) { | ||
await knex('config').where({ key: 'installation.completed' }).delete(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Router } from 'express'; | ||
import asyncHandler from 'express-async-handler'; | ||
import { allowInstallation } from '../../../../helpers/allow-installation.js'; | ||
import createUserAction from '../../../../controllers/api/v1/installation/users/create-user.js'; | ||
|
||
const router = Router(); | ||
|
||
router.post( | ||
'/', | ||
allowInstallation, | ||
asyncHandler(createUserAction) | ||
); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters