2023-07-31 02:37:09 -07:00
|
|
|
import { OwnershipService } from '@/services/ownership.service';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
|
|
import { Role } from '@db/entities/Role';
|
|
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import { User } from '@db/entities/User';
|
|
|
|
import { RoleService } from '@/services/role.service';
|
|
|
|
import type { SharedCredentials } from '@db/entities/SharedCredentials';
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2023-12-05 01:11:18 -08:00
|
|
|
import { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
|
2023-12-11 04:35:09 -08:00
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-12-13 03:41:06 -08:00
|
|
|
import {
|
|
|
|
mockCredRole,
|
|
|
|
mockCredential,
|
|
|
|
mockUser,
|
|
|
|
mockInstanceOwnerRole,
|
|
|
|
wfOwnerRole,
|
|
|
|
} from '../shared/mockObjects';
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-07-31 02:37:09 -07:00
|
|
|
describe('OwnershipService', () => {
|
2023-08-02 23:58:36 -07:00
|
|
|
const roleService = mockInstance(RoleService);
|
2023-12-11 04:35:09 -08:00
|
|
|
const userRepository = mockInstance(UserRepository);
|
2023-07-31 02:37:09 -07:00
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
|
|
|
|
|
|
|
const ownershipService = new OwnershipService(
|
2023-12-11 04:35:09 -08:00
|
|
|
mock(),
|
|
|
|
userRepository,
|
2023-08-02 23:58:36 -07:00
|
|
|
roleService,
|
2023-07-31 02:37:09 -07:00
|
|
|
sharedWorkflowRepository,
|
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
describe('OwnershipService', () => {
|
|
|
|
const roleService = mockInstance(RoleService);
|
|
|
|
const userRepository = mockInstance(UserRepository);
|
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const ownershipService = new OwnershipService(
|
|
|
|
mock(),
|
|
|
|
userRepository,
|
|
|
|
roleService,
|
|
|
|
sharedWorkflowRepository,
|
|
|
|
);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2023-07-31 02:37:09 -07:00
|
|
|
});
|
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
describe('getWorkflowOwner()', () => {
|
|
|
|
test('should retrieve a workflow owner', async () => {
|
|
|
|
roleService.findWorkflowOwnerRole.mockResolvedValueOnce(wfOwnerRole());
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const mockOwner = new User();
|
|
|
|
const mockNonOwner = new User();
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const sharedWorkflow = Object.assign(new SharedWorkflow(), {
|
|
|
|
role: new Role(),
|
|
|
|
user: mockOwner,
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
sharedWorkflowRepository.findOneOrFail.mockResolvedValueOnce(sharedWorkflow);
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const returnedOwner = await ownershipService.getWorkflowOwnerCached('some-workflow-id');
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
expect(returnedOwner).toBe(mockOwner);
|
|
|
|
expect(returnedOwner).not.toBe(mockNonOwner);
|
|
|
|
});
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
test('should throw if no workflow owner role found', async () => {
|
|
|
|
roleService.findWorkflowOwnerRole.mockRejectedValueOnce(new Error());
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
2023-09-04 01:37:16 -07:00
|
|
|
});
|
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
test('should throw if no workflow owner found', async () => {
|
|
|
|
roleService.findWorkflowOwnerRole.mockResolvedValueOnce(wfOwnerRole());
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
sharedWorkflowRepository.findOneOrFail.mockRejectedValue(new Error());
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
|
|
|
});
|
|
|
|
});
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
describe('addOwnedByAndSharedWith()', () => {
|
|
|
|
test('should add `ownedBy` and `sharedWith` to credential', async () => {
|
|
|
|
const owner = mockUser();
|
|
|
|
const editor = mockUser();
|
|
|
|
|
|
|
|
const credential = mockCredential();
|
|
|
|
|
|
|
|
credential.shared = [
|
|
|
|
{ role: mockCredRole('owner'), user: owner },
|
|
|
|
{ role: mockCredRole('editor'), user: editor },
|
|
|
|
] as SharedCredentials[];
|
|
|
|
|
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
|
|
|
|
|
|
|
expect(ownedBy).toStrictEqual({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(sharedWith).toStrictEqual([
|
|
|
|
{
|
|
|
|
id: editor.id,
|
|
|
|
email: editor.email,
|
|
|
|
firstName: editor.firstName,
|
|
|
|
lastName: editor.lastName,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
test('should add `ownedBy` and `sharedWith` to workflow', async () => {
|
|
|
|
const owner = mockUser();
|
|
|
|
const editor = mockUser();
|
|
|
|
|
|
|
|
const workflow = new WorkflowEntity();
|
|
|
|
|
|
|
|
workflow.shared = [
|
|
|
|
{ role: mockCredRole('owner'), user: owner },
|
|
|
|
{ role: mockCredRole('editor'), user: editor },
|
|
|
|
] as SharedWorkflow[];
|
|
|
|
|
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(workflow);
|
|
|
|
|
|
|
|
expect(ownedBy).toStrictEqual({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(sharedWith).toStrictEqual([
|
|
|
|
{
|
|
|
|
id: editor.id,
|
|
|
|
email: editor.email,
|
|
|
|
firstName: editor.firstName,
|
|
|
|
lastName: editor.lastName,
|
|
|
|
},
|
|
|
|
]);
|
2023-12-05 01:11:18 -08:00
|
|
|
});
|
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
test('should produce an empty sharedWith if no sharee', async () => {
|
|
|
|
const owner = mockUser();
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const credential = mockCredential();
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
credential.shared = [{ role: mockCredRole('owner'), user: owner }] as SharedCredentials[];
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
expect(ownedBy).toStrictEqual({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
|
|
|
});
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
expect(sharedWith).toHaveLength(0);
|
2023-09-04 01:37:16 -07:00
|
|
|
});
|
|
|
|
});
|
2023-12-11 04:35:09 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
describe('getInstanceOwner()', () => {
|
|
|
|
test('should find owner using global owner role ID', async () => {
|
|
|
|
const instanceOwnerRole = mockInstanceOwnerRole();
|
|
|
|
roleService.findGlobalOwnerRole.mockResolvedValue(instanceOwnerRole);
|
2023-12-11 04:35:09 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
await ownershipService.getInstanceOwner();
|
2023-12-11 04:35:09 -08:00
|
|
|
|
2023-12-13 03:41:06 -08:00
|
|
|
expect(userRepository.findOneOrFail).toHaveBeenCalledWith({
|
|
|
|
where: { globalRoleId: instanceOwnerRole.id },
|
|
|
|
relations: ['globalRole'],
|
|
|
|
});
|
2023-12-11 04:35:09 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
});
|