1
1
import assert from 'assert';
2
2
import sinon from "sinon";
3
+ import { z } from 'zod';
3
4
import auth from '../../../../Auth.js';
4
5
import { CommandInfo } from "../../../../cli/CommandInfo.js";
5
6
import { Logger } from "../../../../cli/Logger.js";
@@ -19,6 +20,7 @@ describe(commands.ADMINISTRATIVEUNIT_GET, () => {
19
20
let logger: Logger;
20
21
let loggerLogSpy: sinon.SinonSpy;
21
22
let commandInfo: CommandInfo;
23
+ let commandOptionsSchema: z.ZodTypeAny;
22
24
const administrativeUnitsReponse = {
23
25
value: [
24
26
{
@@ -43,6 +45,7 @@ describe(commands.ADMINISTRATIVEUNIT_GET, () => {
43
45
sinon.stub(session, 'getId').returns('');
44
46
auth.connection.active = true;
45
47
commandInfo = cli.getCommandInfo(command);
48
+ commandOptionsSchema = commandInfo.command.getSchemaToParse()!;
46
49
});
47
50
48
51
beforeEach(() => {
@@ -82,6 +85,32 @@ describe(commands.ADMINISTRATIVEUNIT_GET, () => {
82
85
assert.notStrictEqual(command.description, null);
83
86
});
84
87
88
+ it('fails validation when id or displayName are not specified', () => {
89
+ const actual = commandOptionsSchema.safeParse({});
90
+ assert.strictEqual(actual.success, false);
91
+ });
92
+
93
+ it('passes validation when id is specified', () => {
94
+ const actual = commandOptionsSchema.safeParse({
95
+ id: validId
96
+ });
97
+ assert.strictEqual(actual.success, true);
98
+ });
99
+
100
+ it('passes validation when displayName is specified', () => {
101
+ const actual = commandOptionsSchema.safeParse({
102
+ displayName: validDisplayName
103
+ });
104
+ assert.strictEqual(actual.success, true);
105
+ });
106
+
107
+ it('fails validation if the id is not a valid GUID', () => {
108
+ const actual = commandOptionsSchema.safeParse({
109
+ id: '123'
110
+ });
111
+ assert.strictEqual(actual.success, false);
112
+ });
113
+
85
114
it('retrieves information about the specified administrative unit by id', async () => {
86
115
sinon.stub(request, 'get').callsFake(async (opts) => {
87
116
if (opts.url === `https://graph.microsoft.com/v1.0/directory/administrativeUnits/${validId}`) {
@@ -91,7 +120,7 @@ describe(commands.ADMINISTRATIVEUNIT_GET, () => {
91
120
throw 'Invalid request';
92
121
});
93
122
94
- await command.action(logger, { options: { id: validId } });
123
+ await command.action(logger, { options: commandOptionsSchema.parse( { id: validId }) });
95
124
assert(loggerLogSpy.calledOnceWithExactly(administrativeUnitsReponse.value[0]));
96
125
});
97
126
@@ -104,36 +133,21 @@ describe(commands.ADMINISTRATIVEUNIT_GET, () => {
104
133
throw 'Invalid request';
105
134
});
106
135
107
- await command.action(logger, { options: { id: validId, properties: 'id,displayName,visibility' } });
136
+ await command.action(logger, { options: commandOptionsSchema.parse( { id: validId, properties: 'id,displayName,visibility' }) });
108
137
assert(loggerLogSpy.calledOnceWithExactly(administrativeUnitsReponse.value[0]));
109
138
});
110
139
111
140
it('retrieves information about the specified administrative unit by displayName', async () => {
112
141
sinon.stub(entraAdministrativeUnit, 'getAdministrativeUnitByDisplayName').resolves(administrativeUnitsReponse.value[0]);
113
142
114
- await command.action(logger, { options: { displayName: validDisplayName } });
143
+ await command.action(logger, { options: commandOptionsSchema.parse( { displayName: validDisplayName }) });
115
144
assert(loggerLogSpy.calledOnceWithExactly(administrativeUnitsReponse.value[0]));
116
145
});
117
146
118
147
it('handles random API error', async () => {
119
148
const errorMessage = 'Something went wrong';
120
149
sinon.stub(request, 'get').rejects(new Error(errorMessage));
121
150
122
- await assert.rejects(command.action(logger, { options: { id: validId } }), new CommandError(errorMessage));
123
- });
124
-
125
- it('fails validation if the id is not a valid GUID', async () => {
126
- const actual = await command.validate({ options: { id: '123' } }, commandInfo);
127
- assert.notStrictEqual(actual, true);
128
- });
129
-
130
- it('passes validation if the id is a valid GUID', async () => {
131
- const actual = await command.validate({ options: { id: validId } }, commandInfo);
132
- assert.strictEqual(actual, true);
133
- });
134
-
135
- it('passes validation if required options specified (displayName)', async () => {
136
- const actual = await command.validate({ options: { displayName: validDisplayName } }, commandInfo);
137
- assert.strictEqual(actual, true);
151
+ await assert.rejects(command.action(logger, { options: commandOptionsSchema.parse({ id: validId }) }), new CommandError(errorMessage));
138
152
});
139
153
});
0 commit comments