59 lines
2.0 KiB
JavaScript
59 lines
2.0 KiB
JavaScript
import { mkdir, writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
import dotenv from 'dotenv';
|
|
|
|
const root = process.cwd();
|
|
const dataKey = 'BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc=';
|
|
const email = 'e2e-admin@example.com';
|
|
const password = 'e2e-pass-123';
|
|
const png = Buffer.from(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
|
|
'base64',
|
|
);
|
|
|
|
export default async function globalSetup() {
|
|
dotenv.config({ path: path.join(root, 'backend/.env'), quiet: true });
|
|
process.env.NODE_ENV = 'development';
|
|
process.env.DB_NAME = 'ppaimt_crest_test';
|
|
process.env.DATA_ENCRYPTION_KEY = dataKey;
|
|
|
|
const { default: knex } = await import('knex');
|
|
const { default: config } = await import(
|
|
pathToFileURL(path.join(root, 'backend/knexfile.js')).href
|
|
);
|
|
const db = knex(config);
|
|
const argon2 = await import('argon2');
|
|
const passwordHash = await argon2.default.hash(password, { type: argon2.default.argon2id });
|
|
const existing = await db('admin_users').where({ email }).first();
|
|
|
|
if (existing) {
|
|
await db('admin_users').where({ id: existing.id }).update({
|
|
password_hash: passwordHash,
|
|
is_active: true,
|
|
failed_login_count: 0,
|
|
locked_until: null,
|
|
});
|
|
} else {
|
|
await db('admin_users').insert({
|
|
name: 'E2E Admin',
|
|
email,
|
|
password_hash: passwordHash,
|
|
is_active: true,
|
|
});
|
|
}
|
|
|
|
const { seedSurveyDefinition } = await import(
|
|
pathToFileURL(path.join(root, 'backend/src/survey/seed.js')).href
|
|
);
|
|
await seedSurveyDefinition(db);
|
|
|
|
const signatureKey = 'settings/signatory-signature.png';
|
|
await mkdir(path.join(root, 'backend/.local-storage/settings'), { recursive: true });
|
|
await writeFile(path.join(root, 'backend/.local-storage', signatureKey), png);
|
|
await db('settings')
|
|
.where({ key: 'signatory_signature_key' })
|
|
.update({ value: JSON.stringify(signatureKey) });
|
|
await db.destroy();
|
|
}
|