Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -10,7 +10,7 @@ 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 './applicationcustomizer-add.js';
import command, { options } from './applicationcustomizer-add.js';

describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
const webUrl = 'https://contoso.sharepoint.com';
Expand Down Expand Up @@ -48,6 +48,7 @@ describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
let log: any[];
let logger: Logger;
let commandInfo: CommandInfo;
let commandOptionsSchema: typeof options;
let loggerLogToStderrSpy: sinon.SinonSpy;

before(() => {
Expand All @@ -57,6 +58,7 @@ describe(commands.APPLICATIONCUSTOMIZER_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 @@ -103,7 +105,7 @@ describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
throw customActionError;
});

await command.action(logger, { options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, scope: 'Web' } });
await command.action(logger, { options: commandOptionsSchema.parse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, scope: 'Web' }) });
assert.deepStrictEqual(postStub.firstCall.args[0].data, {
Title: title,
Name: title,
Expand All @@ -124,7 +126,7 @@ describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
throw customActionError;
});

await command.action(logger, { options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, description: description, clientSideComponentProperties: clientSideComponentProperties, verbose: true } });
await command.action(logger, { options: commandOptionsSchema.parse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, description: description, clientSideComponentProperties: clientSideComponentProperties, verbose: true }) });
assert.deepStrictEqual(postStub.firstCall.args[0].data, {
Title: title,
Name: title,
Expand All @@ -146,7 +148,7 @@ describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
throw customActionError;
});

await command.action(logger, { options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, description: description, hostProperties: clientSideComponentProperties, verbose: true } });
await command.action(logger, { options: commandOptionsSchema.parse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, description: description, hostProperties: clientSideComponentProperties, verbose: true }) });
assert.deepStrictEqual(postStub.firstCall.args[0].data, {
Title: title,
Name: title,
Expand All @@ -158,33 +160,38 @@ describe(commands.APPLICATIONCUSTOMIZER_ADD, () => {
assert(loggerLogToStderrSpy.called);
});

it('fails validation if the webUrl option is not a valid SharePoint site URL', async () => {
const actual = await command.validate({ options: { webUrl: 'foo', title: title, clientSideComponentId: clientSideComponentId } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if the webUrl option is not a valid SharePoint site URL', () => {
const actual = commandOptionsSchema.safeParse({ webUrl: 'foo', title: title, clientSideComponentId: clientSideComponentId });
assert.strictEqual(actual.success, false);
});

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

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

it('fails validation if the clientSideComponentProperties option is not a valid json string', async () => {
const actual = await command.validate({ options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, clientSideComponentProperties: 'invalid json string' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation with unknown options', () => {
const actual = commandOptionsSchema.safeParse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, unknownOption: 'value' });
assert.strictEqual(actual.success, false);
});

it('fails validation if the hostProperties option is not a valid json string', async () => {
const actual = await command.validate({ options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, hostProperties: 'invalid json string' } }, commandInfo);
assert.notStrictEqual(actual, true);
it('fails validation if the clientSideComponentProperties option is not a valid json string', () => {
const actual = commandOptionsSchema.safeParse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, clientSideComponentProperties: 'invalid json string' });
assert.notStrictEqual(actual.success, true);
});

it('passes validation if all options are passed', async () => {
const actual = await command.validate({ options: { webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, clientSideComponentProperties: clientSideComponentProperties, hostProperties: clientSideComponentProperties, scope: 'Site' } }, commandInfo);
assert.strictEqual(actual, true);
it('fails validation if the hostProperties option is not a valid json string', () => {
const actual = commandOptionsSchema.safeParse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, hostProperties: 'invalid json string' });
assert.notStrictEqual(actual.success, true);
});

it('passes validation if all options are passed', () => {
const actual = commandOptionsSchema.safeParse({ webUrl: webUrl, title: title, clientSideComponentId: clientSideComponentId, clientSideComponentProperties: clientSideComponentProperties, hostProperties: clientSideComponentProperties, scope: 'Site' });
assert.strictEqual(actual.success, true);
});
});
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
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 SpoCommand from '../../../base/SpoCommand.js';
import commands from '../../commands.js';
import { CustomAction } from '../customaction/customaction.js';

export const options = z.strictObject({
...globalOptionsZod.shape,
title: z.string().alias('t'),
webUrl: z.string().refine(val => validation.isValidSharePointUrl(val) === true, {
error: e => `${e.input} is not a valid SharePoint URL`
}).alias('u'),
clientSideComponentId: z.string().refine(val => validation.isValidGuid(val), {
error: e => `${e.input} is not a valid GUID`
}).alias('i'),
description: z.string().optional(),
clientSideComponentProperties: z.string().refine(val => {
try {
JSON.parse(val);
return true;
}
catch {
return false;
}
}, {
error: e => `An error has occurred while parsing clientSideComponentProperties: ${e.input}`
}).optional(),
Comment thread
MartinM85 marked this conversation as resolved.
hostProperties: z.string().refine(val => {
try {
JSON.parse(val);
return true;
}
catch {
return false;
}
}, {
error: e => `An error has occurred while parsing hostProperties: ${e.input}`
}).optional(),
Comment thread
MartinM85 marked this conversation as resolved.
scope: z.enum(['Site', 'Web']).optional().alias('s')
});

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

interface CommandArgs {
options: Options;
}

interface Options extends GlobalOptions {
title: string;
webUrl: string;
clientSideComponentId: string;
description?: string;
clientSideComponentProperties?: string;
hostProperties?: string;
scope?: string;
}

class SpoApplicationCustomizerAddCommand extends SpoCommand {
private static readonly scopes: string[] = ['Site', 'Web'];

public get name(): string {
return commands.APPLICATIONCUSTOMIZER_ADD;
}
Expand All @@ -31,90 +57,8 @@ class SpoApplicationCustomizerAddCommand extends SpoCommand {
return 'Add an application customizer to a site.';
}

constructor() {
super();

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

#initOptions(): void {
this.options.unshift(
{
option: '-t, --title <title>'
},
{
option: '-u, --webUrl <webUrl>'
},
{
option: '-i, --clientSideComponentId <clientSideComponentId>'
},
{
option: '--description [description]'
},
{
option: '--clientSideComponentProperties [clientSideComponentProperties]'
},
{
option: '--hostProperties [hostProperties]'
},
{
option: '-s, --scope [scope]', autocomplete: SpoApplicationCustomizerAddCommand.scopes
}
);
}

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

#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => {
if (args.options.webUrl) {
const isValidSharePointUrl: boolean | string = validation.isValidSharePointUrl(args.options.webUrl);
if (isValidSharePointUrl !== true) {
return isValidSharePointUrl;
}
}

if (!validation.isValidGuid(args.options.clientSideComponentId)) {
return `${args.options.clientSideComponentId} is not a valid GUID`;
}

if (args.options.clientSideComponentProperties) {
try {
JSON.parse(args.options.clientSideComponentProperties);
}
catch (e) {
return `An error has occurred while parsing clientSideComponentProperties: ${e}`;
}
}

if (args.options.hostProperties) {
try {
JSON.parse(args.options.hostProperties);
}
catch (e) {
return `An error has occurred while parsing hostProperties: ${e}`;
}
}

if (args.options.scope && SpoApplicationCustomizerAddCommand.scopes.indexOf(args.options.scope) < 0) {
return `${args.options.scope} is not a valid value for allowedMembers. Valid values are ${SpoApplicationCustomizerAddCommand.scopes.join(', ')}`;
}

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

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