Skip to content

Commit 7ecdfac

Browse files
author
naman-contentstack
committed
updated import test cases
1 parent f973439 commit 7ecdfac

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fileignoreconfig:
110110
- filename: packages/contentstack-audit/src/modules/content-types.ts
111111
checksum: ddf7b08e6a80af09c6a7019a637c26089fb76572c7c3d079a8af244b02985f16
112112
- filename: packages/contentstack-import/test/unit/commands/cm/stacks/import.test.ts
113-
checksum: b11e57f1b824d405f86438e9e7c59183f8c59b66b42d8d16dbeaf76195a30548
113+
checksum: ead3c34bad34f912d8663599273d26a95bb48220e16d9e9e3d33f5c064a487a5
114114
- filename: packages/contentstack-import/test/unit/utils/asset-helper.test.ts
115115
checksum: 8e83200ac8028f9289ff1bd3a50d191b35c8e28f1854141c90fa1b0134d6bf8a
116116
- filename: packages/contentstack-import/test/unit/import/modules/marketplace-apps.test.ts

packages/contentstack-import/test/unit/commands/cm/stacks/import.test.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22
import { fancy } from 'fancy-test';
33
import sinon from 'sinon';
4-
import { managementSDKClient, configHandler, log, handleAndLogError, getLogPath } from '@contentstack/cli-utilities';
4+
import { managementSDKClient, configHandler, log, handleAndLogError, getLogPath, createLogContext } from '@contentstack/cli-utilities';
55
import ImportCommand from '../../../../../src/commands/cm/stacks/import';
66
import { ModuleImporter } from '../../../../../src/import';
77
import { ImportConfig } from '../../../../../src/types';
@@ -201,18 +201,27 @@ describe('ImportCommand', () => {
201201
});
202202
});
203203

204-
describe('createImportContext', () => {
204+
describe('createLogContext', () => {
205205
let configHandlerStub: sinon.SinonStub;
206+
let configHandlerSetStub: sinon.SinonStub;
206207

207208
beforeEach(() => {
208209
configHandlerStub = sinon.stub(configHandler, 'get');
209-
configHandlerStub.withArgs('userUid').returns('user-123');
210+
configHandlerSetStub = sinon.stub(configHandler, 'set');
211+
configHandlerStub.withArgs('clientId').returns('user-123');
210212
configHandlerStub.withArgs('email').returns('test@example.com');
213+
configHandlerStub.withArgs('sessionId').returns('test-session-123');
211214
configHandlerStub.withArgs('oauthOrgUid').returns('org-123');
215+
configHandlerStub.withArgs('authorisationType').returns('BASIC');
216+
});
217+
218+
afterEach(() => {
219+
configHandlerStub.restore();
220+
configHandlerSetStub.restore();
212221
});
213222

214223
it('should create context with all required properties', () => {
215-
const context = command['createImportContext']('test', 'Basic Auth');
224+
const context = createLogContext('cm:stacks:import', 'test', 'Basic Auth');
216225

217226
expect(context).to.have.property('command', 'cm:stacks:import');
218227
expect(context).to.have.property('module', '');
@@ -222,10 +231,11 @@ describe('ImportCommand', () => {
222231
expect(context).to.have.property('apiKey', 'test');
223232
expect(context).to.have.property('orgId', 'org-123');
224233
expect(context).to.have.property('authenticationMethod', 'Basic Auth');
234+
expect(configHandlerSetStub.calledWith('apiKey', 'test')).to.be.true;
225235
});
226236

227237
it('should use default authentication method when not provided', () => {
228-
const context = command['createImportContext']('test');
238+
const context = createLogContext('cm:stacks:import', 'test');
229239

230240
expect(context.authenticationMethod).to.equal('Basic Auth');
231241
});
@@ -234,22 +244,22 @@ describe('ImportCommand', () => {
234244
configHandlerStub.reset();
235245
configHandlerStub.returns(undefined);
236246

237-
const context = command['createImportContext']('test', 'Management Token');
247+
const context = createLogContext('cm:stacks:import', 'test', 'Management Token');
238248

239249
expect(context.userId).to.equal('');
240250
expect(context.email).to.equal('');
241251
expect(context.orgId).to.equal('');
242252
expect(context.authenticationMethod).to.equal('Management Token');
243253
});
244254

245-
it('should use context command when available', () => {
246-
const context = command['createImportContext']('test');
255+
it('should use provided command', () => {
256+
const context = createLogContext('cm:stacks:import', 'test');
247257

248258
expect(context.command).to.equal('cm:stacks:import');
249259
});
250260

251261
it('should handle empty apiKey', () => {
252-
const context = command['createImportContext']('');
262+
const context = createLogContext('cm:stacks:import', '');
253263

254264
expect(context.apiKey).to.equal('');
255265
});
@@ -503,35 +513,43 @@ describe('ImportCommand', () => {
503513
configHandlerStub = sinon.stub(configHandler, 'get');
504514
});
505515

506-
it('should handle undefined context', () => {
507-
(command as any).context = undefined;
516+
afterEach(() => {
517+
configHandlerStub.restore();
518+
});
519+
520+
it('should handle command string directly', () => {
521+
configHandlerStub.returns(undefined);
508522

509-
const context = command['createImportContext']('test');
523+
const context = createLogContext('cm:stacks:import', 'test');
510524

511525
expect(context.command).to.equal('cm:stacks:import');
512526
});
513527

514-
it('should handle context without info', () => {
515-
(command as any).context = { sessionId: 'test-session' };
528+
it('should use command string when provided', () => {
529+
configHandlerStub.withArgs('clientId').returns('user-123');
530+
configHandlerStub.withArgs('email').returns('test@example.com');
531+
configHandlerStub.withArgs('sessionId').returns('test-session');
532+
configHandlerStub.withArgs('oauthOrgUid').returns('org-123');
533+
configHandlerStub.withArgs('authorisationType').returns('BASIC');
516534

517-
const context = command['createImportContext']('test');
535+
const context = createLogContext('cm:stacks:import', 'test');
518536

519537
expect(context.command).to.equal('cm:stacks:import');
538+
expect(context.sessionId).to.equal('test-session');
520539
});
521540

522-
it('should handle context without sessionId', () => {
523-
(command as any).context = { info: { command: 'test' } };
541+
it('should handle missing sessionId from configHandler', () => {
542+
configHandlerStub.returns(undefined);
524543

525-
const context = command['createImportContext']('test');
544+
const context = createLogContext('cm:stacks:import', 'test');
526545

527-
expect(context.sessionId).to.be.undefined;
546+
expect(context.sessionId).to.equal('');
528547
});
529548

530-
it('should handle configHandler throwing errors', () => {
531-
configHandlerStub.reset();
549+
it('should handle configHandler returning undefined values', () => {
532550
configHandlerStub.returns(undefined);
533551

534-
const context = command['createImportContext']('test');
552+
const context = createLogContext('cm:stacks:import', 'test');
535553

536554
expect(context.userId).to.equal('');
537555
expect(context.email).to.equal('');

0 commit comments

Comments
 (0)