11import { expect } from 'chai' ;
2- import { stub , restore , createSandbox } from 'sinon' ; // Import restore for cleaning up
3- import { cliux , configHandler , isAuthenticated , managementSDKClient } from '@contentstack/cli-utilities' ;
4- import * as utilities from '@contentstack/cli-utilities' ;
2+ import { stub , restore } from 'sinon' ; // Import restore for cleaning up
3+ import { cliux , configHandler } from '@contentstack/cli-utilities' ;
54import SetRateLimitCommand from '../../../src/commands/config/set/rate-limit' ;
65import GetRateLimitCommand from '../../../src/commands/config/get/rate-limit' ;
76import RemoveRateLimitCommand from '../../../src/commands/config/remove/rate-limit' ;
@@ -16,9 +15,6 @@ describe('Rate Limit Commands', () => {
1615 let mockClient : any ;
1716
1817 beforeEach ( ( ) => {
19- originalCliuxError = cliux . error ;
20- originalCliuxPrint = cliux . print ;
21- originalIsAuthenticated = isAuthenticated ;
2218 errorMessage = undefined ;
2319 printMessage = undefined ;
2420
@@ -34,8 +30,8 @@ describe('Rate Limit Commands', () => {
3430
3531 stub ( cliux , 'error' ) . callsFake ( ( message : string ) => {
3632 errorMessage = message ;
37- } ;
38- cliux . print = ( message : string , ... args : any [ ] ) => {
33+ } ) ;
34+ stub ( cliux , 'print' ) . callsFake ( ( message : string ) => {
3935 printMessage = message ;
4036 } ) ;
4137 rateLimitHandler = new RateLimitHandler ( ) ;
@@ -60,120 +56,45 @@ describe('Rate Limit Commands', () => {
6056 } ) ;
6157
6258 it ( 'Set Rate Limit: should handle invalid utilization percentages' , async ( ) => {
63- // Stub the run method to test validation logic
64- const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
65- if ( ! isAuthenticated ( ) ) {
66- const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
67- cliux . print ( err . errorMessage , { color : 'red' } ) ;
68- this . exit ( 1 ) ;
69- return ;
70- }
71- const { flags } = await this . parse ( SetRateLimitCommand ) ;
72- const utilize = flags . utilize ;
73- if ( utilize ) {
74- const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
75- if ( utilizeValues . some ( ( u : number ) => isNaN ( u ) || u < 0 || u > 100 ) ) {
76- cliux . error ( 'Utilization percentages must be numbers between 0 and 100.' ) ;
77- this . exit ( 1 ) ;
78- return ;
79- }
80- }
81- } ) ;
82- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
83- // Stub configHandler.get to make isAuthenticated() return true
84- const originalGet = configHandler . get ;
85- const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
86- if ( key === 'authorisationType' ) return 'OAUTH' ;
87- return originalGet . call ( configHandler , key ) ;
88- } ) ;
59+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
60+
8961 const args = [ '--org' , 'test-org-id' , '--utilize' , '150' , '--limit-name' , 'getLimit' ] ;
9062 await SetRateLimitCommand . run ( args ) ;
9163
9264 expect ( errorMessage ) . to . equal ( 'Utilization percentages must be numbers between 0 and 100.' ) ;
65+
9366 expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
94- runStub . restore ( ) ;
67+
68+ // Restore the stub after the test
9569 exitStub . restore ( ) ;
96- configGetStub . restore ( ) ;
9770 } ) ;
9871
9972 it ( 'Set Rate Limit: should handle mismatch between utilize percentages and limit names' , async ( ) => {
100- // Stub the run method to test validation logic
101- const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
102- if ( ! isAuthenticated ( ) ) {
103- const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
104- cliux . print ( err . errorMessage , { color : 'red' } ) ;
105- this . exit ( 1 ) ;
106- return ;
107- }
108- const { flags } = await this . parse ( SetRateLimitCommand ) ;
109- const utilize = flags . utilize ;
110- const limitName = flags [ 'limit-name' ] ;
111- if ( utilize ) {
112- const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
113- if ( limitName ?. length > 0 && limitName [ 0 ] ?. split ( ',' ) ?. length !== utilizeValues . length ) {
114- cliux . error ( 'The number of utilization percentages must match the number of limit names.' ) ;
115- this . exit ( 1 ) ;
116- return ;
117- }
118- }
119- } ) ;
120- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
121- // Stub configHandler.get to make isAuthenticated() return true
122- const originalGet = configHandler . get ;
123- const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
124- if ( key === 'authorisationType' ) return 'OAUTH' ;
125- return originalGet . call ( configHandler , key ) ;
126- } ) ;
73+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
74+
12775 const args = [ '--org' , 'test-org-id' , '--utilize' , '70' , '--limit-name' , 'getLimit,postLimit' ] ;
12876 await SetRateLimitCommand . run ( args ) ;
12977
130- expect ( errorMessage ) . to . equal (
131- 'The number of utilization percentages must match the number of limit names.' ,
132- ) ;
78+ expect ( errorMessage ) . to . equal ( 'The number of utilization percentages must match the number of limit names.' ) ;
79+
13380 expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
134- runStub . restore ( ) ;
81+
82+ // Restore the stub after the test
13583 exitStub . restore ( ) ;
136- configGetStub . restore ( ) ;
13784 } ) ;
13885
13986 it ( 'Set Rate Limit: should handle invalid number of limit names' , async ( ) => {
140- // Stub the run method to test validation logic
141- const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
142- if ( ! isAuthenticated ( ) ) {
143- const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
144- cliux . print ( err . errorMessage , { color : 'red' } ) ;
145- this . exit ( 1 ) ;
146- return ;
147- }
148- const { flags } = await this . parse ( SetRateLimitCommand ) ;
149- const utilize = flags . utilize ;
150- const limitName = flags [ 'limit-name' ] ;
151- if ( utilize ) {
152- const utilizeValues = utilize ?. split ( ',' ) ?. map ( ( u : string ) => Number ( u . trim ( ) ) ) ;
153- if ( limitName ?. length > 0 && limitName [ 0 ] ?. split ( ',' ) ?. length !== utilizeValues . length ) {
154- cliux . error ( 'The number of utilization percentages must match the number of limit names.' ) ;
155- this . exit ( 1 ) ;
156- return ;
157- }
158- }
159- } ) ;
160- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
161- // Stub configHandler.get to make isAuthenticated() return true
162- const originalGet = configHandler . get ;
163- const configGetStub = stub ( configHandler , 'get' ) . callsFake ( ( key ) => {
164- if ( key === 'authorisationType' ) return 'OAUTH' ;
165- return originalGet . call ( configHandler , key ) ;
166- } ) ;
87+ const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
88+
16789 const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit' ] ;
16890 await SetRateLimitCommand . run ( args ) ;
16991
170- expect ( errorMessage ) . to . equal (
171- 'The number of utilization percentages must match the number of limit names.' ,
172- ) ;
92+ expect ( errorMessage ) . to . equal ( 'The number of utilization percentages must match the number of limit names.' ) ;
93+
17394 expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
174- runStub . restore ( ) ;
95+
96+ // Restore the stub after the test
17597 exitStub . restore ( ) ;
176- configGetStub . restore ( ) ;
17798 } ) ;
17899
179100 it ( 'Set Rate Limit: should prompt for the organization UID' , async ( ) => {
@@ -201,17 +122,6 @@ describe('Rate Limit Commands', () => {
201122 } ) ;
202123
203124 it ( 'Set Rate Limit: should handle unauthenticated user' , async ( ) => {
204- // Since isAuthenticated is non-configurable, we'll test by mocking the command's behavior
205- // Instead of stubbing isAuthenticated, we'll stub the entire run method to simulate the unauthenticated case
206- const sandbox = createSandbox ( ) ;
207-
208- // Create a spy on the run method and make it call the unauthenticated path
209- const runStub = sandbox . stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( ) {
210- const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
211- cliux . print ( err . errorMessage , { color : 'red' } ) ;
212- this . exit ( 1 ) ;
213- } ) ;
214-
215125 // Stub the exit method to prevent process exit
216126 const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
217127 const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit,bulkLimit' ] ;
@@ -223,8 +133,7 @@ describe('Rate Limit Commands', () => {
223133 // Ensure exit was called with code 1
224134 expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
225135
226- // Restore
227- sandbox . restore ( ) ;
136+ // Restore the stub
228137 exitStub . restore ( ) ;
229138 } ) ;
230139
@@ -283,28 +192,11 @@ describe('Rate Limit Commands', () => {
283192 } ;
284193
285194 it ( 'Remove Rate Limit: should remove the rate limit for the given organization' , async ( ) => {
286- // Set up rateLimit with default property to match what setRateLimit creates
287- const rateLimitWithDefault = {
288- default : defaultRalteLimitConfig ,
289- ...rateLimit ,
290- } ;
291- configHandler . set ( 'rateLimit' , rateLimitWithDefault ) ;
292- // Stub configHandler.delete to manually remove the org property
293- const originalDelete = configHandler . delete ;
294- const deleteStub = stub ( configHandler , 'delete' ) . callsFake ( ( key : string ) => {
295- if ( key === 'rateLimit.test-org-id' ) {
296- const currentRateLimit = configHandler . get ( 'rateLimit' ) || { } ;
297- delete currentRateLimit [ 'test-org-id' ] ;
298- configHandler . set ( 'rateLimit' , currentRateLimit ) ;
299- return configHandler ;
300- }
301- return originalDelete . call ( configHandler , key ) ;
302- } ) ;
195+ configHandler . set ( 'rateLimit' , rateLimit ) ;
303196 await RemoveRateLimitCommand . run ( [ '--org' , 'test-org-id' ] ) ;
304197 const updatedRateLimit = configHandler . get ( 'rateLimit' ) ;
305198 expect ( updatedRateLimit [ 'test-org-id' ] ) . to . be . undefined ;
306199 expect ( printMessage ) . to . equal ( 'Rate limit entry for organization UID test-org-id has been removed.' ) ;
307- deleteStub . restore ( ) ;
308200 } ) ;
309201
310202 it ( 'Remove Rate Limit: should throw an error if the organization is not found' , async ( ) => {
@@ -316,4 +208,4 @@ describe('Rate Limit Commands', () => {
316208 }
317209 } ) ;
318210 } ) ;
319- } ) ;
211+ } ) ;
0 commit comments