Skip to content

Commit 3f52bed

Browse files
committed
fix: test cases
1 parent ec3133f commit 3f52bed

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,49 @@ describe('Rate Limit Commands', () => {
5454
});
5555

5656
it('Set Rate Limit: should handle invalid utilization percentages', async () => {
57+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit'); // Stub the exit method
58+
5759
const args = ['--org', 'test-org-id', '--utilize', '150', '--limit-name', 'getLimit'];
58-
try {
59-
await SetRateLimitCommand.run(args);
60-
expect.fail('Expected command to exit with error');
61-
} catch (error) {
62-
expect(error.oclif.exit).to.equal(1);
63-
}
60+
await SetRateLimitCommand.run(args);
61+
62+
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();
6468
});
6569

6670
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+
6773
const args = ['--org', 'test-org-id', '--utilize', '70', '--limit-name', 'getLimit,postLimit'];
68-
try {
69-
await SetRateLimitCommand.run(args);
70-
expect.fail('Expected command to exit with error');
71-
} catch (error) {
72-
expect(error.oclif.exit).to.equal(1);
73-
}
74+
await SetRateLimitCommand.run(args);
75+
76+
expect(errorMessage).to.equal(
77+
'The number of utilization percentages must match the number of limit names provided.',
78+
);
79+
80+
expect(exitStub.calledWith(1)).to.be.true;
81+
82+
// Restore the stub after the test
83+
exitStub.restore();
7484
});
7585

7686
it('Set Rate Limit: should handle invalid number of limit names', async () => {
87+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit'); // Stub the exit method
88+
7789
const args = ['--org', 'test-org-id', '--utilize', '70,80', '--limit-name', 'getLimit'];
78-
try {
79-
await SetRateLimitCommand.run(args);
80-
expect.fail('Expected command to exit with error');
81-
} catch (error) {
82-
expect(error.oclif.exit).to.equal(1);
83-
}
90+
await SetRateLimitCommand.run(args);
91+
92+
expect(errorMessage).to.equal(
93+
'The number of utilization percentages must match the number of limit names provided.',
94+
);
95+
96+
expect(exitStub.calledWith(1)).to.be.true;
97+
98+
// Restore the stub after the test
99+
exitStub.restore();
84100
});
85101

86102
it('Set Rate Limit: should prompt for the organization UID', async () => {
@@ -107,17 +123,22 @@ describe('Rate Limit Commands', () => {
107123
}
108124
});
109125

110-
it.skip('Set Rate Limit: should handle unauthenticated user', async () => {
111-
// Mock isAuthenticated to return false
126+
it('Set Rate Limit: should handle unauthenticated user', async () => {
112127
const isAuthenticatedStub = stub().returns(false);
113-
Object.defineProperty(require('@contentstack/cli-utilities'), 'isAuthenticated', {
114-
value: isAuthenticatedStub,
115-
writable: true,
116-
});
117-
128+
authenticated = isAuthenticatedStub;
129+
// Stub the exit method to prevent process exit
130+
const exitStub = stub(SetRateLimitCommand.prototype, 'exit');
118131
const args = ['--org', 'test-org-id', '--utilize', '70,80', '--limit-name', 'getLimit,bulkLimit'];
119132
await SetRateLimitCommand.run(args);
133+
134+
// Assert that the correct error message was printed
120135
expect(printMessage).to.equal('You are not logged in. Please login with command $ csdx auth:login');
136+
137+
// Ensure exit was called with code 1
138+
expect(exitStub.calledWith(1)).to.be.true;
139+
140+
// Restore the stub
141+
exitStub.restore();
121142
});
122143

123144
it('should set default rate limit for organization', async () => {
@@ -179,6 +200,7 @@ describe('Rate Limit Commands', () => {
179200
await RemoveRateLimitCommand.run(['--org', 'test-org-id']);
180201
const updatedRateLimit = configHandler.get('rateLimit');
181202
expect(updatedRateLimit['test-org-id']).to.be.undefined;
203+
expect(printMessage).to.equal('Rate limit entry for organization UID test-org-id has been removed.');
182204
});
183205

184206
it('Remove Rate Limit: should throw an error if the organization is not found', async () => {

0 commit comments

Comments
 (0)