diff --git a/src/m365/entra/commands/license/license-list.spec.ts b/src/m365/entra/commands/license/license-list.spec.ts index a2ae895e100..bc60a8ce56d 100644 --- a/src/m365/entra/commands/license/license-list.spec.ts +++ b/src/m365/entra/commands/license/license-list.spec.ts @@ -1,8 +1,11 @@ import assert from 'assert'; import sinon from 'sinon'; +import { z } from 'zod'; import auth from '../../../../Auth.js'; -import { CommandError } from '../../../../Command.js'; +import { cli } from '../../../../cli/cli.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'; @@ -65,6 +68,9 @@ describe(commands.LICENSE_LIST, () => { let log: string[]; let logger: Logger; let loggerLogSpy: sinon.SinonSpy; + let loggerStderrSpy: sinon.SinonSpy; + let commandInfo: CommandInfo; + let commandOptionsSchema: z.ZodTypeAny; before(() => { sinon.stub(auth, 'restoreAuth').resolves(); @@ -72,6 +78,8 @@ describe(commands.LICENSE_LIST, () => { sinon.stub(pid, 'getProcessName').returns(''); sinon.stub(session, 'getId').returns(''); auth.connection.active = true; + commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse()!; }); beforeEach(() => { @@ -88,6 +96,7 @@ describe(commands.LICENSE_LIST, () => { } }; loggerLogSpy = sinon.spy(logger, 'log'); + loggerStderrSpy = sinon.spy(logger, 'logToStderr'); }); afterEach(() => { @@ -109,6 +118,11 @@ describe(commands.LICENSE_LIST, () => { assert.notStrictEqual(command.description, null); }); + it('passes validation with no options', () => { + const actual = commandOptionsSchema.safeParse({}); + assert.strictEqual(actual.success, true); + }); + it('defines correct properties for the default output', () => { assert.deepStrictEqual(command.defaultProperties(), ['id', 'skuId', 'skuPartNumber']); }); @@ -118,13 +132,19 @@ describe(commands.LICENSE_LIST, () => { if ((opts.url === `https://graph.microsoft.com/v1.0/subscribedSkus`)) { return licenseResponse; } - - throw 'Invalid request'; + throw new Error('Invalid request'); }); - await command.action(logger, { options: { debug: true } }); + await command.action(logger, { options: commandOptionsSchema.parse({ debug: true }) }); assert(loggerLogSpy.calledWith(licenseResponse.value)); + }); + + it('logs retrieving message in verbose mode', async () => { + sinon.stub(request, 'get').resolves({ value: [] }); + + await command.action(logger, { options: commandOptionsSchema.parse({ verbose: true }) }); + assert(loggerStderrSpy.calledWith('Retrieving the commercial subscriptions that an organization has acquired')); }); it('correctly handles random API error', async () => { @@ -136,7 +156,7 @@ describe(commands.LICENSE_LIST, () => { sinon.stub(request, 'get').rejects(error); await assert.rejects(command.action(logger, { - options: {} + options: commandOptionsSchema.parse({}) }), new CommandError(error.error.message)); }); }); diff --git a/src/m365/entra/commands/license/license-list.ts b/src/m365/entra/commands/license/license-list.ts index b3bdcc9dfba..a76f46c8dc4 100644 --- a/src/m365/entra/commands/license/license-list.ts +++ b/src/m365/entra/commands/license/license-list.ts @@ -1,7 +1,11 @@ +import { z } from 'zod'; import { Logger } from '../../../../cli/Logger.js'; import { odata } from '../../../../utils/odata.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; +import { globalOptionsZod } from '../../../../Command.js'; + +const options = globalOptionsZod.strict(); class EntraLicenseListCommand extends GraphCommand { public get name(): string { @@ -12,6 +16,10 @@ class EntraLicenseListCommand extends GraphCommand { return 'Lists commercial subscriptions that an organization has acquired'; } + public get schema(): z.ZodTypeAny | undefined { + return options; + } + public defaultProperties(): string[] | undefined { return ['id', 'skuId', 'skuPartNumber']; }