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 { CommandError } from '../../../../Command.js' ;
5
6
import { cli } from '../../../../cli/cli.js' ;
@@ -19,6 +20,7 @@ describe(commands.M365GROUP_GET, () => {
19
20
let logger : Logger ;
20
21
let loggerLogSpy : sinon . SinonSpy ;
21
22
let commandInfo : CommandInfo ;
23
+ let commandOptionsSchema : z . ZodTypeAny ;
22
24
23
25
before ( ( ) => {
24
26
sinon . stub ( auth , 'restoreAuth' ) . resolves ( ) ;
@@ -28,6 +30,7 @@ describe(commands.M365GROUP_GET, () => {
28
30
sinon . stub ( entraGroup , 'isUnifiedGroup' ) . resolves ( true ) ;
29
31
auth . connection . active = true ;
30
32
commandInfo = cli . getCommandInfo ( command ) ;
33
+ commandOptionsSchema = commandInfo . command . getSchemaToParse ( ) ! ;
31
34
} ) ;
32
35
33
36
beforeEach ( ( ) => {
@@ -109,7 +112,7 @@ describe(commands.M365GROUP_GET, () => {
109
112
throw 'Invalid request' ;
110
113
} ) ;
111
114
112
- await command . action ( logger , { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } } ) ;
115
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } ) } ) ;
113
116
assert ( loggerLogSpy . calledWith ( {
114
117
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
115
118
"deletedDateTime" : null ,
@@ -189,7 +192,7 @@ describe(commands.M365GROUP_GET, () => {
189
192
throw 'Invalid request' ;
190
193
} ) ;
191
194
192
- await command . action ( logger , { options : { displayName : 'Finance' } } ) ;
195
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { displayName : 'Finance' } ) } ) ;
193
196
assert ( loggerLogSpy . calledWith ( {
194
197
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
195
198
"deletedDateTime" : null ,
@@ -265,7 +268,7 @@ describe(commands.M365GROUP_GET, () => {
265
268
throw 'Invalid request' ;
266
269
} ) ;
267
270
268
- await command . action ( logger , { options : { debug : true , id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } } ) ;
271
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { debug : true , id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } ) } ) ;
269
272
assert ( loggerLogSpy . calledWith ( {
270
273
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
271
274
"deletedDateTime" : null ,
@@ -346,7 +349,7 @@ describe(commands.M365GROUP_GET, () => {
346
349
throw 'Invalid request' ;
347
350
} ) ;
348
351
349
- await command . action ( logger , { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , includeSiteUrl : true } } ) ;
352
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , includeSiteUrl : true } ) } ) ;
350
353
assert ( loggerErrSpy . calledWith ( chalk . yellow ( `Parameter 'includeSiteUrl' is deprecated. Please use 'withSiteUrl' instead` ) ) ) ;
351
354
352
355
sinonUtil . restore ( loggerErrSpy ) ;
@@ -397,7 +400,7 @@ describe(commands.M365GROUP_GET, () => {
397
400
throw 'Invalid request' ;
398
401
} ) ;
399
402
400
- await command . action ( logger , { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } } ) ;
403
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } ) } ) ;
401
404
assert ( loggerLogSpy . calledWith ( {
402
405
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
403
406
"deletedDateTime" : null ,
@@ -478,7 +481,7 @@ describe(commands.M365GROUP_GET, () => {
478
481
throw 'Invalid request' ;
479
482
} ) ;
480
483
481
- await command . action ( logger , { options : { debug : true , id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } } ) ;
484
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { debug : true , id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } ) } ) ;
482
485
assert ( loggerLogSpy . calledWith ( {
483
486
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
484
487
"deletedDateTime" : null ,
@@ -559,7 +562,7 @@ describe(commands.M365GROUP_GET, () => {
559
562
throw 'Invalid request' ;
560
563
} ) ;
561
564
562
- await command . action ( logger , { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } } ) ;
565
+ await command . action ( logger , { options : commandOptionsSchema . parse ( { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' , withSiteUrl : true } ) } ) ;
563
566
assert ( loggerLogSpy . calledWith ( {
564
567
"id" : "1caf7dcd-7e83-4c3a-94f7-932a1299c844" ,
565
568
"deletedDateTime" : null ,
@@ -597,28 +600,39 @@ describe(commands.M365GROUP_GET, () => {
597
600
const errorMessage = 'Something went wrong' ;
598
601
sinon . stub ( request , 'get' ) . rejects ( new Error ( errorMessage ) ) ;
599
602
600
- await assert . rejects ( command . action ( logger , { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } } ) , new CommandError ( errorMessage ) ) ;
603
+ await assert . rejects ( command . action ( logger , { options : commandOptionsSchema . parse ( { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } ) } ) , new CommandError ( errorMessage ) ) ;
601
604
} ) ;
602
605
603
- it ( 'fails validation if the id is not a valid GUID' , async ( ) => {
604
- const actual = await command . validate ( { options : { id : '123' } } , commandInfo ) ;
605
- assert . notStrictEqual ( actual , true ) ;
606
+ it ( 'fails validation when id and displayName are not specified' , ( ) => {
607
+ const actual = commandOptionsSchema . safeParse ( { } ) ;
608
+ assert . strictEqual ( actual . success , false ) ;
606
609
} ) ;
607
610
608
- it ( 'passes validation if the id is a valid GUID' , async ( ) => {
609
- const actual = await command . validate ( { options : { id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' } } , commandInfo ) ;
610
- assert . strictEqual ( actual , true ) ;
611
+ it ( 'fails validation when both id and displayName are specified' , ( ) => {
612
+ const actual = commandOptionsSchema . safeParse ( {
613
+ id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844' ,
614
+ displayName : 'Finance'
615
+ } ) ;
616
+ assert . strictEqual ( actual . success , false ) ;
611
617
} ) ;
612
618
613
- it ( 'supports specifying id' , ( ) => {
614
- const options = command . options ;
615
- let containsOption = false ;
616
- options . forEach ( o => {
617
- if ( o . option . indexOf ( '--id' ) > - 1 ) {
618
- containsOption = true ;
619
- }
619
+ it ( 'fails validation if the id is not a valid GUID' , ( ) => {
620
+ const actual = commandOptionsSchema . safeParse ( { id : '123' } ) ;
621
+ assert . strictEqual ( actual . success , false ) ;
622
+ } ) ;
623
+
624
+ it ( 'passes validation if the id is a valid GUID' , ( ) => {
625
+ const actual = commandOptionsSchema . safeParse ( {
626
+ id : '1caf7dcd-7e83-4c3a-94f7-932a1299c844'
627
+ } ) ;
628
+ assert . strictEqual ( actual . success , true ) ;
629
+ } ) ;
630
+
631
+ it ( 'passes validation when displayName is specified' , ( ) => {
632
+ const actual = commandOptionsSchema . safeParse ( {
633
+ displayName : 'Finance'
620
634
} ) ;
621
- assert ( containsOption ) ;
635
+ assert . strictEqual ( actual . success , true ) ;
622
636
} ) ;
623
637
624
638
it ( 'shows error when the group is not a unified group' , async ( ) => {
@@ -650,7 +664,7 @@ describe(commands.M365GROUP_GET, () => {
650
664
sinonUtil . restore ( entraGroup . isUnifiedGroup ) ;
651
665
sinon . stub ( entraGroup , 'isUnifiedGroup' ) . resolves ( false ) ;
652
666
653
- await assert . rejects ( command . action ( logger , { options : { id : groupId } } ) ,
667
+ await assert . rejects ( command . action ( logger , { options : commandOptionsSchema . parse ( { id : groupId } ) } ) ,
654
668
new CommandError ( `Specified group with id '${ groupId } ' is not a Microsoft 365 group.` ) ) ;
655
669
} ) ;
656
670
} ) ;
0 commit comments