diff --git a/plugins/auth-basic/package.json b/plugins/auth-basic/package.json index 6debc8e7..89affc22 100644 --- a/plugins/auth-basic/package.json +++ b/plugins/auth-basic/package.json @@ -11,6 +11,7 @@ "version": "0.1.0", "scripts": { "build": "yaakcli build", - "dev": "yaakcli dev" + "dev": "yaakcli dev", + "test": "vitest --run tests" } } diff --git a/plugins/auth-basic/src/index.ts b/plugins/auth-basic/src/index.ts index df5ace6d..d7a5783a 100644 --- a/plugins/auth-basic/src/index.ts +++ b/plugins/auth-basic/src/index.ts @@ -21,7 +21,8 @@ export const plugin: PluginDefinition = { }, ], async onApply(_ctx, { values }) { - const { username, password } = values; + const username = values.username ?? ''; + const password = values.password ?? ''; const value = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`; return { setHeaders: [{ name: 'Authorization', value }] }; }, diff --git a/plugins/auth-basic/tests/index.test.ts b/plugins/auth-basic/tests/index.test.ts new file mode 100644 index 00000000..b337c689 --- /dev/null +++ b/plugins/auth-basic/tests/index.test.ts @@ -0,0 +1,77 @@ +import type { Context } from '@yaakapp/api'; +import { describe, expect, test } from 'vitest'; +import { plugin } from '../src'; + +const ctx = {} as Context; + +describe('auth-basic', () => { + test('Both username and password', async () => { + expect( + await plugin.authentication?.onApply(ctx, { + values: { username: 'user', password: 'pass' }, + headers: [], + url: 'https://yaak.app', + method: 'POST', + contextId: '111', + }), + ).toEqual({ + setHeaders: [{ name: 'Authorization', value: `Basic ${Buffer.from('user:pass').toString('base64')}` }], + }); + }); + + test('Empty password', async () => { + expect( + await plugin.authentication?.onApply(ctx, { + values: { username: 'apikey', password: '' }, + headers: [], + url: 'https://yaak.app', + method: 'POST', + contextId: '111', + }), + ).toEqual({ + setHeaders: [{ name: 'Authorization', value: `Basic ${Buffer.from('apikey:').toString('base64')}` }], + }); + }); + + test('Missing password (undefined)', async () => { + expect( + await plugin.authentication?.onApply(ctx, { + values: { username: 'apikey' }, + headers: [], + url: 'https://yaak.app', + method: 'POST', + contextId: '111', + }), + ).toEqual({ + setHeaders: [{ name: 'Authorization', value: `Basic ${Buffer.from('apikey:').toString('base64')}` }], + }); + }); + + test('Missing username (undefined)', async () => { + expect( + await plugin.authentication?.onApply(ctx, { + values: { password: 'secret' }, + headers: [], + url: 'https://yaak.app', + method: 'POST', + contextId: '111', + }), + ).toEqual({ + setHeaders: [{ name: 'Authorization', value: `Basic ${Buffer.from(':secret').toString('base64')}` }], + }); + }); + + test('No values (both undefined)', async () => { + expect( + await plugin.authentication?.onApply(ctx, { + values: {}, + headers: [], + url: 'https://yaak.app', + method: 'POST', + contextId: '111', + }), + ).toEqual({ + setHeaders: [{ name: 'Authorization', value: `Basic ${Buffer.from(':').toString('base64')}` }], + }); + }); +});