From 1dea80eb3823469e5f8645b857943f1ca2743481 Mon Sep 17 00:00:00 2001 From: Brendan Burns <5751682+brendandburns@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:59:19 +0000 Subject: [PATCH] Improve exec_auth_test coverage --- src/exec_auth_test.ts | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/exec_auth_test.ts b/src/exec_auth_test.ts index dfdcfdad02..1d8ca77baa 100644 --- a/src/exec_auth_test.ts +++ b/src/exec_auth_test.ts @@ -233,6 +233,49 @@ describe('ExecAuth', () => { strictEqual(opts.headers.Authorization, undefined); }); + it('should handle returned errors correctly', async () => { + const auth = new ExecAuth(); + (auth as any).execFn = ( + command: string, + args?: readonly string[], + options?: child_process.SpawnOptionsWithoutStdio, + ): child_process.ChildProcessWithoutNullStreams => { + return { + stdout: { + setEncoding: () => {}, + on: () => {}, + }, + stderr: { + setEncoding: () => {}, + on: () => {}, + }, + on: (op: string, f: any) => { + if (op === 'error') { + f(new Error('Some error!')); + } + if (op === 'close') { + f(1); + } + }, + } as unknown as child_process.ChildProcessWithoutNullStreams; + }; + const user = { + name: 'user', + authProvider: { + config: { + exec: { + command: '/path/to/bin', + }, + }, + }, + }; + const opts = {} as https.RequestOptions; + opts.headers = {} as OutgoingHttpHeaders; + + const promise = auth.applyAuthentication(user, opts); + await rejects(promise, { name: 'Error' }); + }); + it('should throw on spawnSync errors', async () => { // TODO: fix this test for Windows if (process.platform === 'win32') { @@ -444,4 +487,54 @@ describe('ExecAuth', () => { ); strictEqual(opts.headers?.Authorization, 'Bearer foo'); }); + it('should handle null credentials correctly', async () => { + const auth = new ExecAuth(); + // Fake out Typescript + const res = (auth as any).getToken(null); + strictEqual(res, null); + }); + + it('should handle parse errors correctly', async () => { + const auth = new ExecAuth(); + (auth as any).execFn = ( + command: string, + args?: readonly string[], + options?: child_process.SpawnOptionsWithoutStdio, + ): child_process.ChildProcessWithoutNullStreams => { + return { + stdout: { + setEncoding: () => {}, + on: () => {}, + }, + stderr: { + setEncoding: () => {}, + on: () => {}, + }, + on: (op: string, f: any) => { + if (op === 'data') { + // Invalid JSON + f('foo'); + } + if (op === 'close') { + f(0); + } + }, + } as unknown as child_process.ChildProcessWithoutNullStreams; + }; + const user = { + name: 'user', + authProvider: { + config: { + exec: { + command: '/path/to/bin', + }, + }, + }, + }; + const opts = {} as https.RequestOptions; + opts.headers = {} as OutgoingHttpHeaders; + + const promise = auth.applyAuthentication(user, opts); + await rejects(promise, { name: 'SyntaxError' }); + }); });