Skip to content

Migrates 'entra administrativeunit list' to Zod. Closes #6810 #6811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import assert from 'assert';
import sinon from 'sinon';
import { z } from 'zod';
import auth from '../../../../Auth.js';
import { CommandInfo } from '../../../../cli/CommandInfo.js';
import { Logger } from '../../../../cli/Logger.js';
import { CommandError } from '../../../../Command.js';
import request from '../../../../request.js';
import { telemetry } from '../../../../telemetry.js';
import { pid } from '../../../../utils/pid.js';
import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import { cli } from '../../../../cli/cli.js';
import commands from '../../commands.js';
import command from './administrativeunit-list.js';

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

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

beforeEach(() => {
Expand Down Expand Up @@ -85,9 +92,7 @@ describe(commands.ADMINISTRATIVEUNIT_LIST, () => {
throw 'Invalid request';
});

await command.action(logger, {
options: {}
});
await command.action(logger, { options: commandOptionsSchema.parse({}) });

assert(
loggerLogSpy.calledWith([
Expand Down Expand Up @@ -125,9 +130,7 @@ describe(commands.ADMINISTRATIVEUNIT_LIST, () => {
throw 'Invalid request';
});

await command.action(logger, {
options: { properties: 'id,displayName' }
});
await command.action(logger, { options: commandOptionsSchema.parse({ properties: 'id,displayName' }) });

assert(
loggerLogSpy.calledWith([
Expand All @@ -152,7 +155,7 @@ describe(commands.ADMINISTRATIVEUNIT_LIST, () => {
});

await assert.rejects(
command.action(logger, { options: {} } as any),
command.action(logger, { options: commandOptionsSchema.parse({}) }),
new CommandError('An error has occurred')
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { z } from 'zod';
import { AdministrativeUnit } from '@microsoft/microsoft-graph-types';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import { odata } from '../../../../utils/odata.js';
import { zod } from '../../../../utils/zod.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';

import GlobalOptions from '../../../../GlobalOptions.js';
const options = globalOptionsZod
.extend({
properties: zod.alias('p', z.string().optional())
}).strict();
declare type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

export interface Options extends GlobalOptions {
properties?: string;
}

class EntraAdministrativeUnitListCommand extends GraphCommand {
public get name(): string {
return commands.ADMINISTRATIVEUNIT_LIST;
Expand All @@ -23,29 +26,12 @@ class EntraAdministrativeUnitListCommand extends GraphCommand {
return 'Retrieves a list of administrative units';
}

public defaultProperties(): string[] | undefined {
return ['id', 'displayName', 'visibility'];
public get schema(): z.ZodTypeAny | undefined {
return options;
}

constructor() {
super();

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

#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
properties: typeof args.options.properties !== 'undefined'
});
});
}

#initOptions(): void {
this.options.unshift(
{ option: '-p, --properties [properties]' }
);
public defaultProperties(): string[] | undefined {
return ['id', 'displayName', 'visibility'];
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
Expand Down
Loading