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 { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import { User } from '@db/entities/User';
|
|
|
|
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';
|
2024-01-24 04:38:57 -08:00
|
|
|
import { mockCredential, mockUser } from '../shared/mockObjects';
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2023-07-31 02:37:09 -07:00
|
|
|
describe('OwnershipService', () => {
|
2023-12-11 04:35:09 -08:00
|
|
|
const userRepository = mockInstance(UserRepository);
|
2023-07-31 02:37:09 -07:00
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
2024-01-24 04:38:57 -08:00
|
|
|
const ownershipService = new OwnershipService(mock(), userRepository, sharedWorkflowRepository);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
describe('getWorkflowOwner()', () => {
|
|
|
|
test('should retrieve a workflow owner', async () => {
|
|
|
|
const mockOwner = new User();
|
|
|
|
const mockNonOwner = new User();
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const sharedWorkflow = Object.assign(new SharedWorkflow(), {
|
|
|
|
role: 'workflow:owner',
|
|
|
|
user: mockOwner,
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
sharedWorkflowRepository.findOneOrFail.mockResolvedValueOnce(sharedWorkflow);
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const returnedOwner = await ownershipService.getWorkflowOwnerCached('some-workflow-id');
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(returnedOwner).toBe(mockOwner);
|
|
|
|
expect(returnedOwner).not.toBe(mockNonOwner);
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
test('should throw if no workflow owner found', async () => {
|
|
|
|
sharedWorkflowRepository.findOneOrFail.mockRejectedValue(new Error());
|
2023-07-31 02:37:09 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
|
|
|
});
|
|
|
|
});
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
describe('addOwnedByAndSharedWith()', () => {
|
|
|
|
test('should add `ownedBy` and `sharedWith` to credential', async () => {
|
|
|
|
const owner = mockUser();
|
|
|
|
const editor = mockUser();
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const credential = mockCredential();
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
credential.shared = [
|
|
|
|
{ role: 'credential:owner', user: owner },
|
|
|
|
{ role: 'credential:editor', user: editor },
|
|
|
|
] as SharedCredentials[];
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
|
|
|
|
|
|
|
expect(ownedBy).toStrictEqual({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
2023-09-04 01:37:16 -07:00
|
|
|
});
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(sharedWith).toStrictEqual([
|
|
|
|
{
|
|
|
|
id: editor.id,
|
|
|
|
email: editor.email,
|
|
|
|
firstName: editor.firstName,
|
|
|
|
lastName: editor.lastName,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
test('should add `ownedBy` and `sharedWith` to workflow', async () => {
|
|
|
|
const owner = mockUser();
|
|
|
|
const editor = mockUser();
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const workflow = new WorkflowEntity();
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
workflow.shared = [
|
|
|
|
{ role: 'workflow:owner', user: owner },
|
|
|
|
{ role: 'workflow:editor', user: editor },
|
|
|
|
] as SharedWorkflow[];
|
|
|
|
|
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(workflow);
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(ownedBy).toStrictEqual({
|
|
|
|
id: owner.id,
|
|
|
|
email: owner.email,
|
|
|
|
firstName: owner.firstName,
|
|
|
|
lastName: owner.lastName,
|
2023-12-05 01:11:18 -08:00
|
|
|
});
|
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(sharedWith).toStrictEqual([
|
|
|
|
{
|
|
|
|
id: editor.id,
|
|
|
|
email: editor.email,
|
|
|
|
firstName: editor.firstName,
|
|
|
|
lastName: editor.lastName,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2023-12-05 01:11:18 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
test('should produce an empty sharedWith if no sharee', async () => {
|
|
|
|
const owner = mockUser();
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const credential = mockCredential();
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
credential.shared = [{ role: 'credential:owner', user: owner }] as SharedCredentials[];
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
const { ownedBy, sharedWith } = ownershipService.addOwnedByAndSharedWith(credential);
|
2023-09-04 01:37:16 -07:00
|
|
|
|
2024-01-24 04:38:57 -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-11 04:35:09 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(sharedWith).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
2023-12-11 04:35:09 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
describe('getInstanceOwner()', () => {
|
|
|
|
test('should find owner using global owner role ID', async () => {
|
|
|
|
await ownershipService.getInstanceOwner();
|
2023-12-11 04:35:09 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
expect(userRepository.findOneOrFail).toHaveBeenCalledWith({
|
|
|
|
where: { role: 'global:owner' },
|
2023-12-11 04:35:09 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
});
|