From 95ac2efbaf8070cf065e046c0940f63f88b9c773 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 27 Apr 2021 12:17:00 +0200 Subject: [PATCH] fix(shell-api): do not forward auth errors on completion MONGOSH-719 --- packages/cli-repl/src/cli-repl.spec.ts | 18 ++++++++++++++++++ packages/shell-api/src/shell-internal-state.ts | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 93ddb715c9..addf0423d1 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -766,6 +766,15 @@ describe('CliRepl', () => { }); }); + context('with an auth-required mongod', () => { + verifyAutocompletion({ + testServer: startTestServer('not-shared', '--auth'), + wantWatch: false, + wantShardDistribution: false, + hasCollectionNames: false + }); + }); + function verifyAutocompletion({ testServer, wantWatch, wantShardDistribution, hasCollectionNames }: { testServer: MongodSetup | null, wantWatch: boolean, @@ -786,6 +795,8 @@ describe('CliRepl', () => { afterEach(async() => { await cliRepl.mongoshRepl.close(); + expect(output).not.to.include('Tab completion error'); + expect(output).not.to.include('listCollections requires authentication'); }); it(`${wantWatch ? 'completes' : 'does not complete'} the watch method`, async() => { @@ -799,6 +810,13 @@ describe('CliRepl', () => { } }); + it('completes the db.version method', async() => { + output = ''; + input.write('db.vers\u0009\u0009'); + await waitCompletion(cliRepl.bus); + expect(output).to.include('db.version'); + }); + it(`${wantShardDistribution ? 'completes' : 'does not complete'} the getShardDistribution method`, async() => { output = ''; input.write('db.coll.getShardDis\u0009\u0009'); diff --git a/packages/shell-api/src/shell-internal-state.ts b/packages/shell-api/src/shell-internal-state.ts index def6525fa6..0f12e094e7 100644 --- a/packages/shell-api/src/shell-internal-state.ts +++ b/packages/shell-api/src/shell-internal-state.ts @@ -308,7 +308,7 @@ export default class ShellInternalState { const collectionNames = await this.currentDb._getCollectionNamesForCompletion(); return collectionNames.filter((name) => name.startsWith(collName)); } catch (err) { - if (err.code === ShellApiErrors.NotConnected) { + if (err.code === ShellApiErrors.NotConnected || err.codeName === 'Unauthorized') { return []; } throw err;