Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/m365/entra/commands/groupsetting/groupsetting-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './groupsetting-add.js';
import { options } from './groupsetting-get.js';

describe(commands.GROUPSETTING_ADD, () => {
let log: string[];
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -26,6 +28,7 @@ describe(commands.GROUPSETTING_ADD, () => {
sinon.stub(session, 'getId').returns('');
auth.connection.active = true;
commandInfo = cli.getCommandInfo(command);
commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options;
});

beforeEach(() => {
Expand Down Expand Up @@ -481,14 +484,14 @@ describe(commands.GROUPSETTING_ADD, () => {
new CommandError(`A conflicting object with one or more of the specified property values is present in the directory.`));
});

it('fails validation if the templateId is not a valid GUID', async () => {
const actual = await command.validate({ options: { templateId: 'invalid' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if the templateId is not a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ templateId: 'invalid' });
assert.strictEqual(actual.success, false);
});

it('passes validation if the templateId is a valid GUID', async () => {
const actual = await command.validate({ options: { templateId: '68be84bf-a585-4776-80b3-30aa5207aa22' } }, commandInfo);
assert.strictEqual(actual, true);
it('passes validation if the templateId is a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ templateId: '68be84bf-a585-4776-80b3-30aa5207aa22' });
assert.strictEqual(actual.success, true);
});

it('allows unknown properties', () => {
Expand Down
46 changes: 13 additions & 33 deletions src/m365/entra/commands/groupsetting/groupsetting-add.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { GroupSettingTemplate } from '@microsoft/microsoft-graph-types';
import GlobalOptions from '../../../../GlobalOptions.js';
import { z } from 'zod';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';

const options = z.looseObject({
...globalOptionsZod.shape,
templateId: z.uuid().alias('i')
});

declare type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
templateId: string;
}

class EntraGroupSettingAddCommand extends GraphCommand {
public get name(): string {
return commands.GROUPSETTING_ADD;
Expand All @@ -23,37 +26,14 @@ class EntraGroupSettingAddCommand extends GraphCommand {
return 'Creates a group setting';
}

constructor() {
super();

this.#initOptions();
this.#initValidators();
}

#initOptions(): void {
this.options.unshift(
{
option: '-i, --templateId <templateId>'
}
);
}

#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => {
if (!validation.isValidGuid(args.options.templateId)) {
return `${args.options.templateId} is not a valid GUID`;
}

return true;
}
);
}

public allowUnknownOptions(): boolean | undefined {
return true;
}

public get schema(): z.ZodType | undefined {
return options;
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
if (this.verbose) {
await logger.logToStderr(`Retrieving group setting template with id '${args.options.templateId}'...`);
Expand Down
27 changes: 9 additions & 18 deletions src/m365/entra/commands/groupsetting/groupsetting-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './groupsetting-get.js';
import command, { options } from './groupsetting-get.js';

describe(commands.GROUPSETTING_GET, () => {
let log: string[];
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
Expand All @@ -26,6 +27,7 @@ describe(commands.GROUPSETTING_GET, () => {
sinon.stub(session, 'getId').returns('');
auth.connection.active = true;
commandInfo = cli.getCommandInfo(command);
commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options;
});

beforeEach(() => {
Expand Down Expand Up @@ -153,24 +155,13 @@ describe(commands.GROUPSETTING_GET, () => {
new CommandError(`Resource '1caf7dcd-7e83-4c3a-94f7-932a1299c843' does not exist or one of its queried reference-property objects are not present.`));
});

it('fails validation if the id is not a valid GUID', async () => {
const actual = await command.validate({ options: { id: '123' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if the id is not a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ id: '123' });
assert.strictEqual(actual.success, false);
});

it('passes validation if the id is a valid GUID', async () => {
const actual = await command.validate({ options: { id: '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } }, commandInfo);
assert.strictEqual(actual, true);
});

it('supports specifying id', () => {
const options = command.options;
let containsOption = false;
options.forEach(o => {
if (o.option.indexOf('--id') > -1) {
containsOption = true;
}
});
assert(containsOption);
it('passes validation if the id is a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ id: '1caf7dcd-7e83-4c3a-94f7-932a1299c844' });
assert.strictEqual(actual.success, true);
});
});
42 changes: 11 additions & 31 deletions src/m365/entra/commands/groupsetting/groupsetting-get.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { z } from 'zod';
import { Logger } from '../../../../cli/Logger.js';
import GlobalOptions from '../../../../GlobalOptions.js';
import { globalOptionsZod } from '../../../../Command.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({
...globalOptionsZod.shape,
id: z.uuid().alias('i')
});

declare type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
id: string;
}

class EntraGroupSettingGetCommand extends GraphCommand {
public get name(): string {
return commands.GROUPSETTING_GET;
Expand All @@ -22,31 +25,8 @@ class EntraGroupSettingGetCommand extends GraphCommand {
return 'Gets information about the particular group setting';
}

constructor() {
super();

this.#initOptions();
this.#initValidators();
}

#initOptions(): void {
this.options.unshift(
{
option: '-i, --id <id>'
}
);
}

#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => {
if (!validation.isValidGuid(args.options.id)) {
return `${args.options.id} is not a valid GUID`;
}

return true;
}
);
public get schema(): z.ZodType | undefined {
return options;
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions src/m365/entra/commands/groupsetting/groupsetting-list.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { GroupSetting } from '@microsoft/microsoft-graph-types';
import { z } from 'zod';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import { odata } from '../../../../utils/odata.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';

export const options = z.strictObject({ ...globalOptionsZod.shape });

class EntraGroupSettingListCommand extends GraphCommand {
public get name(): string {
return commands.GROUPSETTING_LIST;
Expand All @@ -17,6 +21,10 @@ class EntraGroupSettingListCommand extends GraphCommand {
return ['id', 'displayName'];
}

public get schema(): z.ZodType | undefined {
return options;
}

public async commandAction(logger: Logger): Promise<void> {
try {
const groupSettings = await odata.getAllItems<GroupSetting>(`${this.resource}/v1.0/groupSettings`);
Expand Down
40 changes: 9 additions & 31 deletions src/m365/entra/commands/groupsetting/groupsetting-remove.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'assert';
import fs from 'fs';
import sinon from 'sinon';
import auth from '../../../../Auth.js';
import { cli } from '../../../../cli/cli.js';
Expand All @@ -12,22 +11,23 @@ import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './groupsetting-remove.js';
import command, { options } from './groupsetting-remove.js';

describe(commands.GROUPSETTING_REMOVE, () => {
let log: string[];
let logger: Logger;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;
let promptIssued: boolean = false;

before(() => {
sinon.stub(auth, 'restoreAuth').resolves();
sinon.stub(telemetry, 'trackEvent').resolves();
sinon.stub(pid, 'getProcessName').returns('');
sinon.stub(session, 'getId').returns('');
sinon.stub(fs, 'readFileSync').returns('abc');
auth.connection.active = true;
commandInfo = cli.getCommandInfo(command);
commandOptionsSchema = commandInfo.command.getSchemaToParse() as typeof options;
});

beforeEach(() => {
Expand Down Expand Up @@ -153,35 +153,13 @@ describe(commands.GROUPSETTING_REMOVE, () => {
new CommandError('File Not Found.'));
});

it('supports specifying id', () => {
const options = command.options;
let containsOption = false;
options.forEach(o => {
if (o.option.indexOf('--id') > -1) {
containsOption = true;
}
});
assert(containsOption);
});

it('supports specifying confirmation flag', () => {
const options = command.options;
let containsOption = false;
options.forEach(o => {
if (o.option.indexOf('--force') > -1) {
containsOption = true;
}
});
assert(containsOption);
});

it('fails validation if the id is not a valid GUID', async () => {
const actual = await command.validate({ options: { id: 'abc' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if the id is not a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ id: 'abc' });
assert.strictEqual(actual.success, false);
});

it('passes validation when the id is a valid GUID', async () => {
const actual = await command.validate({ options: { id: '2c1ba4c4-cd9b-4417-832f-92a34bc34b2a' } }, commandInfo);
assert.strictEqual(actual, true);
it('passes validation when the id is a valid GUID', () => {
const actual = commandOptionsSchema.safeParse({ id: '2c1ba4c4-cd9b-4417-832f-92a34bc34b2a' });
assert.strictEqual(actual.success, true);
});
});
Loading
Loading