2023-04-12 01:59:14 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { DataSource, EntityManager } from 'typeorm';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-05-02 01:37:19 -07:00
|
|
|
import type { RoleNames, RoleScopes } from '@db/entities/Role';
|
|
|
|
import { Role } from '@db/entities/Role';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { RoleRepository } from '@db/repositories/role.repository';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { mockInstance } from '../../integration/shared/utils/';
|
2023-04-12 01:59:14 -07:00
|
|
|
import { randomInteger } from '../../integration/shared/random';
|
|
|
|
|
|
|
|
describe('RoleRepository', () => {
|
|
|
|
const entityManager = mockInstance(EntityManager);
|
|
|
|
const dataSource = mockInstance(DataSource, { manager: entityManager });
|
|
|
|
dataSource.getMetadata.mockReturnValue(mock());
|
|
|
|
Object.assign(entityManager, { connection: dataSource });
|
|
|
|
const roleRepository = Container.get(RoleRepository);
|
|
|
|
|
|
|
|
describe('findRole', () => {
|
|
|
|
test('should return the role when present', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(createRole('global', 'owner'));
|
|
|
|
const role = await roleRepository.findRole('global', 'owner');
|
|
|
|
expect(role?.name).toEqual('owner');
|
|
|
|
expect(role?.scope).toEqual('global');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return null otherwise', async () => {
|
|
|
|
entityManager.findOne.mockResolvedValueOnce(null);
|
|
|
|
const role = await roleRepository.findRole('global', 'owner');
|
|
|
|
expect(role).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('findRoleOrFail', () => {
|
|
|
|
test('should return the role when present', async () => {
|
|
|
|
entityManager.findOneOrFail.mockResolvedValueOnce(createRole('global', 'owner'));
|
|
|
|
const role = await roleRepository.findRoleOrFail('global', 'owner');
|
|
|
|
expect(role?.name).toEqual('owner');
|
|
|
|
expect(role?.scope).toEqual('global');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw otherwise', async () => {
|
|
|
|
entityManager.findOneOrFail.mockRejectedValueOnce(new Error());
|
2023-05-23 17:01:45 -07:00
|
|
|
await expect(async () => roleRepository.findRoleOrFail('global', 'owner')).rejects.toThrow();
|
2023-04-12 01:59:14 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const createRole = (scope: RoleScopes, name: RoleNames) =>
|
|
|
|
Object.assign(new Role(), { name, scope, id: `${randomInteger()}` });
|
|
|
|
});
|