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';
|
2024-08-22 02:10:37 -07:00
|
|
|
import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials';
|
|
|
|
import { NodeTypes } from '@/node-types';
|
2024-08-27 07:44:32 -07:00
|
|
|
import { SharedWorkflowRepository } from '@/databases/repositories/shared-workflow.repository';
|
|
|
|
import { SharedCredentialsRepository } from '@/databases/repositories/shared-credentials.repository';
|
2024-08-27 08:24:20 -07:00
|
|
|
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
|
2024-08-27 07:44:32 -07:00
|
|
|
import { CredentialsEntity } from '@/databases/entities/credentials-entity';
|
2024-08-27 08:24:20 -07:00
|
|
|
import { SettingsRepository } from '@/databases/repositories/settings.repository';
|
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
2023-11-10 06:04:26 -08:00
|
|
|
|
2024-08-28 04:59:27 -07:00
|
|
|
import { setupTestCommand } from '@test-integration/utils/test-command';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2024-08-28 04:59:27 -07:00
|
|
|
import * as testDb from '../shared/test-db';
|
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(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
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|