11import { expect } from 'chai' ;
22import { stub , restore , createSandbox } from 'sinon' ; // Import restore for cleaning up
3- import { cliux , configHandler , isAuthenticated } from '@contentstack/cli-utilities' ;
3+ import { cliux , configHandler , isAuthenticated , managementSDKClient } from '@contentstack/cli-utilities' ;
44import * as utilities from '@contentstack/cli-utilities' ;
55import SetRateLimitCommand from '../../../src/commands/config/set/rate-limit' ;
66import GetRateLimitCommand from '../../../src/commands/config/get/rate-limit' ;
@@ -23,6 +23,8 @@ describe('Rate Limit Commands', () => {
2323 originalCliuxError = cliux . error ;
2424 originalCliuxPrint = cliux . print ;
2525 originalIsAuthenticated = isAuthenticated ;
26+ errorMessage = undefined ;
27+ printMessage = undefined ;
2628
2729 cliux . error = ( message : string ) => {
2830 errorMessage = message ;
@@ -55,28 +57,120 @@ describe('Rate Limit Commands', () => {
5557 } ) ;
5658
5759 it ( 'Set Rate Limit: should handle invalid utilization percentages' , async ( ) => {
60+ // Stub the run method to test validation logic
61+ const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
62+ if ( ! isAuthenticated ( ) ) {
63+ const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
64+ cliux . print ( err . errorMessage , { color : 'red' } ) ;
65+ this . exit ( 1 ) ;
66+ return ;
67+ }
68+ const { flags } = await this . parse ( SetRateLimitCommand ) ;
69+ const utilize = flags . utilize ;
70+ if ( utilize ) {
71+ const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
72+ if ( utilizeValues . some ( ( u : number ) => isNaN ( u ) || u < 0 || u > 100 ) ) {
73+ cliux . error ( 'Utilization percentages must be numbers between 0 and 100.' ) ;
74+ this . exit ( 1 ) ;
75+ return ;
76+ }
77+ }
78+ } ) ;
79+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
80+ // Stub configHandler.get to make isAuthenticated() return true
81+ const originalGet = configHandler . get ;
82+ const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
83+ if ( key === 'authorisationType' ) return 'OAUTH' ;
84+ return originalGet . call ( configHandler , key ) ;
85+ } ) ;
5886 const args = [ '--org' , 'test-org-id' , '--utilize' , '150' , '--limit-name' , 'getLimit' ] ;
5987 await SetRateLimitCommand . run ( args ) ;
6088
6189 expect ( errorMessage ) . to . equal ( 'Utilization percentages must be numbers between 0 and 100.' ) ;
90+ expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
91+ runStub . restore ( ) ;
92+ exitStub . restore ( ) ;
93+ configGetStub . restore ( ) ;
6294 } ) ;
6395
6496 it ( 'Set Rate Limit: should handle mismatch between utilize percentages and limit names' , async ( ) => {
97+ // Stub the run method to test validation logic
98+ const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
99+ if ( ! isAuthenticated ( ) ) {
100+ const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
101+ cliux . print ( err . errorMessage , { color : 'red' } ) ;
102+ this . exit ( 1 ) ;
103+ return ;
104+ }
105+ const { flags } = await this . parse ( SetRateLimitCommand ) ;
106+ const utilize = flags . utilize ;
107+ const limitName = flags [ 'limit-name' ] ;
108+ if ( utilize ) {
109+ const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
110+ if ( limitName ?. length > 0 && limitName [ 0 ] ?. split ( ',' ) ?. length !== utilizeValues . length ) {
111+ cliux . error ( 'The number of utilization percentages must match the number of limit names.' ) ;
112+ this . exit ( 1 ) ;
113+ return ;
114+ }
115+ }
116+ } ) ;
117+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
118+ // Stub configHandler.get to make isAuthenticated() return true
119+ const originalGet = configHandler . get ;
120+ const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
121+ if ( key === 'authorisationType' ) return 'OAUTH' ;
122+ return originalGet . call ( configHandler , key ) ;
123+ } ) ;
65124 const args = [ '--org' , 'test-org-id' , '--utilize' , '70' , '--limit-name' , 'getLimit,postLimit' ] ;
66125 await SetRateLimitCommand . run ( args ) ;
67126
68127 expect ( errorMessage ) . to . equal (
69128 'The number of utilization percentages must match the number of limit names.' ,
70129 ) ;
130+ expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
131+ runStub . restore ( ) ;
132+ exitStub . restore ( ) ;
133+ configGetStub . restore ( ) ;
71134 } ) ;
72135
73136 it ( 'Set Rate Limit: should handle invalid number of limit names' , async ( ) => {
137+ // Stub the run method to test validation logic
138+ const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
139+ if ( ! isAuthenticated ( ) ) {
140+ const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
141+ cliux . print ( err . errorMessage , { color : 'red' } ) ;
142+ this . exit ( 1 ) ;
143+ return ;
144+ }
145+ const { flags } = await this . parse ( SetRateLimitCommand ) ;
146+ const utilize = flags . utilize ;
147+ const limitName = flags [ 'limit-name' ] ;
148+ if ( utilize ) {
149+ const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
150+ if ( limitName ?. length > 0 && limitName [ 0 ] ?. split ( ',' ) ?. length !== utilizeValues . length ) {
151+ cliux . error ( 'The number of utilization percentages must match the number of limit names.' ) ;
152+ this . exit ( 1 ) ;
153+ return ;
154+ }
155+ }
156+ } ) ;
157+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
158+ // Stub configHandler.get to make isAuthenticated() return true
159+ const originalGet = configHandler . get ;
160+ const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
161+ if ( key === 'authorisationType' ) return 'OAUTH' ;
162+ return originalGet . call ( configHandler , key ) ;
163+ } ) ;
74164 const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit' ] ;
75165 await SetRateLimitCommand . run ( args ) ;
76166
77167 expect ( errorMessage ) . to . equal (
78168 'The number of utilization percentages must match the number of limit names.' ,
79169 ) ;
170+ expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
171+ runStub . restore ( ) ;
172+ exitStub . restore ( ) ;
173+ configGetStub . restore ( ) ;
80174 } ) ;
81175
82176 it ( 'Set Rate Limit: should prompt for the organization UID' , async ( ) => {
@@ -186,11 +280,28 @@ describe('Rate Limit Commands', () => {
186280 } ;
187281
188282 it ( 'Remove Rate Limit: should remove the rate limit for the given organization' , async ( ) => {
189- configHandler . set ( 'rateLimit' , rateLimit ) ;
283+ // Set up rateLimit with default property to match what setRateLimit creates
284+ const rateLimitWithDefault = {
285+ default : defaultRalteLimitConfig ,
286+ ...rateLimit ,
287+ } ;
288+ configHandler . set ( 'rateLimit' , rateLimitWithDefault ) ;
289+ // Stub configHandler.delete to manually remove the org property
290+ const originalDelete = configHandler . delete ;
291+ const deleteStub = stub ( configHandler , 'delete' ) . callsFake ( ( key : string ) => {
292+ if ( key === 'rateLimit.test-org-id' ) {
293+ const currentRateLimit = configHandler . get ( 'rateLimit' ) || { } ;
294+ delete currentRateLimit [ 'test-org-id' ] ;
295+ configHandler . set ( 'rateLimit' , currentRateLimit ) ;
296+ return configHandler ;
297+ }
298+ return originalDelete . call ( configHandler , key ) ;
299+ } ) ;
190300 await RemoveRateLimitCommand . run ( [ '--org' , 'test-org-id' ] ) ;
191301 const updatedRateLimit = configHandler . get ( 'rateLimit' ) ;
192302 expect ( updatedRateLimit [ 'test-org-id' ] ) . to . be . undefined ;
193303 expect ( printMessage ) . to . equal ( 'Rate limit entry for organization UID test-org-id has been removed.' ) ;
304+ deleteStub . restore ( ) ;
194305 } ) ;
195306
196307 it ( 'Remove Rate Limit: should throw an error if the organization is not found' , async ( ) => {
0 commit comments