Skip to content

Commit f539bfb

Browse files
committed
Fixed the test cases
1 parent c2b4723 commit f539bfb

6 files changed

Lines changed: 46 additions & 47 deletions

File tree

.talismanrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ fileignoreconfig:
4141
checksum: 3c9e7e43bebec8683b4af2ed2b28ed71c24cf0e7e7379aa7acceee620d2cc310
4242
- filename: packages/contentstack-config/test/unit/services/totp.service.test.ts
4343
checksum: 68a9e64134c5972828d6c1e65d6975b6200f36a0edab3ee6e4df05445a52f941
44-
version: "1.0"
44+
- filename: packages/contentstack-config/src/services/totp/totp.service.ts
45+
checksum: 63aeb5dc49a60c195a5cfcefd52aeb276f0bb5227152d44c7d6305b0dbf7bee5
46+
- filename: packages/contentstack-config/src/commands/config/totp/remove.ts
47+
checksum: 17f6576f90d3b30f37c5ef1318b188db2c70ab31e7fdedc3c94807d1eb822a8b
48+
version: "1.0"
49+

packages/contentstack-config/src/commands/config/totp/add.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ export default class AddTOTPCommand extends BaseCommand<typeof AddTOTPCommand> {
3030
const { flags } = await this.parse(AddTOTPCommand);
3131
let secret = flags.secret;
3232

33-
// Validate and normalize secret
3433
if (!secret) {
3534
secret = await cliux.inquire({
3635
type: 'password',
3736
name: 'secret',
38-
message: 'Enter your TOTP secret:',
37+
message: 'Enter your secret:',
3938
validate: (input: string) => {
40-
if (!input) return 'TOTP secret is required';
39+
if (!input) return 'Secret is required';
4140
if (!this.totpService.validateSecret(input)) return 'Invalid TOTP secret format';
4241
return true;
4342
},
@@ -55,7 +54,7 @@ export default class AddTOTPCommand extends BaseCommand<typeof AddTOTPCommand> {
5554
const confirm = await cliux.inquire({
5655
type: 'confirm',
5756
name: 'confirm',
58-
message: 'TOTP configuration already exists. Do you want to overwrite it?',
57+
message: 'Secret configuration already exists. Do you want to overwrite it?',
5958
});
6059

6160
if (!confirm) {
@@ -68,18 +67,18 @@ export default class AddTOTPCommand extends BaseCommand<typeof AddTOTPCommand> {
6867
try {
6968
const encryptedSecret = this.totpService.encryptSecret(secret);
7069
this.totpService.storeConfig({ secret: encryptedSecret });
71-
cliux.success('TOTP secret has been stored successfully');
70+
cliux.success('Secret has been stored successfully');
7271
} catch (error) {
7372
if (error instanceof TOTPError) {
7473
throw error;
7574
}
76-
throw new TOTPError('Failed to store TOTP secret');
75+
throw new TOTPError('Failed to store configuration');
7776
}
7877
} catch (error) {
7978
if (error instanceof TOTPError) {
8079
cliux.error(error.message);
8180
} else {
82-
cliux.error('Failed to store TOTP secret');
81+
cliux.error('Failed to store configuration');
8382
}
8483
throw error;
8584
}

packages/contentstack-config/src/commands/config/totp/remove.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TOTPError } from '../../../services/totp/types';
55
import { Flags } from '@oclif/core';
66

77
export default class RemoveTOTPCommand extends BaseCommand<typeof RemoveTOTPCommand> {
8-
static readonly description = 'Remove stored TOTP secret';
8+
static readonly description = 'Remove stored secret';
99

1010
static readonly examples = [
1111
'$ csdx config:totp:remove',
@@ -31,32 +31,31 @@ export default class RemoveTOTPCommand extends BaseCommand<typeof RemoveTOTPComm
3131
try {
3232
const { flags } = await this.parse(RemoveTOTPCommand);
3333

34-
// Check if TOTP configuration exists
3534
let config;
3635
try {
3736
config = this.totpService.getStoredConfig();
3837
if (!config?.secret) {
39-
throw new TOTPError('Failed to remove TOTP configuration');
38+
throw new TOTPError('Failed to remove configuration');
4039
}
4140
} catch (error) {
4241
if (error instanceof TOTPError) {
4342
throw error;
4443
}
45-
throw new TOTPError('Failed to remove TOTP configuration');
44+
throw new TOTPError('Failed to remove configuration');
4645
}
4746

4847
// Verify the configuration is valid
4948
let isCorrupted = false;
5049
try {
5150
this.totpService.decryptSecret(config.secret);
5251
} catch (error) {
53-
this.logger.debug('Failed to decrypt TOTP secret', { error });
52+
this.logger.debug('Failed to decrypt secret', { error });
5453
isCorrupted = true;
5554
}
5655

5756
// Confirm removal unless -y flag is used
5857
if (!flags.yes) {
59-
let message = 'Are you sure you want to remove the stored TOTP secret?';
58+
let message = 'Are you sure you want to remove the stored secret?';
6059
if (isCorrupted) {
6160
message = 'Configuration appears corrupted. Do you want to remove it anyway?';
6261
}
@@ -75,16 +74,16 @@ export default class RemoveTOTPCommand extends BaseCommand<typeof RemoveTOTPComm
7574

7675
try {
7776
this.totpService.removeConfig();
78-
cliux.success('TOTP secret has been removed successfully');
77+
cliux.success('Secret has been removed successfully');
7978
} catch (error) {
80-
this.logger.error('Failed to remove TOTP configuration', { error });
81-
throw new TOTPError('Failed to remove TOTP configuration');
79+
this.logger.error('Failed to remove secret configuration', { error });
80+
throw new TOTPError('Failed to remove configuration');
8281
}
8382
} catch (error) {
8483
if (error instanceof TOTPError) {
8584
cliux.error(error.message);
8685
} else {
87-
cliux.error('Failed to remove TOTP configuration');
86+
cliux.error('Failed to remove configuration');
8887
}
8988
throw error;
9089
}

packages/contentstack-config/src/services/totp/totp.service.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ export class TOTPService implements ITOTPService {
1616
return false;
1717
}
1818

19-
// Check for leading/trailing spaces in original input
2019
if (secret.trim() !== secret) {
2120
return false;
2221
}
2322

24-
// Base32 validation (A-Z, 2-7)
2523
const base32Regex = /^[A-Z2-7]+=*$/;
2624
const normalizedSecret = secret.trim().toUpperCase();
2725

@@ -30,7 +28,6 @@ export class TOTPService implements ITOTPService {
3028
return false;
3129
}
3230

33-
// Check for invalid padding
3431
const paddingRegex = /=+$/;
3532
const paddingMatch = paddingRegex.exec(normalizedSecret);
3633
if (paddingMatch) {
@@ -40,7 +37,6 @@ export class TOTPService implements ITOTPService {
4037
return false;
4138
}
4239
} else if (normalizedSecret.length % 8 !== 0) {
43-
// If no padding, length must be a multiple of 8
4440
return false;
4541
}
4642

@@ -58,7 +54,7 @@ export class TOTPService implements ITOTPService {
5854
return this.encrypter.encrypt(secret.trim().toUpperCase());
5955
} catch (error) {
6056
this.logger.error('Secret encryption failed', { error });
61-
throw new TOTPError('Failed to encrypt TOTP secret');
57+
throw new TOTPError('Failed to encrypt secret');
6258
}
6359
}
6460

@@ -67,7 +63,7 @@ export class TOTPService implements ITOTPService {
6763
return this.encrypter.decrypt(encryptedSecret);
6864
} catch (error) {
6965
this.logger.error('Secret decryption failed', { error });
70-
throw new TOTPError('Failed to decrypt TOTP secret');
66+
throw new TOTPError('Failed to decrypt secret');
7167
}
7268
}
7369

@@ -76,8 +72,8 @@ export class TOTPService implements ITOTPService {
7672
const config = configHandler.get('totp');
7773
return config?.secret ? config as TOTPConfig : null;
7874
} catch (error) {
79-
this.logger.error('Failed to read TOTP config', { error });
80-
throw new TOTPError('Failed to read TOTP configuration');
75+
this.logger.error('Failed to read config', { error });
76+
throw new TOTPError('Failed to read configuration');
8177
}
8278
}
8379

@@ -90,34 +86,34 @@ export class TOTPService implements ITOTPService {
9086
};
9187
configHandler.set('totp', updatedConfig);
9288
} catch (error) {
93-
this.logger.error('Failed to store TOTP config', { error });
94-
throw new TOTPError('Failed to store TOTP configuration');
89+
this.logger.error('Failed to store config', { error });
90+
throw new TOTPError('Failed to store configuration');
9591
}
9692
}
9793

9894
removeConfig(): void {
9995
try {
10096
configHandler.delete('totp');
10197
} catch (error) {
102-
this.logger.error('Failed to remove TOTP config', { error });
103-
throw new TOTPError('Failed to remove TOTP configuration');
98+
this.logger.error('Failed to remove config', { error });
99+
throw new TOTPError('Failed to remove configuration');
104100
}
105101
}
106102

107103
generateTOTP(secret: string): string {
108104
try {
109105
return authenticator.generate(secret.trim().toUpperCase());
110106
} catch (error) {
111-
this.logger.error('Failed to generate TOTP code', { error });
112-
throw new TOTPError('Failed to generate TOTP code');
107+
this.logger.error('Failed to generate code', { error });
108+
throw new TOTPError('Failed to generate code');
113109
}
114110
}
115111

116112
verifyTOTP(secret: string, token: string): boolean {
117113
try {
118114
return authenticator.check(token, secret.trim().toUpperCase());
119115
} catch (error) {
120-
this.logger.debug('TOTP verification failed', { error });
116+
this.logger.debug('Secret verification failed', { error });
121117
return false;
122118
}
123119
}

packages/contentstack-config/test/unit/commands/totp.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('TOTP Commands', function () {
124124
expect.fail('Should have thrown an error');
125125
} catch (error: unknown) {
126126
const err = error as Error;
127-
expect(err.message).to.contain('Failed to store TOTP secret');
127+
expect(err.message).to.contain('Failed to store configuration');
128128
}
129129
});
130130

@@ -223,7 +223,7 @@ describe('TOTP Commands', function () {
223223
expect.fail('Should have thrown an error');
224224
} catch (error: unknown) {
225225
const err = error as Error;
226-
expect(err.message).to.equal('Failed to remove TOTP configuration');
226+
expect(err.message).to.equal('Failed to remove configuration');
227227
expect(configStub.delete.called).to.be.false;
228228
}
229229
});
@@ -261,7 +261,7 @@ describe('TOTP Commands', function () {
261261
expect.fail('Should have thrown an error');
262262
} catch (error: unknown) {
263263
const err = error as Error;
264-
expect(err.message).to.contain('Failed to remove TOTP configuration');
264+
expect(err.message).to.contain('Failed to remove configuration');
265265
}
266266
});
267267

@@ -272,7 +272,7 @@ describe('TOTP Commands', function () {
272272
expect.fail('Should have thrown an error');
273273
} catch (error: unknown) {
274274
const err = error as Error;
275-
expect(err.message).to.contain('Failed to remove TOTP configuration');
275+
expect(err.message).to.contain('Failed to remove configuration');
276276
}
277277
});
278278

@@ -283,7 +283,7 @@ describe('TOTP Commands', function () {
283283
expect.fail('Should have thrown an error');
284284
} catch (error: unknown) {
285285
const err = error as Error;
286-
expect(err.message).to.contain('Failed to remove TOTP configuration');
286+
expect(err.message).to.contain('Failed to remove configuration');
287287
}
288288
});
289289

@@ -294,7 +294,7 @@ describe('TOTP Commands', function () {
294294
expect.fail('Should have thrown an error');
295295
} catch (error: unknown) {
296296
const err = error as Error;
297-
expect(err.message).to.contain('Failed to remove TOTP configuration');
297+
expect(err.message).to.contain('Failed to remove configuration');
298298
}
299299
});
300300

@@ -305,7 +305,7 @@ describe('TOTP Commands', function () {
305305
expect.fail('Should have thrown an error');
306306
} catch (error: unknown) {
307307
const err = error as Error;
308-
expect(err.message).to.contain('Failed to remove TOTP configuration');
308+
expect(err.message).to.contain('Failed to remove configuration');
309309
}
310310
});
311311

@@ -337,7 +337,7 @@ describe('TOTP Commands', function () {
337337
expect.fail('Should have thrown an error');
338338
} catch (error: unknown) {
339339
const err = error as Error;
340-
expect(err.message).to.contain('Failed to remove TOTP configuration');
340+
expect(err.message).to.contain('Failed to remove configuration');
341341
}
342342
});
343343

packages/contentstack-config/test/unit/services/totp.service.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('TOTP Service', () => {
9292
error = err;
9393
}
9494
expect(error).to.be.an('error').with.property('name', 'TOTPError');
95-
expect((error as TOTPError).message).to.equal('Failed to encrypt TOTP secret');
95+
expect((error as TOTPError).message).to.equal('Failed to encrypt secret');
9696
});
9797

9898
it('should normalize secret before encryption', () => {
@@ -123,7 +123,7 @@ describe('TOTP Service', () => {
123123
error = err;
124124
}
125125
expect(error).to.be.an('error').with.property('name', 'TOTPError');
126-
expect((error as TOTPError).message).to.equal('Failed to decrypt TOTP secret');
126+
expect((error as TOTPError).message).to.equal('Failed to decrypt secret');
127127
});
128128
});
129129

@@ -154,7 +154,7 @@ describe('TOTP Service', () => {
154154
error = err;
155155
}
156156
expect(error).to.be.an('error').with.property('name', 'TOTPError');
157-
expect((error as TOTPError).message).to.equal('Failed to read TOTP configuration');
157+
expect((error as TOTPError).message).to.equal('Failed to read configuration');
158158
});
159159
});
160160

@@ -193,7 +193,7 @@ describe('TOTP Service', () => {
193193
error = err;
194194
}
195195
expect(error).to.be.an('error').with.property('name', 'TOTPError');
196-
expect((error as TOTPError).message).to.equal('Failed to store TOTP configuration');
196+
expect((error as TOTPError).message).to.equal('Failed to store configuration');
197197
});
198198
});
199199

@@ -213,7 +213,7 @@ describe('TOTP Service', () => {
213213
error = err;
214214
}
215215
expect(error).to.be.an('error').with.property('name', 'TOTPError');
216-
expect((error as TOTPError).message).to.equal('Failed to remove TOTP configuration');
216+
expect((error as TOTPError).message).to.equal('Failed to remove configuration');
217217
});
218218
});
219219

@@ -237,7 +237,7 @@ describe('TOTP Service', () => {
237237
error = err;
238238
}
239239
expect(error).to.be.an('error').with.property('name', 'TOTPError');
240-
expect((error as TOTPError).message).to.equal('Failed to generate TOTP code');
240+
expect((error as TOTPError).message).to.equal('Failed to generate code');
241241
});
242242

243243
it('should normalize secret before generation', () => {

0 commit comments

Comments
 (0)