Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion application/backend/src/utils/createAdmin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ describe('Test create admin script', () => {
})
it('Creates admin user if specified in env file', async () => {
process.env['ORG_ADMIN_EMAIL'] = 'admin@test.com'
process.env['ORG_ADMIN_PASSWORD'] = 'tespassword'
process.env['ORG_ADMIN_PASSWORD'] = 'Ap9!hK2vBmN4qXr7'
await createAdmin()
const admin_users = await prisma.user.findMany({ where: { role: 'OrganisationAdmin' } })
expect(admin_users).toHaveLength(3)
})
it('Throws if ORG_ADMIN_PASSWORD does not meet strength policy', async () => {
process.env['ORG_ADMIN_EMAIL'] = 'weak-admin@test.com'
process.env['ORG_ADMIN_PASSWORD'] = 'tespassword'
await expect(createAdmin()).rejects.toThrow('ORG_ADMIN_PASSWORD does not meet strength policy')
})
})
8 changes: 8 additions & 0 deletions application/backend/src/utils/createAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { checkPasswordStrength } from 'common/src/PasswordStrength'
import { hashPassword } from '../authentication'
import prisma from '../PrismaClient'

const createAdmin = async () => {
const admin = await prisma.user.findFirst({ where: { email: process.env['ORG_ADMIN_EMAIL'] } })

if (!admin && process.env['ORG_ADMIN_PASSWORD'] && process.env['ORG_ADMIN_EMAIL']) {
const { isValid, fields } = checkPasswordStrength(process.env['ORG_ADMIN_PASSWORD'] as string)
if (!isValid) {
const reasons = Object.values(fields)
.map((f) => f.message)
.join(', ')
throw new Error(`ORG_ADMIN_PASSWORD does not meet strength policy: ${reasons}`)
}
await prisma.user.create({
data: {
email: process.env['ORG_ADMIN_EMAIL'] as string,
Expand Down
Loading