Skip to content

Commit 8bef867

Browse files
committed
fixed the auth error in clone command
1 parent 92baaf2 commit 8bef867

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

packages/contentstack-clone/src/commands/cm/stacks/clone.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ Use this plugin to automate the process of cloning a stack in few steps.
157157
async run(): Promise<void> {
158158
try {
159159
const self = this;
160-
configHandler.set('log.progressSupportedModule', 'clone');
160+
// Clear any stale progressSupportedModule persisted from a previous run so that
161+
// auth/pre-flight errors always reach the console regardless of showConsoleLogs setting.
162+
// It will be re-set inside handleClone() once authentication passes.
163+
configHandler.set('log.progressSupportedModule', null);
161164
const { flags: cloneCommandFlags } = await self.parse(StackCloneCommand);
162165
const {
163166
yes,
@@ -176,6 +179,7 @@ Use this plugin to automate the process of cloning a stack in few steps.
176179
} = cloneCommandFlags;
177180

178181
const handleClone = async (): Promise<void> => {
182+
configHandler.set('log.progressSupportedModule', 'clone');
179183
const listOfTokens = configHandler.get('tokens');
180184
const authenticationMethod = this.determineAuthenticationMethod(
181185
sourceManagementTokenAlias,

packages/contentstack-clone/test/commands/cm/stacks/clone.test.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -538,29 +538,30 @@ describe('StackCloneCommand', () => {
538538
}
539539
});
540540

541-
it.skip('should exit when not authenticated and no management token aliases', async () => {
541+
it('should exit when not authenticated and no management token aliases', async () => {
542542
const parseStub = sandbox.stub(command, 'parse' as any).resolves({
543543
flags: mockFlags,
544544
});
545-
const exitStub = sandbox.stub(command, 'exit' as any).callsFake((() => {
546-
throw new Error('exit called');
547-
}) as () => never);
545+
const configHandlerGetStub = sandbox.stub(cliUtilities.configHandler, 'get').returns(undefined);
546+
const configHandlerSetStub = sandbox.stub(cliUtilities.configHandler, 'set');
547+
const logStub = { error: sandbox.stub(), warn: sandbox.stub(), debug: sandbox.stub(), info: sandbox.stub() };
548+
sandbox.stub(cliUtilities, 'log').value(logStub);
549+
// exit(1) throws inside run()'s own try/catch which swallows it — stub cleanUp to
550+
// prevent real filesystem ops and assert on exitStub directly after run() settles.
551+
const cleanUpStub = sandbox.stub(command, 'cleanUp').resolves();
552+
const exitStub = sandbox.stub(command, 'exit' as any);
548553

549-
// Only test if not authenticated
550-
if (!cliUtilities.isAuthenticated()) {
551-
try {
552-
await command.run();
553-
expect.fail('Should have exited');
554-
} catch (error: any) {
555-
expect(error.message).to.equal('exit called');
556-
}
554+
await command.run();
557555

558-
expect(parseStub.calledOnce).to.be.true;
559-
expect(exitStub.calledOnce).to.be.true;
560-
}
556+
expect(parseStub.calledOnce).to.be.true;
557+
expect(exitStub.calledOnce).to.be.true;
558+
expect(logStub.error.called).to.be.true;
559+
expect(logStub.error.firstCall.args[0]).to.include('Please login');
560+
// progressSupportedModule must NOT be set when auth fails
561+
expect(configHandlerSetStub.calledWith('log.progressSupportedModule', 'clone')).to.be.false;
561562
});
562563

563-
it.skip('should exit when management token aliases provided but not authenticated and branches provided', async () => {
564+
it('should exit when management token aliases provided but not authenticated and branches provided', async () => {
564565
const parseStub = sandbox.stub(command, 'parse' as any).resolves({
565566
flags: {
566567
...mockFlags,
@@ -569,22 +570,21 @@ describe('StackCloneCommand', () => {
569570
'source-branch': 'main',
570571
},
571572
});
572-
const exitStub = sandbox.stub(command, 'exit' as any).callsFake((() => {
573-
throw new Error('exit called');
574-
}) as () => never);
573+
const configHandlerGetStub = sandbox.stub(cliUtilities.configHandler, 'get').returns(undefined);
574+
const configHandlerSetStub = sandbox.stub(cliUtilities.configHandler, 'set');
575+
const logStub = { error: sandbox.stub(), warn: sandbox.stub(), debug: sandbox.stub(), info: sandbox.stub() };
576+
sandbox.stub(cliUtilities, 'log').value(logStub);
577+
const cleanUpStub = sandbox.stub(command, 'cleanUp').resolves();
578+
const exitStub = sandbox.stub(command, 'exit' as any);
575579

576-
// Only test if not authenticated
577-
if (!cliUtilities.isAuthenticated()) {
578-
try {
579-
await command.run();
580-
expect.fail('Should have exited');
581-
} catch (error: any) {
582-
expect(error.message).to.equal('exit called');
583-
}
580+
await command.run();
584581

585-
expect(parseStub.calledOnce).to.be.true;
586-
expect(exitStub.calledOnce).to.be.true;
587-
}
582+
expect(parseStub.calledOnce).to.be.true;
583+
expect(exitStub.calledOnce).to.be.true;
584+
expect(logStub.error.called).to.be.true;
585+
expect(logStub.error.firstCall.args[0]).to.include('Log in');
586+
// progressSupportedModule must NOT be set when auth fails
587+
expect(configHandlerSetStub.calledWith('log.progressSupportedModule', 'clone')).to.be.false;
588588
});
589589

590590
it('should handle run error and cleanup', async () => {
@@ -794,6 +794,7 @@ describe('StackCloneCommand', () => {
794794
'destination-management-token-alias': 'dest-alias',
795795
},
796796
});
797+
const configHandlerSetStub = sandbox.stub(cliUtilities.configHandler, 'set');
797798
const configHandlerStub = sandbox.stub(cliUtilities.configHandler, 'get');
798799
// Stub authorisationType to 'OAUTH' to make isAuthenticated() return true
799800
configHandlerStub.callsFake((key: string) => {
@@ -835,6 +836,8 @@ describe('StackCloneCommand', () => {
835836
expect(cloneHandlerExecuteStub.calledOnce).to.be.true;
836837
// Verify all config flags were set
837838
expect(logStub.debug.called).to.be.true;
839+
// progressSupportedModule must be set inside handleClone (after auth passes)
840+
expect(configHandlerSetStub.calledWith('log.progressSupportedModule', 'clone')).to.be.true;
838841
});
839842

840843
it('should handle CloneHandler.execute error (covers line 263)', async () => {

0 commit comments

Comments
 (0)