Skip to content

Commit fe3c538

Browse files
author
naman-contentstack
committed
[DX-2346], fixed failing test cases
1 parent 3fcc8be commit fe3c538

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

packages/contentstack-config/test/unit/commands/rate-limit.test.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,49 @@ describe('Rate Limit Commands', () => {
5656
});
5757

5858
it('Set Rate Limit: should handle invalid utilization percentages', async () => {
59+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit'); // Stub the exit method
60+
5961
const args = ['--org', 'test-org-id', '--utilize', '150', '--limit-name', 'getLimit'];
6062
await SetRateLimitCommand.run(args);
63+
6164
expect(errorMessage).to.equal('Utilize percentages must be numbers between 0 and 100.');
65+
66+
expect(exitStub.calledWith(1)).to.be.true;
67+
68+
// Restore the stub after the test
69+
exitStub.restore();
6270
});
6371

6472
it('Set Rate Limit: should handle mismatch between utilize percentages and limit names', async () => {
73+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit'); // Stub the exit method
74+
6575
const args = ['--org', 'test-org-id', '--utilize', '70', '--limit-name', 'getLimit,postLimit'];
6676
await SetRateLimitCommand.run(args);
77+
6778
expect(errorMessage).to.equal(
6879
'The number of utilization percentages must match the number of limit names provided.',
6980
);
81+
82+
expect(exitStub.calledWith(1)).to.be.true;
83+
84+
// Restore the stub after the test
85+
exitStub.restore();
7086
});
7187

7288
it('Set Rate Limit: should handle invalid number of limit names', async () => {
89+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit'); // Stub the exit method
90+
7391
const args = ['--org', 'test-org-id', '--utilize', '70,80', '--limit-name', 'getLimit'];
7492
await SetRateLimitCommand.run(args);
93+
7594
expect(errorMessage).to.equal(
7695
'The number of utilization percentages must match the number of limit names provided.',
7796
);
97+
98+
expect(exitStub.calledWith(1)).to.be.true;
99+
100+
// Restore the stub after the test
101+
exitStub.restore();
78102
});
79103

80104
it('Set Rate Limit: should prompt for the organization UID', async () => {
@@ -104,14 +128,21 @@ describe('Rate Limit Commands', () => {
104128
it('Set Rate Limit: should handle unauthenticated user', async () => {
105129
const isAuthenticatedStub = stub().returns(false);
106130
authenticated = isAuthenticatedStub;
131+
// Stub the exit method to prevent process exit
132+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit');
107133
const args = ['--org', 'test-org-id', '--utilize', '70,80', '--limit-name', 'getLimit,bulkLimit'];
108-
try {
109-
await SetRateLimitCommand.run(args);
110-
} catch (error) {
111-
expect(errorMessage).to.equal('You are not logged in. Please login with command $ csdx auth:login');
112-
expect(error?.code).to.equal(1);
113-
}
134+
await SetRateLimitCommand.run(args);
135+
136+
// Assert that the correct error message was printed
137+
expect(printMessage).to.equal('You are not logged in. Please login with command $ csdx auth:login');
138+
139+
// Ensure exit was called with code 1
140+
expect(exitStub.calledWith(1)).to.be.true;
141+
142+
// Restore the stub
143+
exitStub.restore();
114144
});
145+
115146
it('should set default rate limit for organization', async () => {
116147
const config = { org: 'test-org-id', default: true };
117148
await rateLimitHandler.setRateLimit(config);

0 commit comments

Comments
 (0)