mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
chore: type improvements (#1941)
Simplify the type to its minimum so it matches the API spec
This commit is contained in:
parent
e927012f9f
commit
d91b91b56f
154
src/lib/services/access-service.test.ts
Normal file
154
src/lib/services/access-service.test.ts
Normal file
@ -0,0 +1,154 @@
|
||||
import NameExistsError from '../error/name-exists-error';
|
||||
import getLogger from '../../test/fixtures/no-logger';
|
||||
import createStores from '../../test/fixtures/store';
|
||||
import { AccessService, IRoleValidation } from './access-service';
|
||||
|
||||
function getSetup(withNameInUse: boolean) {
|
||||
const stores = createStores();
|
||||
|
||||
stores.roleStore = {
|
||||
...stores.roleStore,
|
||||
async nameInUse(): Promise<boolean> {
|
||||
return withNameInUse;
|
||||
},
|
||||
};
|
||||
return {
|
||||
accessService: new AccessService(
|
||||
stores,
|
||||
{
|
||||
getLogger,
|
||||
},
|
||||
undefined, // GroupService
|
||||
),
|
||||
stores,
|
||||
};
|
||||
}
|
||||
test('should fail when name exists', async () => {
|
||||
const { accessService } = getSetup(true);
|
||||
|
||||
const existingRole: IRoleValidation = {
|
||||
name: 'existing role',
|
||||
description: 'description',
|
||||
};
|
||||
expect(accessService.validateRole(existingRole)).rejects.toThrow(
|
||||
new NameExistsError(
|
||||
`There already exists a role with the name ${existingRole.name}`,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('should validate a role without permissions', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
|
||||
const withoutPermissions: IRoleValidation = {
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
};
|
||||
expect(await accessService.validateRole(withoutPermissions)).toEqual(
|
||||
withoutPermissions,
|
||||
);
|
||||
});
|
||||
|
||||
test('should complete description field when not present', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
const withoutDescription: IRoleValidation = {
|
||||
name: 'name of the role',
|
||||
};
|
||||
expect(await accessService.validateRole(withoutDescription)).toEqual({
|
||||
name: 'name of the role',
|
||||
description: '',
|
||||
});
|
||||
});
|
||||
|
||||
test('should accept empty permissions', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
const withEmptyPermissions: IRoleValidation = {
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [],
|
||||
};
|
||||
expect(await accessService.validateRole(withEmptyPermissions)).toEqual({
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('should complete environment field of permissions when not present', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
const withoutEnvironmentInPermissions: IRoleValidation = {
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(
|
||||
await accessService.validateRole(withoutEnvironmentInPermissions),
|
||||
).toEqual({
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
environment: '',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('should return the same object when all fields are valid and present', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
|
||||
const roleWithAllFields: IRoleValidation = {
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
environment: 'development',
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(await accessService.validateRole(roleWithAllFields)).toEqual({
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
environment: 'development',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('should be able to validate and cleanup with additional properties', async () => {
|
||||
const { accessService } = getSetup(false);
|
||||
const base = {
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
additional: 'property',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
environment: 'development',
|
||||
name: 'name',
|
||||
displayName: 'displayName',
|
||||
type: 'type',
|
||||
additional: 'property',
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(await accessService.validateRole(base)).toEqual({
|
||||
name: 'name of the role',
|
||||
description: 'description',
|
||||
permissions: [
|
||||
{
|
||||
id: 1,
|
||||
environment: 'development',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
@ -48,6 +48,12 @@ interface IRoleCreation {
|
||||
permissions?: IPermission[];
|
||||
}
|
||||
|
||||
export interface IRoleValidation {
|
||||
name: string;
|
||||
description?: string;
|
||||
permissions?: Pick<IPermission, 'id' | 'environment'>[];
|
||||
}
|
||||
|
||||
interface IRoleUpdate {
|
||||
id: number;
|
||||
name: string;
|
||||
@ -525,7 +531,7 @@ export class AccessService {
|
||||
}
|
||||
|
||||
async validateRole(
|
||||
role: IRoleCreation,
|
||||
role: IRoleValidation,
|
||||
existingId?: number,
|
||||
): Promise<IRoleCreation> {
|
||||
const cleanedRole = await roleSchema.validateAsync(role);
|
||||
|
Loading…
Reference in New Issue
Block a user