From 9b314a161cc56fc98fbb75590003991700d498be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Fri, 3 Jun 2022 17:44:34 +0200 Subject: [PATCH] fix(core): Fix `user-management:reset` command (#3403) * :bug: Fix `email` setting * :test_tube: Add test * :test_tube: Add expectation for user email * :zap: Replace request with helper --- packages/cli/src/databases/entities/User.ts | 2 +- .../integration/commands/reset.cmd.test.ts | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/cli/test/integration/commands/reset.cmd.test.ts diff --git a/packages/cli/src/databases/entities/User.ts b/packages/cli/src/databases/entities/User.ts index 598d0c6fe1c90..20043251ea6f9 100644 --- a/packages/cli/src/databases/entities/User.ts +++ b/packages/cli/src/databases/entities/User.ts @@ -133,7 +133,7 @@ export class User { @BeforeInsert() @BeforeUpdate() preUpsertHook(): void { - this.email = this.email?.toLowerCase(); + this.email = this.email?.toLowerCase() ?? null; this.updatedAt = new Date(); } diff --git a/packages/cli/test/integration/commands/reset.cmd.test.ts b/packages/cli/test/integration/commands/reset.cmd.test.ts new file mode 100644 index 0000000000000..bc770178785ed --- /dev/null +++ b/packages/cli/test/integration/commands/reset.cmd.test.ts @@ -0,0 +1,44 @@ +import { execSync } from 'child_process'; + +import express from 'express'; +import path from 'path'; + +import { Db } from '../../../src'; +import * as utils from '../shared/utils'; +import type { Role } from '../../../src/databases/entities/Role'; +import * as testDb from '../shared/testDb'; +import { randomEmail, randomName, randomValidPassword } from '../shared/random'; + +let app: express.Application; +let testDbName = ''; +let globalOwnerRole: Role; + +beforeAll(async () => { + app = utils.initTestServer({ endpointGroups: ['owner'], applyAuth: true }); + const initResult = await testDb.init(); + testDbName = initResult.testDbName; + + globalOwnerRole = await testDb.getGlobalOwnerRole(); +}); + +afterAll(async () => { + await testDb.terminate(testDbName); +}); + +test('user-management:reset should reset DB to default user state', async () => { + await testDb.createUser({ globalRole: globalOwnerRole }); + + const command = [path.join('bin', 'n8n'), 'user-management:reset'].join(' '); + + execSync(command); + + const user = await Db.collections.User.findOne(); + + expect(user?.email).toBeNull(); + expect(user?.firstName).toBeNull(); + expect(user?.lastName).toBeNull(); + expect(user?.password).toBeNull(); + expect(user?.resetPasswordToken).toBeNull(); + expect(user?.resetPasswordTokenExpiration).toBeNull(); + expect(user?.personalizationAnswers).toBeNull(); +});