mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
f53c482939
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>
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
import type { RoleNames, RoleScopes } from '@db/entities/Role';
|
|
import { Role } from '@db/entities/Role';
|
|
import { RoleService } from '@/services/role.service';
|
|
import { RoleRepository } from '@db/repositories/role.repository';
|
|
import { CacheService } from '@/services/cache/cache.service';
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
import { mockInstance } from '../../shared/mocking';
|
|
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);
|
|
|
|
describe('RoleService', () => {
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
|
const roleRepository = mockInstance(RoleRepository);
|
|
const cacheService = mockInstance(CacheService);
|
|
const roleService = new RoleService(roleRepository, sharedWorkflowRepository, cacheService);
|
|
|
|
const userId = '1';
|
|
const workflowId = '42';
|
|
|
|
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);
|
|
});
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|
|
});
|
|
});
|