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 './site-versionpolicy-get.js' ;
16+
17+ describe ( commands . SITE_VERSIONPOLICY_GET , ( ) => {
18+ let log : any [ ] ;
19+ let logger : Logger ;
20+ let loggerLogSpy : sinon . SinonSpy ;
21+ let commandInfo : CommandInfo ;
22+ let commandOptionsSchema : z . ZodTypeAny ;
23+ const validSiteUrl = "https://contoso.sharepoint.com" ;
24+
25+ before ( ( ) => {
26+ sinon . stub ( auth , 'restoreAuth' ) . resolves ( ) ;
27+ sinon . stub ( telemetry , 'trackEvent' ) . resolves ( ) ;
28+ sinon . stub ( pid , 'getProcessName' ) . returns ( '' ) ;
29+ sinon . stub ( session , 'getId' ) . returns ( '' ) ;
30+ commandInfo = cli . getCommandInfo ( command ) ;
31+ commandOptionsSchema = commandInfo . command . getSchemaToParse ( ) ! ;
32+ auth . connection . active = true ;
33+ } ) ;
34+
35+ beforeEach ( ( ) => {
36+ log = [ ] ;
37+ logger = {
38+ log : async ( msg : string ) => {
39+ log . push ( msg ) ;
40+ } ,
41+ logRaw : async ( msg : string ) => {
42+ log . push ( msg ) ;
43+ } ,
44+ logToStderr : async ( msg : string ) => {
45+ log . push ( msg ) ;
46+ }
47+ } ;
48+ loggerLogSpy = sinon . spy ( logger , 'log' ) ;
49+ } ) ;
50+
51+ afterEach ( ( ) => {
52+ sinonUtil . restore ( [
53+ request . get
54+ ] ) ;
55+ } ) ;
56+
57+ after ( ( ) => {
58+ sinon . restore ( ) ;
59+ auth . connection . active = false ;
60+ } ) ;
61+
62+ it ( 'has correct name' , ( ) => {
63+ assert . strictEqual ( command . name , commands . SITE_VERSIONPOLICY_GET ) ;
64+ } ) ;
65+
66+ it ( 'has a description' , ( ) => {
67+ assert . notStrictEqual ( command . description , null ) ;
68+ } ) ;
69+
70+ it ( 'fails validation if site URL is not a valid URL' , async ( ) => {
71+ const actual = commandOptionsSchema . safeParse ( { siteUrl : 'foo' } ) ;
72+ assert . strictEqual ( actual . success , false ) ;
73+ } ) ;
74+
75+ it ( 'passes validation if valid site URL is specified' , async ( ) => {
76+ const actual = await command . validate ( { options : { siteUrl : validSiteUrl } } , commandInfo ) ;
77+ assert . strictEqual ( actual , true ) ;
78+ } ) ;
79+
80+ it ( 'retrieves "age" version policy settings for the specified site' , async ( ) => {
81+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
82+ if ( opts . url === `${ validSiteUrl } /_api/site/VersionPolicyForNewLibrariesTemplate?$expand=VersionPolicies` ) {
83+ return {
84+ VersionPolicies : {
85+ DefaultTrimMode : 1 ,
86+ DefaultExpireAfterDays : 200
87+ } ,
88+ MajorVersionLimit : 100
89+ } ;
90+ }
91+
92+ throw 'Invalid request' ;
93+ } ) ;
94+
95+ await command . action ( logger , { options : { siteUrl : validSiteUrl } } ) ;
96+ assert ( loggerLogSpy . calledWith ( {
97+ defaultTrimMode : 'age' ,
98+ defaultExpireAfterDays : 200 ,
99+ majorVersionLimit : 100
100+ } ) ) ;
101+ } ) ;
102+
103+ it ( 'retrieves "automatic" version policy settings for the specified site' , async ( ) => {
104+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
105+ if ( opts . url === `${ validSiteUrl } /_api/site/VersionPolicyForNewLibrariesTemplate?$expand=VersionPolicies` ) {
106+ return {
107+ VersionPolicies : {
108+ DefaultTrimMode : 2 ,
109+ DefaultExpireAfterDays : 30
110+ } ,
111+ MajorVersionLimit : 500
112+ } ;
113+ }
114+
115+ throw 'Invalid request' ;
116+ } ) ;
117+
118+ await command . action ( logger , { options : { siteUrl : validSiteUrl } } ) ;
119+ assert ( loggerLogSpy . calledWith ( {
120+ defaultTrimMode : 'automatic' ,
121+ defaultExpireAfterDays : 30 ,
122+ majorVersionLimit : 500
123+ } ) ) ;
124+ } ) ;
125+
126+ it ( 'retrieves "number" version policy settings for the specified site' , async ( ) => {
127+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
128+ if ( opts . url === `${ validSiteUrl } /_api/site/VersionPolicyForNewLibrariesTemplate?$expand=VersionPolicies` ) {
129+ return {
130+ VersionPolicies : {
131+ DefaultTrimMode : 0 ,
132+ DefaultExpireAfterDays : 0
133+ } ,
134+ MajorVersionLimit : 300
135+ } ;
136+ }
137+
138+ throw 'Invalid request' ;
139+ } ) ;
140+
141+ await command . action ( logger , { options : { siteUrl : validSiteUrl , verbose : true } } ) ;
142+ assert ( loggerLogSpy . calledWith ( {
143+ defaultTrimMode : 'number' ,
144+ defaultExpireAfterDays : 0 ,
145+ majorVersionLimit : 300
146+ } ) ) ;
147+ } ) ;
148+
149+ it ( 'retrieves "inheritTenant" version policy settings for the specified site' , async ( ) => {
150+ sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
151+ if ( opts . url === `${ validSiteUrl } /_api/site/VersionPolicyForNewLibrariesTemplate?$expand=VersionPolicies` ) {
152+ return {
153+ MajorVersionLimit : - 1
154+ } ;
155+ }
156+
157+ throw 'Invalid request' ;
158+ } ) ;
159+
160+ await command . action ( logger , { options : { siteUrl : validSiteUrl , verbose : true } } ) ;
161+ assert ( loggerLogSpy . calledWith ( {
162+ defaultTrimMode : 'inheritTenant' ,
163+ defaultExpireAfterDays : null ,
164+ majorVersionLimit : - 1
165+ } ) ) ;
166+ } ) ;
167+
168+ it ( 'correctly handles API OData error' , async ( ) => {
169+ sinon . stub ( request , 'get' ) . rejects ( new Error ( 'An error has occurred' ) ) ;
170+
171+ await assert . rejects ( command . action ( logger , { options : { siteUrl : validSiteUrl } } ) ,
172+ new CommandError ( 'An error has occurred' ) ) ;
173+ } ) ;
174+ } ) ;
0 commit comments