n8n/packages/cli/test/unit/repositories/role.repository.test.ts
Iván Ovejero e4f041815a
perf(core): Cache roles (#6803)
* refactor: Create `RoleService`

* refactor: Refactor to use service

* refactor: Move `getUserRoleForWorkflow`

* refactor: Clear out old `RoleService`

* refactor: Consolidate utils into service

* refactor: Remove unused methods

* test: Add tests

* refactor: Remove redundant return types

* refactor: Missing utility

* chore: Remove commented out bit

* refactor: Make `Db.collections.Repository` inaccessible

* chore: Cleanup

* feat: Prepopulate cache

* chore: Remove logging

* fix: Account for tests where roles are undefined

* fix: Restore `prettier.prettierPath`

* test: Account for cache enabled and disabled

* fix: Restore `Role` in `Db.collections`

* refactor: Simplify by removing `orFail`

* refactor: Rename for clarity

* refactor: Use `cacheKey` for readability

* refactor: Validate role before creation

* refacator: Remove redundant `cache` prefix

* ci: Lint fix

* test: Fix e2e
2023-08-03 08:58:36 +02:00

35 lines
1.4 KiB
TypeScript

import { Container } from 'typedi';
import { DataSource, EntityManager } from 'typeorm';
import { mock } from 'jest-mock-extended';
import type { RoleNames, RoleScopes } from '@db/entities/Role';
import { Role } from '@db/entities/Role';
import { RoleRepository } from '@db/repositories/role.repository';
import { mockInstance } from '../../integration/shared/utils/';
import { randomInteger } from '../../integration/shared/random';
describe('RoleRepository', () => {
const entityManager = mockInstance(EntityManager);
const dataSource = mockInstance(DataSource, { manager: entityManager });
dataSource.getMetadata.mockReturnValue(mock());
Object.assign(entityManager, { connection: dataSource });
const roleRepository = Container.get(RoleRepository);
describe('findRole', () => {
test('should return the role when present', async () => {
entityManager.findOne.mockResolvedValueOnce(createRole('global', 'owner'));
const role = await roleRepository.findRole('global', 'owner');
expect(role?.name).toEqual('owner');
expect(role?.scope).toEqual('global');
});
test('should return null otherwise', async () => {
entityManager.findOne.mockResolvedValueOnce(null);
const role = await roleRepository.findRole('global', 'owner');
expect(role).toEqual(null);
});
});
const createRole = (scope: RoleScopes, name: RoleNames) =>
Object.assign(new Role(), { name, scope, id: `${randomInteger()}` });
});