Path: blob/main/build/lib/test/stringEnumPolicy.test.ts
4772 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { StringEnumPolicy } from '../policies/stringEnumPolicy.ts';7import { PolicyType, type LanguageTranslations } from '../policies/types.ts';8import type { CategoryDto, PolicyDto } from '../policies/policyDto.ts';910suite('StringEnumPolicy', () => {11const mockCategory: CategoryDto = {12key: 'test.category',13name: { value: 'Category1', key: 'test.category' },14};1516const mockPolicy: PolicyDto = {17key: 'test.stringenum.policy',18name: 'TestStringEnumPolicy',19category: 'Category1',20minimumVersion: '1.0',21type: 'string',22localization: {23description: { key: 'test.policy.description', value: 'Test policy description' },24enumDescriptions: [25{ key: 'test.option.one', value: 'Option One' },26{ key: 'test.option.two', value: 'Option Two' },27{ key: 'test.option.three', value: 'Option Three' }28]29},30enum: ['auto', 'manual', 'disabled']31};3233test('should create StringEnumPolicy from factory method', () => {34const policy = StringEnumPolicy.from(mockCategory, mockPolicy);3536assert.ok(policy);37assert.strictEqual(policy.name, 'TestStringEnumPolicy');38assert.strictEqual(policy.minimumVersion, '1.0');39assert.strictEqual(policy.category.name.nlsKey, mockCategory.name.key);40assert.strictEqual(policy.category.name.value, mockCategory.name.value);41assert.strictEqual(policy.type, PolicyType.StringEnum);42});4344test('should render ADMX elements correctly', () => {45const policy = StringEnumPolicy.from(mockCategory, mockPolicy);4647assert.ok(policy);4849const admx = policy.renderADMX('TestKey');5051assert.deepStrictEqual(admx, [52'<policy name="TestStringEnumPolicy" class="Both" displayName="$(string.TestStringEnumPolicy)" explainText="$(string.TestStringEnumPolicy_test_policy_description)" key="Software\\Policies\\Microsoft\\TestKey" presentation="$(presentation.TestStringEnumPolicy)">',53'\t<parentCategory ref="test.category" />',54'\t<supportedOn ref="Supported_1_0" />',55'\t<elements>',56'<enum id="TestStringEnumPolicy" valueName="TestStringEnumPolicy">',57'\t<item displayName="$(string.TestStringEnumPolicy_test_option_one)"><value><string>auto</string></value></item>',58'\t<item displayName="$(string.TestStringEnumPolicy_test_option_two)"><value><string>manual</string></value></item>',59'\t<item displayName="$(string.TestStringEnumPolicy_test_option_three)"><value><string>disabled</string></value></item>',60'</enum>',61'\t</elements>',62'</policy>'63]);64});6566test('should render ADML strings correctly', () => {67const policy = StringEnumPolicy.from(mockCategory, mockPolicy);6869assert.ok(policy);7071const admlStrings = policy.renderADMLStrings();7273assert.deepStrictEqual(admlStrings, [74'<string id="TestStringEnumPolicy">TestStringEnumPolicy</string>',75'<string id="TestStringEnumPolicy_test_policy_description">Test policy description</string>',76'<string id="TestStringEnumPolicy_test_option_one">Option One</string>',77'<string id="TestStringEnumPolicy_test_option_two">Option Two</string>',78'<string id="TestStringEnumPolicy_test_option_three">Option Three</string>'79]);80});8182test('should render ADML strings with translations', () => {83const policy = StringEnumPolicy.from(mockCategory, mockPolicy);8485assert.ok(policy);8687const translations: LanguageTranslations = {88'': {89'test.policy.description': 'Translated description',90'test.option.one': 'Translated Option One',91'test.option.two': 'Translated Option Two'92}93};9495const admlStrings = policy.renderADMLStrings(translations);9697assert.deepStrictEqual(admlStrings, [98'<string id="TestStringEnumPolicy">TestStringEnumPolicy</string>',99'<string id="TestStringEnumPolicy_test_policy_description">Translated description</string>',100'<string id="TestStringEnumPolicy_test_option_one">Translated Option One</string>',101'<string id="TestStringEnumPolicy_test_option_two">Translated Option Two</string>',102'<string id="TestStringEnumPolicy_test_option_three">Option Three</string>'103]);104});105106test('should render ADML presentation correctly', () => {107const policy = StringEnumPolicy.from(mockCategory, mockPolicy);108109assert.ok(policy);110111const presentation = policy.renderADMLPresentation();112113assert.strictEqual(presentation, '<presentation id="TestStringEnumPolicy"><dropdownList refId="TestStringEnumPolicy" /></presentation>');114});115116test('should render JSON value correctly', () => {117const policy = StringEnumPolicy.from(mockCategory, mockPolicy);118119assert.ok(policy);120121const jsonValue = policy.renderJsonValue();122123assert.strictEqual(jsonValue, 'auto');124});125126test('should render profile value correctly', () => {127const policy = StringEnumPolicy.from(mockCategory, mockPolicy);128129assert.ok(policy);130131const profileValue = policy.renderProfileValue();132133assert.strictEqual(profileValue, '<string>auto</string>');134});135136test('should render profile correctly', () => {137const policy = StringEnumPolicy.from(mockCategory, mockPolicy);138139assert.ok(policy);140141const profile = policy.renderProfile();142143assert.strictEqual(profile.length, 2);144assert.strictEqual(profile[0], '<key>TestStringEnumPolicy</key>');145assert.strictEqual(profile[1], '<string>auto</string>');146});147148test('should render profile manifest value correctly', () => {149const policy = StringEnumPolicy.from(mockCategory, mockPolicy);150151assert.ok(policy);152153const manifestValue = policy.renderProfileManifestValue();154155assert.strictEqual(manifestValue, '<key>pfm_default</key>\n<string>auto</string>\n<key>pfm_description</key>\n<string>Test policy description</string>\n<key>pfm_name</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_title</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_type</key>\n<string>string</string>\n<key>pfm_range_list</key>\n<array>\n\t<string>auto</string>\n\t<string>manual</string>\n\t<string>disabled</string>\n</array>');156});157158test('should render profile manifest value with translations', () => {159const policy = StringEnumPolicy.from(mockCategory, mockPolicy);160161assert.ok(policy);162163const translations: LanguageTranslations = {164'': {165'test.policy.description': 'Translated manifest description'166}167};168169const manifestValue = policy.renderProfileManifestValue(translations);170171assert.strictEqual(manifestValue, '<key>pfm_default</key>\n<string>auto</string>\n<key>pfm_description</key>\n<string>Translated manifest description</string>\n<key>pfm_name</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_title</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_type</key>\n<string>string</string>\n<key>pfm_range_list</key>\n<array>\n\t<string>auto</string>\n\t<string>manual</string>\n\t<string>disabled</string>\n</array>');172});173174test('should render profile manifest correctly', () => {175const policy = StringEnumPolicy.from(mockCategory, mockPolicy);176177assert.ok(policy);178179const manifest = policy.renderProfileManifest();180181assert.strictEqual(manifest, '<dict>\n<key>pfm_default</key>\n<string>auto</string>\n<key>pfm_description</key>\n<string>Test policy description</string>\n<key>pfm_name</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_title</key>\n<string>TestStringEnumPolicy</string>\n<key>pfm_type</key>\n<string>string</string>\n<key>pfm_range_list</key>\n<array>\n\t<string>auto</string>\n\t<string>manual</string>\n\t<string>disabled</string>\n</array>\n</dict>');182});183});184185186