feat: 打通 Jenkins 直构导入闭环
This commit is contained in:
@@ -16,7 +16,10 @@ import { ConfigService } from '@nestjs/config';
|
||||
describe('AuthService', () => {
|
||||
const passwordService = new PasswordService();
|
||||
|
||||
async function createService(options?: { superAdminPassword?: string }) {
|
||||
async function createService(options?: {
|
||||
superAdminPassword?: string;
|
||||
nodeEnv?: EnvConfig['NODE_ENV'];
|
||||
}) {
|
||||
const adminPassword =
|
||||
options?.superAdminPassword ?? SUPER_ADMIN_INITIAL_PASSWORD;
|
||||
const adminHash = await passwordService.hashPassword(adminPassword);
|
||||
@@ -113,7 +116,7 @@ describe('AuthService', () => {
|
||||
}
|
||||
|
||||
if (key === 'NODE_ENV') {
|
||||
return 'test';
|
||||
return options?.nodeEnv ?? 'test';
|
||||
}
|
||||
|
||||
return undefined;
|
||||
@@ -163,6 +166,17 @@ describe('AuthService', () => {
|
||||
expect(result.user.role).toBe('super_admin');
|
||||
});
|
||||
|
||||
it('生产环境拒绝使用公开默认密码初始化超级管理员', async () => {
|
||||
const { repository, service } = await createService({
|
||||
nodeEnv: 'production',
|
||||
});
|
||||
|
||||
await expect(service.onModuleInit()).rejects.toMatchObject({
|
||||
code: 'CONFIG_MISSING',
|
||||
});
|
||||
expect(repository.ensureSuperAdmin).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('拒绝超级管理员使用忘记密码流程', async () => {
|
||||
const { service } = await createService();
|
||||
|
||||
|
||||
@@ -285,6 +285,14 @@ export class AuthService implements OnModuleInit {
|
||||
return password;
|
||||
}
|
||||
|
||||
if (this.config.get('NODE_ENV', { infer: true }) === 'production') {
|
||||
throw new AppError(
|
||||
'CONFIG_MISSING',
|
||||
'生产环境缺少 SUPER_ADMIN_INITIAL_PASSWORD,不能使用公开默认密码初始化超级管理员',
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
return SUPER_ADMIN_INITIAL_PASSWORD;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,19 @@ const prisma = new PrismaClient();
|
||||
const passwordService = new PasswordService();
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const password = process.env.SUPER_ADMIN_RESET_PASSWORD?.trim()
|
||||
? process.env.SUPER_ADMIN_RESET_PASSWORD.trim()
|
||||
: SUPER_ADMIN_INITIAL_PASSWORD;
|
||||
const configuredPassword = process.env.SUPER_ADMIN_RESET_PASSWORD?.trim();
|
||||
const password =
|
||||
configuredPassword ||
|
||||
(process.env.NODE_ENV === 'production'
|
||||
? undefined
|
||||
: SUPER_ADMIN_INITIAL_PASSWORD);
|
||||
|
||||
if (!password) {
|
||||
throw new Error(
|
||||
'生产环境缺少 SUPER_ADMIN_RESET_PASSWORD,不能使用公开默认密码重置超级管理员',
|
||||
);
|
||||
}
|
||||
|
||||
const passwordHash = await passwordService.hashPassword(password);
|
||||
|
||||
await prisma.user.upsert({
|
||||
|
||||
Reference in New Issue
Block a user