UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature toggles service. It provides different strategies for handling feature toggles.

137 lines 4.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const name_exists_error_1 = __importDefault(require("../error/name-exists-error")); const no_logger_1 = __importDefault(require("../../test/fixtures/no-logger")); const store_1 = __importDefault(require("../../test/fixtures/store")); const access_service_1 = require("./access-service"); function getSetup(withNameInUse) { const stores = (0, store_1.default)(); stores.roleStore = { ...stores.roleStore, async nameInUse() { return withNameInUse; }, }; return { accessService: new access_service_1.AccessService(stores, { getLogger: no_logger_1.default, }, undefined), stores, }; } test('should fail when name exists', async () => { const { accessService } = getSetup(true); const existingRole = { name: 'existing role', description: 'description', }; expect(accessService.validateRole(existingRole)).rejects.toThrow(new name_exists_error_1.default(`There already exists a role with the name ${existingRole.name}`)); }); test('should validate a role without permissions', async () => { const { accessService } = getSetup(false); const withoutPermissions = { 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 = { 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 = { 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 = { 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 = { 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', }, ], }); }); //# sourceMappingURL=access-service.test.js.map