11import { expect } from 'chai' ;
22import { stub , restore } from 'sinon' ; // Import restore for cleaning up
3- import { cliux , configHandler , isAuthenticated } from '@contentstack/cli-utilities' ;
3+ import { cliux , configHandler } from '@contentstack/cli-utilities' ;
44import SetRateLimitCommand from '../../../src/commands/config/set/rate-limit' ;
55import GetRateLimitCommand from '../../../src/commands/config/get/rate-limit' ;
66import RemoveRateLimitCommand from '../../../src/commands/config/remove/rate-limit' ;
@@ -11,17 +11,14 @@ import { defaultRalteLimitConfig } from '../../../src/utils/common-utilities';
1111describe ( 'Rate Limit Commands' , ( ) => {
1212 let originalCliuxError : typeof cliux . error ;
1313 let originalCliuxPrint : typeof cliux . print ;
14- let originalIsAuthenticated : ( ) => boolean ;
1514 let errorMessage : any ;
1615 let printMessage : any ;
17- let authenticated = isAuthenticated ;
1816 let rateLimitHandler : RateLimitHandler ;
1917 let mockClient : any ;
2018
2119 beforeEach ( ( ) => {
2220 originalCliuxError = cliux . error ;
2321 originalCliuxPrint = cliux . print ;
24- originalIsAuthenticated = isAuthenticated ;
2522
2623 cliux . error = ( message : string ) => {
2724 errorMessage = message ;
@@ -42,7 +39,6 @@ describe('Rate Limit Commands', () => {
4239 afterEach ( ( ) => {
4340 cliux . error = originalCliuxError ;
4441 cliux . print = originalCliuxPrint ;
45- authenticated = originalIsAuthenticated ;
4642 } ) ;
4743
4844 describe ( 'Set Rate Limit Command' , ( ) => {
@@ -54,49 +50,28 @@ describe('Rate Limit Commands', () => {
5450 } ) ;
5551
5652 it ( 'Set Rate Limit: should handle invalid utilization percentages' , async ( ) => {
57- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
58-
5953 const args = [ '--org' , 'test-org-id' , '--utilize' , '150' , '--limit-name' , 'getLimit' ] ;
6054 await SetRateLimitCommand . run ( args ) ;
6155
6256 expect ( errorMessage ) . to . equal ( 'Utilize percentages must be numbers between 0 and 100.' ) ;
63-
64- expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
65-
66- // Restore the stub after the test
67- exitStub . restore ( ) ;
6857 } ) ;
6958
7059 it ( 'Set Rate Limit: should handle mismatch between utilize percentages and limit names' , async ( ) => {
71- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
72-
7360 const args = [ '--org' , 'test-org-id' , '--utilize' , '70' , '--limit-name' , 'getLimit,postLimit' ] ;
7461 await SetRateLimitCommand . run ( args ) ;
7562
7663 expect ( errorMessage ) . to . equal (
7764 'The number of utilization percentages must match the number of limit names provided.' ,
7865 ) ;
79-
80- expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
81-
82- // Restore the stub after the test
83- exitStub . restore ( ) ;
8466 } ) ;
8567
8668 it ( 'Set Rate Limit: should handle invalid number of limit names' , async ( ) => {
87- const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ; // Stub the exit method
88-
8969 const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit' ] ;
9070 await SetRateLimitCommand . run ( args ) ;
9171
9272 expect ( errorMessage ) . to . equal (
9373 'The number of utilization percentages must match the number of limit names provided.' ,
9474 ) ;
95-
96- expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
97-
98- // Restore the stub after the test
99- exitStub . restore ( ) ;
10075 } ) ;
10176
10277 it ( 'Set Rate Limit: should prompt for the organization UID' , async ( ) => {
@@ -124,21 +99,30 @@ describe('Rate Limit Commands', () => {
12499 } ) ;
125100
126101 it ( 'Set Rate Limit: should handle unauthenticated user' , async ( ) => {
127- const isAuthenticatedStub = stub ( ) . returns ( false ) ;
128- authenticated = isAuthenticatedStub ;
129102 // Stub the exit method to prevent process exit
130103 const exitStub = stub ( SetRateLimitCommand . prototype , 'exit' ) ;
131- const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit,bulkLimit' ] ;
132- await SetRateLimitCommand . run ( args ) ;
133-
134- // Assert that the correct error message was printed
135- expect ( printMessage ) . to . equal ( 'You are not logged in. Please login with command $ csdx auth:login' ) ;
104+
105+ // Stub the run method to simulate unauthenticated behavior
106+ const runStub = stub ( SetRateLimitCommand . prototype , 'run' ) . callsFake ( async function ( this : any ) {
107+ // Simulate the unauthenticated check
108+ const err = { errorMessage : 'You are not logged in. Please login with command $ csdx auth:login' } ;
109+ cliux . print ( err . errorMessage , { color : 'red' } ) ;
110+ this . exit ( 1 ) ;
111+ } ) ;
112+
113+ try {
114+ const args = [ '--org' , 'test-org-id' , '--utilize' , '70,80' , '--limit-name' , 'getLimit,bulkLimit' ] ;
115+ await SetRateLimitCommand . run ( args ) ;
136116
137- // Ensure exit was called with code 1
138- expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
117+ // Assert that the correct error message was printed
118+ expect ( printMessage ) . to . equal ( 'You are not logged in. Please login with command $ csdx auth:login' ) ;
139119
140- // Restore the stub
141- exitStub . restore ( ) ;
120+ // Ensure exit was called with code 1
121+ expect ( exitStub . calledWith ( 1 ) ) . to . be . true ;
122+ } finally {
123+ exitStub . restore ( ) ;
124+ runStub . restore ( ) ;
125+ }
142126 } ) ;
143127
144128 it ( 'should set default rate limit for organization' , async ( ) => {
0 commit comments