mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 12:44:07 -08:00
f667b384c9
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
102 lines
3.5 KiB
TypeScript
102 lines
3.5 KiB
TypeScript
import { Container } from 'typedi';
|
|
|
|
import { Reset } from '@/commands/user-management/reset';
|
|
import { LoadNodesAndCredentials } from '@/load-nodes-and-credentials';
|
|
import { NodeTypes } from '@/node-types';
|
|
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';
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
import { setupTestCommand } from '@test-integration/utils/testCommand';
|
|
import { mockInstance } from '../../shared/mocking';
|
|
import * as testDb from '../shared/testDb';
|
|
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';
|
|
|
|
mockInstance(LoadNodesAndCredentials);
|
|
mockInstance(NodeTypes);
|
|
const command = setupTestCommand(Reset);
|
|
|
|
beforeEach(async () => {
|
|
await testDb.truncate(['User']);
|
|
});
|
|
|
|
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);
|
|
|
|
// 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
|
|
//
|
|
await command.run();
|
|
|
|
//
|
|
// 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' }]);
|
|
|
|
// 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' }]);
|
|
|
|
// the instance is marked as not set up:
|
|
await expect(
|
|
Container.get(SettingsRepository).findBy({ key: 'userManagement.isInstanceOwnerSetUp' }),
|
|
).resolves.toMatchObject([{ value: 'false' }]);
|
|
});
|