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-07-13 01:14:48 -07:00
|
|
|
import { mockInstance } from '../../integration/shared/utils/';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { RoleService } from '@/services/role.service';
|
|
|
|
import { RoleRepository } from '@/databases/repositories';
|
|
|
|
import { CacheService } from '@/services/cache.service';
|
|
|
|
import { SharedWorkflow } from '@/databases/entities/SharedWorkflow';
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|