2023-07-31 02:37:09 -07:00
|
|
|
import { OwnershipService } from '@/services/ownership.service';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { SharedWorkflowRepository, UserRepository } from '@/databases/repositories';
|
2023-07-31 02:37:09 -07:00
|
|
|
import { mockInstance } from '../../integration/shared/utils';
|
|
|
|
import { Role } from '@/databases/entities/Role';
|
|
|
|
import { randomInteger } from '../../integration/shared/random';
|
|
|
|
import { SharedWorkflow } from '@/databases/entities/SharedWorkflow';
|
|
|
|
import { CacheService } from '@/services/cache.service';
|
|
|
|
import { User } from '@/databases/entities/User';
|
2023-08-02 23:58:36 -07:00
|
|
|
import { RoleService } from '@/services/role.service';
|
2023-07-31 02:37:09 -07:00
|
|
|
|
|
|
|
const wfOwnerRole = () =>
|
|
|
|
Object.assign(new Role(), {
|
|
|
|
scope: 'workflow',
|
|
|
|
name: 'owner',
|
|
|
|
id: randomInteger(),
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('OwnershipService', () => {
|
|
|
|
const cacheService = mockInstance(CacheService);
|
2023-08-02 23:58:36 -07:00
|
|
|
const roleService = mockInstance(RoleService);
|
2023-07-31 02:37:09 -07:00
|
|
|
const userRepository = mockInstance(UserRepository);
|
|
|
|
const sharedWorkflowRepository = mockInstance(SharedWorkflowRepository);
|
|
|
|
|
|
|
|
const ownershipService = new OwnershipService(
|
|
|
|
cacheService,
|
|
|
|
userRepository,
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|