1+ import assert from 'assert' ;
2+ import sinon from 'sinon' ;
3+ import auth from '../../../../Auth.js' ;
4+ import { cli } from '../../../../cli/cli.js' ;
5+ import { CommandInfo } from '../../../../cli/CommandInfo.js' ;
6+ import { Logger } from '../../../../cli/Logger.js' ;
7+ import { CommandError } from '../../../../Command.js' ;
8+ import request from '../../../../request.js' ;
9+ import { telemetry } from '../../../../telemetry.js' ;
10+ import { pid } from '../../../../utils/pid.js' ;
11+ import { session } from '../../../../utils/session.js' ;
12+ import { sinonUtil } from '../../../../utils/sinonUtil.js' ;
13+ import { z } from 'zod' ;
14+ import commands from '../../commands.js' ;
15+ import command from './brandcenter-settings-list.js' ;
16+
17+ describe ( commands . BRANDCENTER_SETTINGS_LIST , ( ) => {
18+ let log : any [ ] ;
19+ let logger : Logger ;
20+ let loggerLogSpy : sinon . SinonSpy ;
21+ let commandInfo : CommandInfo ;
22+ let commandOptionsSchema : z . ZodTypeAny ;
23+
24+ const successReponse = {
25+ "BrandColorsListId" : "00000000-0000-0000-0000-000000000000" ,
26+ "BrandColorsListUrl" : null ,
27+ "BrandFontLibraryId" : "23af51de-856c-4d00-aa11-0d03af0e46e3" ,
28+ "BrandFontLibraryUrl" : {
29+ "DecodedUrl" : "https://contoso.sharepoint.com/sites/BrandGuide/Fonts"
30+ } ,
31+ "IsBrandCenterSiteFeatureEnabled" : true ,
32+ "IsPublicCdnEnabled" : true ,
33+ "OrgAssets" : {
34+ "CentralAssetRepositoryLibraries" : null ,
35+ "Domain" : {
36+ "DecodedUrl" : "https://contoso.sharepoint.com"
37+ } ,
38+ "OrgAssetsLibraries" : {
39+ "OrgAssetsLibraries" : [
40+ {
41+ "DisplayName" : "Fonts" ,
42+ "FileType" : "" ,
43+ "LibraryUrl" : {
44+ "DecodedUrl" : "sites/BrandGuide/Fonts"
45+ } ,
46+ "ListId" : "23af51de-856c-4d00-aa11-0d03af0e46e3" ,
47+ "OrgAssetFlags" : 0 ,
48+ "OrgAssetType" : 8 ,
49+ "ThumbnailUrl" : null ,
50+ "UniqueId" : "00000000-0000-0000-0000-000000000000"
51+ }
52+ ] ,
53+ "Items" : [
54+ {
55+ "DisplayName" : "Fonts" ,
56+ "FileType" : "" ,
57+ "LibraryUrl" : {
58+ "DecodedUrl" : "sites/BrandGuide/Fonts"
59+ } ,
60+ "ListId" : "23af51de-856c-4d00-aa11-0d03af0e46e3" ,
61+ "OrgAssetFlags" : 0 ,
62+ "OrgAssetType" : 8 ,
63+ "ThumbnailUrl" : null ,
64+ "UniqueId" : "00000000-0000-0000-0000-000000000000"
65+ }
66+ ]
67+ } ,
68+ "SiteId" : "52b46e48-9c0c-40cb-a955-13eb6c717ff3" ,
69+ "Url" : {
70+ "DecodedUrl" : "/sites/BrandGuide"
71+ } ,
72+ "WebId" : "206988d5-e133-4a24-819d-24101f3407ce"
73+ } ,
74+ "SiteId" : "52b46e48-9c0c-40cb-a955-13eb6c717ff3" ,
75+ "SiteUrl" : "https://contoso.sharepoint.com/sites/BrandGuide"
76+ } ;
77+
78+ before ( ( ) => {
79+ sinon . stub ( auth , 'restoreAuth' ) . resolves ( ) ;
80+ sinon . stub ( telemetry , 'trackEvent' ) . resolves ( ) ;
81+ sinon . stub ( pid , 'getProcessName' ) . returns ( '' ) ;
82+ sinon . stub ( session , 'getId' ) . returns ( '' ) ;
83+
84+ auth . connection . active = true ;
85+ auth . connection . spoUrl = 'https://contoso.sharepoint.com' ;
86+ commandInfo = cli . getCommandInfo ( command ) ;
87+ commandOptionsSchema = commandInfo . command . getSchemaToParse ( ) ! ;
88+ } ) ;
89+
90+ beforeEach ( ( ) => {
91+ log = [ ] ;
92+ logger = {
93+ log : async ( msg : string ) => {
94+ log . push ( msg ) ;
95+ } ,
96+ logRaw : async ( msg : string ) => {
97+ log . push ( msg ) ;
98+ } ,
99+ logToStderr : async ( msg : string ) => {
100+ log . push ( msg ) ;
101+ }
102+ } ;
103+ loggerLogSpy = sinon . spy ( logger , 'log' ) ;
104+ } ) ;
105+
106+ afterEach ( ( ) => {
107+ sinonUtil . restore ( [
108+ request . get
109+ ] ) ;
110+ } ) ;
111+
112+ after ( ( ) => {
113+ sinon . restore ( ) ;
114+ auth . connection . active = false ;
115+ auth . connection . spoUrl = undefined ;
116+ } ) ;
117+
118+ it ( 'has correct name' , ( ) => {
119+ assert . strictEqual ( command . name , commands . BRANDCENTER_SETTINGS_LIST ) ;
120+ } ) ;
121+
122+ it ( 'has a description' , ( ) => {
123+ assert . notStrictEqual ( command . description , null ) ;
124+ } ) ;
125+
126+ it ( 'passes validation with no options' , ( ) => {
127+ const actual = commandOptionsSchema . safeParse ( { } ) ;
128+ assert . strictEqual ( actual . success , true ) ;
129+ } ) ;
130+
131+ it ( 'fails validation with unknown options' , ( ) => {
132+ const actual = commandOptionsSchema . safeParse ( { option : "value" } ) ;
133+ assert . strictEqual ( actual . success , false ) ;
134+ } ) ;
135+
136+ it ( 'successfully lists brand center settings' , async ( ) => {
137+ const getStub = sinon . stub ( request , 'get' ) . callsFake ( async ( opts : any ) => {
138+ if ( opts . url === `https://contoso.sharepoint.com/_api/Brandcenter/Configuration` ) {
139+ return successReponse ;
140+ }
141+
142+ throw 'Invalid request' ;
143+ } ) ;
144+
145+ await command . action ( logger , { options : { } } ) ;
146+ assert ( getStub . calledOnce ) ;
147+ assert ( loggerLogSpy . calledWith ( successReponse ) ) ;
148+ } ) ;
149+
150+ it ( 'successfully lists brand center settings with verbose output' , async ( ) => {
151+ const getStub = sinon . stub ( request , 'get' ) . callsFake ( async ( opts : any ) => {
152+ if ( opts . url === `https://contoso.sharepoint.com/_api/Brandcenter/Configuration` ) {
153+ return successReponse ;
154+ }
155+
156+ throw 'Invalid request' ;
157+ } ) ;
158+
159+ await command . action ( logger , { options : { verbose : true } } ) ;
160+ assert ( getStub . calledOnce ) ;
161+ assert ( loggerLogSpy . calledWith ( successReponse ) ) ;
162+ } ) ;
163+
164+ it ( 'correctly handles error when retrieving settings' , async ( ) => {
165+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
166+ if ( opts . url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration' ) {
167+ throw 'An unknown error has occurred' ;
168+ }
169+
170+ throw 'Invalid request' ;
171+ } ) ;
172+
173+ await assert . rejects ( command . action ( logger , { options : { } } ) ,
174+ new CommandError ( `An unknown error has occurred` ) ) ;
175+ } ) ;
176+ } ) ;
0 commit comments