1+ import assert from 'assert' ;
2+ import sinon from 'sinon' ;
3+ import auth from '../../../../Auth.js' ;
4+ import { Logger } from '../../../../cli/Logger.js' ;
5+ import { CommandError } from '../../../../Command.js' ;
6+ import request from '../../../../request.js' ;
7+ import { telemetry } from '../../../../telemetry.js' ;
8+ import { pid } from '../../../../utils/pid.js' ;
9+ import { session } from '../../../../utils/session.js' ;
10+ import { sinonUtil } from '../../../../utils/sinonUtil.js' ;
11+ import commands from '../../commands.js' ;
12+ import command from './engage-role-list.js' ;
13+
14+ describe ( commands . ENGAGE_ROLE_LIST , ( ) => {
15+ let log : string [ ] ;
16+ let logger : Logger ;
17+ let loggerLogSpy : sinon . SinonSpy ;
18+
19+ before ( ( ) => {
20+ sinon . stub ( auth , 'restoreAuth' ) . resolves ( ) ;
21+ sinon . stub ( telemetry , 'trackEvent' ) . resolves ( ) ;
22+ sinon . stub ( pid , 'getProcessName' ) . returns ( '' ) ;
23+ sinon . stub ( session , 'getId' ) . returns ( '' ) ;
24+ auth . connection . active = true ;
25+ } ) ;
26+
27+ beforeEach ( ( ) => {
28+ log = [ ] ;
29+ logger = {
30+ log : async ( msg : string ) => {
31+ log . push ( msg ) ;
32+ } ,
33+ logRaw : async ( msg : string ) => {
34+ log . push ( msg ) ;
35+ } ,
36+ logToStderr : async ( msg : string ) => {
37+ log . push ( msg ) ;
38+ }
39+ } ;
40+ loggerLogSpy = sinon . spy ( logger , 'log' ) ;
41+ } ) ;
42+
43+ afterEach ( ( ) => {
44+ sinonUtil . restore ( [
45+ request . get
46+ ] ) ;
47+ } ) ;
48+
49+ after ( ( ) => {
50+ sinon . restore ( ) ;
51+ auth . connection . active = false ;
52+ } ) ;
53+
54+ it ( 'has correct name' , ( ) => {
55+ assert . strictEqual ( command . name , commands . ENGAGE_ROLE_LIST ) ;
56+ } ) ;
57+
58+ it ( 'has a description' , ( ) => {
59+ assert . notStrictEqual ( command . description , null ) ;
60+ } ) ;
61+
62+ it ( 'defines correct properties for the default output' , ( ) => {
63+ assert . deepStrictEqual ( command . defaultProperties ( ) , [ 'id' , 'displayName' ] ) ;
64+ } ) ;
65+
66+ it ( `should get a list of Viva Engage roles` , async ( ) => {
67+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
68+ if ( opts . url === `https://graph.microsoft.com/beta/employeeExperience/roles` ) {
69+ return {
70+ "value" : [
71+ {
72+ "id" : "ec759127-089f-4f91-8dfc-03a30b51cb38" ,
73+ "displayName" : "Network Admin"
74+ } ,
75+ {
76+ "id" : "966b8ec4-6457-4f22-bd3c-5a2520e98f4a" ,
77+ "displayName" : "Verified Admin"
78+ } ,
79+ {
80+ "id" : "77aa47ad-96fe-4ecc-8024-fd1ac5e28f17" ,
81+ "displayName" : "Corporate Communicator"
82+ }
83+ ]
84+ } ;
85+ }
86+
87+ throw 'Invalid request' ;
88+ } ) ;
89+
90+ await command . action ( logger , {
91+ options : { verbose : true }
92+ } ) ;
93+
94+ assert (
95+ loggerLogSpy . calledOnceWith ( [
96+ {
97+ "id" : "ec759127-089f-4f91-8dfc-03a30b51cb38" ,
98+ "displayName" : "Network Admin"
99+ } ,
100+ {
101+ "id" : "966b8ec4-6457-4f22-bd3c-5a2520e98f4a" ,
102+ "displayName" : "Verified Admin"
103+ } ,
104+ {
105+ "id" : "77aa47ad-96fe-4ecc-8024-fd1ac5e28f17" ,
106+ "displayName" : "Corporate Communicator"
107+ }
108+ ] )
109+ ) ;
110+ } ) ;
111+
112+ it ( 'handles error when retrieving Viva Engage roles failed' , async ( ) => {
113+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
114+ if ( opts . url === `https://graph.microsoft.com/beta/employeeExperience/roles` ) {
115+ throw { error : { message : 'An error has occurred' } } ;
116+ }
117+ throw `Invalid request` ;
118+ } ) ;
119+
120+ await assert . rejects (
121+ command . action ( logger , { options : { } } ) ,
122+ new CommandError ( 'An error has occurred' )
123+ ) ;
124+ } ) ;
125+ } ) ;
0 commit comments