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 { CacheService } from '@/services/cache.service';
|
|
|
|
import { User } from '@db/entities/User';
|
|
|
|
import { RoleService } from '@/services/role.service';
|
|
|
|
import { UserService } from '@/services/user.service';
|
|
|
|
import { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
|
|
|
import type { SharedCredentials } from '@db/entities/SharedCredentials';
|
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2023-09-04 01:37:16 -07:00
|
|
|
import {
|
|
|
|
randomCredentialPayload,
|
|
|
|
randomEmail,
|
|
|
|
randomInteger,
|
|
|
|
randomName,
|
|
|
|
} from '../../integration/shared/random';
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
const wfOwnerRole = () =>
|
|
|
|
Object.assign(new Role(), {
|
|
|
|
scope: 'workflow',
|
|
|
|
name: 'owner',
|
|
|
|
id: randomInteger(),
|
|
|
|
});
|
|
|
|
|
2023-09-04 01:37:16 -07:00
|
|
|
const mockCredRole = (name: 'owner' | 'editor'): Role =>
|
|
|
|
Object.assign(new Role(), {
|
|
|
|
scope: 'credentials',
|
|
|
|
name,
|
|
|
|
id: randomInteger(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockCredential = (): CredentialsEntity =>
|
|
|
|
Object.assign(new CredentialsEntity(), randomCredentialPayload());
|
|
|
|
|
|
|
|
const mockUser = (): User =>
|
|
|
|
Object.assign(new User(), {
|
|
|
|
id: randomInteger(),
|
|
|
|
email: randomEmail(),
|
|
|
|
firstName: randomName(),
|
|
|
|
lastName: randomName(),
|
|
|
|
});
|
|
|
|
|
2023-07-31 02:37:09 -07:00
|
|
|
describe('OwnershipService', () => {
|
|
|
|
const cacheService = mockInstance(CacheService);
|
2023-08-02 23:58:36 -07:00
|
|
|
const roleService = mockInstance(RoleService);
|
2023-08-22 06:58:05 -07:00
|
|
|
const userService = mockInstance(UserService);
|
2023-07-31 02:37:09 -07:00
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
|
|
|
|
|
|
|
const ownershipService = new OwnershipService(
|
|
|
|
cacheService,
|
2023-08-22 06:58:05 -07:00
|
|
|
userService,
|
2023-08-02 23:58:36 -07:00
|
|
|
roleService,
|
2023-07-31 02:37:09 -07:00
|
|
|
sharedWorkflowRepository,
|
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getWorkflowOwner()', () => {
|
|
|
|
test('should retrieve a workflow owner', async () => {
|
2023-08-02 23:58:36 -07:00
|
|
|
roleService.findWorkflowOwnerRole.mockResolvedValueOnce(wfOwnerRole());
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
const mockOwner = new User();
|
|
|
|
const mockNonOwner = new User();
|
|
|
|
|
|
|
|
const sharedWorkflow = Object.assign(new SharedWorkflow(), {
|
|
|
|
role: new Role(),
|
|
|
|
user: mockOwner,
|
|
|
|
});
|
|
|
|
|
|
|
|
sharedWorkflowRepository.findOneOrFail.mockResolvedValueOnce(sharedWorkflow);
|
|
|
|
|
|
|
|
const returnedOwner = await ownershipService.getWorkflowOwnerCached('some-workflow-id');
|
|
|
|
|
|
|
|
expect(returnedOwner).toBe(mockOwner);
|
|
|
|
expect(returnedOwner).not.toBe(mockNonOwner);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw if no workflow owner role found', async () => {
|
2023-08-02 23:58:36 -07:00
|
|
|
roleService.findWorkflowOwnerRole.mockRejectedValueOnce(new Error());
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw if no workflow owner found', async () => {
|
2023-08-02 23:58:36 -07:00
|
|
|
roleService.findWorkflowOwnerRole.mockResolvedValueOnce(wfOwnerRole());
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
sharedWorkflowRepository.findOneOrFail.mockRejectedValue(new Error());
|
|
|
|
|
|
|
|
await expect(ownershipService.getWorkflowOwnerCached('some-workflow-id')).rejects.toThrow();
|
|
|
|
});
|
|
|
|
});
|
2023-09-04 01:37:16 -07: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,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should produce an empty sharedWith if no sharee', async () => {
|
|
|
|
const owner = mockUser();
|
|
|
|
|
|
|
|
const credential = mockCredential();
|
|
|
|
|
|
|
|
credential.shared = [{ role: mockCredRole('owner'), user: owner }] 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).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
2023-07-31 02:37:09 -07:00
|
|
|
});
|