From d91b91b56f6b143bd82d3d67e91b6eef9cdbcbc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Fri, 19 Aug 2022 10:28:53 +0200 Subject: [PATCH] chore: type improvements (#1941) Simplify the type to its minimum so it matches the API spec --- src/lib/services/access-service.test.ts | 154 ++++++++++++++++++++++++ src/lib/services/access-service.ts | 8 +- 2 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/lib/services/access-service.test.ts diff --git a/src/lib/services/access-service.test.ts b/src/lib/services/access-service.test.ts new file mode 100644 index 0000000000..96539bcc30 --- /dev/null +++ b/src/lib/services/access-service.test.ts @@ -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 { + 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', + }, + ], + }); +}); diff --git a/src/lib/services/access-service.ts b/src/lib/services/access-service.ts index caf997f576..36266b44ce 100644 --- a/src/lib/services/access-service.ts +++ b/src/lib/services/access-service.ts @@ -48,6 +48,12 @@ interface IRoleCreation { permissions?: IPermission[]; } +export interface IRoleValidation { + name: string; + description?: string; + permissions?: Pick[]; +} + interface IRoleUpdate { id: number; name: string; @@ -525,7 +531,7 @@ export class AccessService { } async validateRole( - role: IRoleCreation, + role: IRoleValidation, existingId?: number, ): Promise { const cleanedRole = await roleSchema.validateAsync(role);