Skip to content
Draft
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
23 changes: 19 additions & 4 deletions src/m365/external/commands/connection/connection-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ 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 './connection-list.js';
import command, { options } from './connection-list.js';
import { CommandInfo } from '../../../../cli/CommandInfo.js';
import { cli } from '../../../../cli/cli.js';

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

const externalConnections: { value: ExternalConnectors.ExternalConnection[] } = {
value: [
Expand All @@ -39,6 +43,8 @@ describe(commands.CONNECTION_LIST, () => {
sinon.stub(pid, 'getProcessName').returns('');
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 @@ -85,14 +91,23 @@ describe(commands.CONNECTION_LIST, () => {
assert.deepStrictEqual(command.defaultProperties(), ['id', 'name', 'state']);
});

it('passes validation with no options', () => {
const actual = commandOptionsSchema.safeParse({});
assert.strictEqual(actual.success, true);
});

it('fails validation with unknown options', () => {
const actual = commandOptionsSchema.safeParse({ option: "value" });
assert.strictEqual(actual.success, false);
});

it('correctly handles error', async () => {
sinon.stub(request, 'get').callsFake(() => {
throw 'An error has occurred';
});

await assert.rejects(command.action(logger, {
options: {
}
options: commandOptionsSchema.parse({})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commandOptionsSchema.parse is redundant and can be removed, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's redundant in this case, but the commandOptionsSchema.parse will enforce the schema validation and check at least that no unknown option is passed to the command.

}), new CommandError('An error has occurred'));
});

Expand All @@ -105,7 +120,7 @@ describe(commands.CONNECTION_LIST, () => {
throw 'Invalid request';
});

await command.action(logger, { options: { debug: true } } as any);
await command.action(logger, { options: commandOptionsSchema.parse({ debug: true }) });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commandOptionsSchema.parse is redundant and can be removed, no?

assert(loggerLogSpy.calledWith(externalConnections.value));
});
});
8 changes: 8 additions & 0 deletions src/m365/external/commands/connection/connection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Logger } from '../../../../cli/Logger.js';
import { odata } from '../../../../utils/odata.js';
import GraphCommand from '../../../base/GraphCommand.js';
import commands from '../../commands.js';
import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';

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

class ExternalConnectionListCommand extends GraphCommand {
public get name(): string {
Expand All @@ -16,6 +20,10 @@ class ExternalConnectionListCommand extends GraphCommand {
return [commands.EXTERNALCONNECTION_LIST];
}

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

public defaultProperties(): string[] | undefined {
return ['id', 'name', 'state'];
}
Expand Down