Skip to content
30 changes: 25 additions & 5 deletions src/m365/entra/commands/license/license-list.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -65,13 +68,18 @@ 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();
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 All @@ -88,6 +96,7 @@ describe(commands.LICENSE_LIST, () => {
}
};
loggerLogSpy = sinon.spy(logger, 'log');
loggerStderrSpy = sinon.spy(logger, 'logToStderr');
});

afterEach(() => {
Expand All @@ -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']);
});
Expand All @@ -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 () => {
Expand All @@ -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));
});
});
8 changes: 8 additions & 0 deletions src/m365/entra/commands/license/license-list.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'];
}
Expand Down