2024-06-18 01:50:39 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
|
2022-11-09 06:25:00 -08:00
|
|
|
import { Reset } from '@/commands/user-management/reset';
|
2023-02-21 10:21:56 -08:00
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
2024-06-18 01:50:39 -07:00
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
|
|
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
|
|
|
|
import { CredentialsRepository } from '@db/repositories/credentials.repository';
|
|
|
|
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
|
|
|
import { SettingsRepository } from '@db/repositories/settings.repository';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
|
2024-06-18 01:50:39 -07:00
|
|
|
import { setupTestCommand } from '@test-integration/utils/testCommand';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
|
|
|
import * as testDb from '../shared/testDb';
|
2024-05-17 01:53:15 -07:00
|
|
|
import { createMember, createUser } from '../shared/db/users';
|
|
|
|
import { createWorkflow } from '../shared/db/workflows';
|
|
|
|
import { getPersonalProject } from '../shared/db/projects';
|
|
|
|
import { encryptCredentialData, saveCredential } from '../shared/db/credentials';
|
|
|
|
import { randomCredentialPayload } from '../shared/random';
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2024-06-18 01:50:39 -07:00
|
|
|
mockInstance(InternalHooks);
|
|
|
|
mockInstance(LoadNodesAndCredentials);
|
|
|
|
mockInstance(NodeTypes);
|
|
|
|
const command = setupTestCommand(Reset);
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
beforeEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate(['User']);
|
2022-06-25 21:03:46 -07:00
|
|
|
});
|
|
|
|
|
2023-05-24 00:45:37 -07:00
|
|
|
// eslint-disable-next-line n8n-local-rules/no-skipped-tests
|
2024-05-17 01:53:15 -07:00
|
|
|
test('user-management:reset should reset DB to default user state', async () => {
|
|
|
|
//
|
|
|
|
// ARRANGE
|
|
|
|
//
|
|
|
|
const owner = await createUser({ role: 'global:owner' });
|
|
|
|
const ownerProject = await getPersonalProject(owner);
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2024-05-17 01:53:15 -07:00
|
|
|
// should be deleted
|
|
|
|
const member = await createMember();
|
|
|
|
|
|
|
|
// should be re-owned
|
|
|
|
const workflow = await createWorkflow({}, member);
|
|
|
|
const credential = await saveCredential(randomCredentialPayload(), {
|
|
|
|
user: member,
|
|
|
|
role: 'credential:owner',
|
|
|
|
});
|
|
|
|
|
|
|
|
// dangling credentials should also be re-owned
|
|
|
|
const danglingCredential = await Container.get(CredentialsRepository).save(
|
|
|
|
await encryptCredentialData(Object.assign(new CredentialsEntity(), randomCredentialPayload())),
|
|
|
|
);
|
|
|
|
|
|
|
|
// mark instance as set up
|
|
|
|
await Container.get(SettingsRepository).update(
|
|
|
|
{ key: 'userManagement.isInstanceOwnerSetUp' },
|
|
|
|
{ value: 'true' },
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// ACT
|
|
|
|
//
|
2024-06-18 01:50:39 -07:00
|
|
|
await command.run();
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2024-05-17 01:53:15 -07:00
|
|
|
//
|
|
|
|
// ASSERT
|
|
|
|
//
|
|
|
|
|
|
|
|
// check if the owner account was reset:
|
|
|
|
await expect(
|
|
|
|
Container.get(UserRepository).findOneBy({ role: 'global:owner' }),
|
|
|
|
).resolves.toMatchObject({
|
|
|
|
email: null,
|
|
|
|
firstName: null,
|
|
|
|
lastName: null,
|
|
|
|
password: null,
|
|
|
|
personalizationAnswers: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
// all members were deleted:
|
|
|
|
const members = await Container.get(UserRepository).findOneBy({ role: 'global:member' });
|
|
|
|
expect(members).toBeNull();
|
|
|
|
|
|
|
|
// all workflows are owned by the owner:
|
|
|
|
await expect(
|
|
|
|
Container.get(SharedWorkflowRepository).findBy({ workflowId: workflow.id }),
|
|
|
|
).resolves.toMatchObject([{ projectId: ownerProject.id, role: 'workflow:owner' }]);
|
|
|
|
|
|
|
|
// all credentials are owned by the owner
|
|
|
|
await expect(
|
|
|
|
Container.get(SharedCredentialsRepository).findBy({ credentialsId: credential.id }),
|
|
|
|
).resolves.toMatchObject([{ projectId: ownerProject.id, role: 'credential:owner' }]);
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2024-05-17 01:53:15 -07:00
|
|
|
// all dangling credentials are owned by the owner
|
|
|
|
await expect(
|
|
|
|
Container.get(SharedCredentialsRepository).findBy({ credentialsId: danglingCredential.id }),
|
|
|
|
).resolves.toMatchObject([{ projectId: ownerProject.id, role: 'credential:owner' }]);
|
2022-06-03 08:44:34 -07:00
|
|
|
|
2024-05-17 01:53:15 -07:00
|
|
|
// the instance is marked as not set up:
|
|
|
|
await expect(
|
|
|
|
Container.get(SettingsRepository).findBy({ key: 'userManagement.isInstanceOwnerSetUp' }),
|
|
|
|
).resolves.toMatchObject([{ value: 'false' }]);
|
2022-06-03 08:44:34 -07:00
|
|
|
});
|