2023-04-12 01:59:14 -07:00
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
2023-08-02 23:58:36 -07:00
|
|
|
import type { RoleNames, RoleScopes } from '@db/entities/Role';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { Role } from '@db/entities/Role';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { RoleService } from '@/services/role.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { RoleRepository } from '@db/repositories/role.repository';
|
perf(core): Improve caching service (#8213)
Story: https://linear.app/n8n/issue/PAY-1188
- Implement Redis hashes on the caching service, based on Micha's work
in #7747, adapted from `node-cache-manager-ioredis-yet`. Optimize
workflow ownership lookups and manual webhook lookups with Redis hashes.
- Simplify the caching service by removing all currently unused methods
and options: `enable`, `disable`, `getCache`, `keys`, `keyValues`,
`refreshFunctionEach`, `refreshFunctionMany`, `refreshTtl`, etc.
- Remove the flag `N8N_CACHE_ENABLED`. Currently some features on
`master` are broken with caching disabled, and test webhooks now rely
entirely on caching, for multi-main setup support. We originally
introduced this flag to protect against excessive memory usage, but
total cache usage is low enough that we decided to drop this setting.
Apparently this flag was also never documented.
- Overall caching service refactor: use generics, reduce branching, add
discriminants for cache kinds for better type safety, type caching
events, improve readability, remove outdated docs, etc. Also refactor
and expand caching service tests.
Follow-up to: https://github.com/n8n-io/n8n/pull/8176
---------
Co-authored-by: Michael Auerswald <michael.auerswald@gmail.com>
2024-01-05 02:52:44 -08:00
|
|
|
import { CacheService } from '@/services/cache/cache.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { chooseRandomly } from '../../integration/shared/random';
|
|
|
|
import config from '@/config';
|
|
|
|
|
|
|
|
const ROLE_PROPS: Array<{ name: RoleNames; scope: RoleScopes }> = [
|
|
|
|
{ name: 'owner', scope: 'global' },
|
|
|
|
{ name: 'member', scope: 'global' },
|
|
|
|
{ name: 'owner', scope: 'workflow' },
|
|
|
|
{ name: 'owner', scope: 'credential' },
|
|
|
|
{ name: 'user', scope: 'credential' },
|
|
|
|
{ name: 'editor', scope: 'workflow' },
|
|
|
|
];
|
|
|
|
|
|
|
|
export const uppercaseInitial = (str: string) => str[0].toUpperCase() + str.slice(1);
|
2023-04-12 01:59:14 -07:00
|
|
|
|
|
|
|
describe('RoleService', () => {
|
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
2023-08-02 23:58:36 -07:00
|
|
|
const roleRepository = mockInstance(RoleRepository);
|
|
|
|
const cacheService = mockInstance(CacheService);
|
|
|
|
const roleService = new RoleService(roleRepository, sharedWorkflowRepository, cacheService);
|
2023-04-12 01:59:14 -07:00
|
|
|
|
|
|
|
const userId = '1';
|
|
|
|
const workflowId = '42';
|
|
|
|
|
2023-08-02 23:58:36 -07:00
|
|
|
const { name, scope } = chooseRandomly(ROLE_PROPS);
|
|
|
|
|
|
|
|
const display = {
|
|
|
|
name: uppercaseInitial(name),
|
|
|
|
scope: uppercaseInitial(scope),
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config.load(config.default);
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
[true, false].forEach((cacheEnabled) => {
|
|
|
|
const tag = ['cache', cacheEnabled ? 'enabled' : 'disabled'].join(' ');
|
|
|
|
|
|
|
|
describe(`find${display.scope}${display.name}Role() [${tag}]`, () => {
|
|
|
|
test(`should return the ${scope} ${name} role if found`, async () => {
|
|
|
|
config.set('cache.enabled', cacheEnabled);
|
|
|
|
|
|
|
|
const role = roleRepository.create({ name, scope });
|
|
|
|
roleRepository.findRole.mockResolvedValueOnce(role);
|
|
|
|
const returnedRole = await roleRepository.findRole(scope, name);
|
|
|
|
|
|
|
|
expect(returnedRole).toBe(role);
|
|
|
|
});
|
2023-04-12 01:59:14 -07:00
|
|
|
});
|
|
|
|
|
2023-08-02 23:58:36 -07:00
|
|
|
describe(`findRoleByUserAndWorkflow() [${tag}]`, () => {
|
|
|
|
test('should return the role if a shared workflow is found', async () => {
|
|
|
|
config.set('cache.enabled', cacheEnabled);
|
|
|
|
|
|
|
|
const sharedWorkflow = Object.assign(new SharedWorkflow(), { role: new Role() });
|
|
|
|
sharedWorkflowRepository.findOne.mockResolvedValueOnce(sharedWorkflow);
|
|
|
|
const returnedRole = await roleService.findRoleByUserAndWorkflow(userId, workflowId);
|
|
|
|
|
|
|
|
expect(returnedRole).toBe(sharedWorkflow.role);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return undefined if no shared workflow is found', async () => {
|
|
|
|
config.set('cache.enabled', cacheEnabled);
|
|
|
|
|
|
|
|
sharedWorkflowRepository.findOne.mockResolvedValueOnce(null);
|
|
|
|
const returnedRole = await roleService.findRoleByUserAndWorkflow(userId, workflowId);
|
|
|
|
|
|
|
|
expect(returnedRole).toBeUndefined();
|
|
|
|
});
|
2023-04-12 01:59:14 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|