From 469050594d4c592e7387ab995cbd7b3226fe823f Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Fri, 27 Oct 2023 14:44:44 +0530 Subject: [PATCH] test: improve tests coverage --- tests/auth/authenticator_client.spec.ts | 78 +++++++++++++++++++++++ tests/auth/errors.spec.ts | 15 +++++ tests/guards/session/authenticate.spec.ts | 4 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/auth/authenticator_client.spec.ts diff --git a/tests/auth/authenticator_client.spec.ts b/tests/auth/authenticator_client.spec.ts new file mode 100644 index 0000000..b28567c --- /dev/null +++ b/tests/auth/authenticator_client.spec.ts @@ -0,0 +1,78 @@ +/* + * @adonisjs/auth + * + * (c) AdonisJS + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { test } from '@japa/runner' +import { HttpContextFactory } from '@adonisjs/core/factories/http' + +import { FactoryUser } from '../../factories/lucid_user_provider.js' +import { createDatabase, createEmitter, createTables } from '../helpers.js' +import { SessionGuardFactory } from '../../factories/session_guard_factory.js' +import { AuthenticatorClient } from '../../src/auth/authenticator_client.js' + +test.group('Authenticator client', () => { + test('create authenticator client with guards', async ({ assert, expectTypeOf }) => { + const emitter = createEmitter() + const ctx = new HttpContextFactory().create() + const sessionGuard = new SessionGuardFactory().create(ctx).withEmitter(emitter) + + const client = new AuthenticatorClient({ + default: 'web', + guards: { + web: () => sessionGuard, + }, + }) + + assert.instanceOf(client, AuthenticatorClient) + expectTypeOf(client.use).parameters.toMatchTypeOf<['web'?]>() + }) + + test('access guard using its name', async ({ assert, expectTypeOf }) => { + const emitter = createEmitter() + const ctx = new HttpContextFactory().create() + const sessionGuard = new SessionGuardFactory().create(ctx).withEmitter(emitter) + + const client = new AuthenticatorClient({ + default: 'web', + guards: { + web: () => sessionGuard, + }, + }) + + const webGuard = client.use('web') + assert.strictEqual(webGuard, sessionGuard) + assert.equal(client.defaultGuard, 'web') + assert.equal(webGuard.driverName, 'session') + assert.strictEqual(client.use('web'), client.use('web')) + assert.strictEqual(client.use(), client.use('web')) + expectTypeOf(webGuard.user).toMatchTypeOf() + }) + + test('call authenticateAsClient via client', async ({ assert }) => { + const db = await createDatabase() + await createTables(db) + + const emitter = createEmitter() + const ctx = new HttpContextFactory().create() + const user = await FactoryUser.createWithDefaults() + const sessionGuard = new SessionGuardFactory().create(ctx).withEmitter(emitter) + + const client = new AuthenticatorClient({ + default: 'web', + guards: { + web: () => sessionGuard, + }, + }) + + assert.deepEqual(await client.use('web').authenticateAsClient(user), { + session: { + auth_web: user.id, + }, + }) + }) +}) diff --git a/tests/auth/errors.spec.ts b/tests/auth/errors.spec.ts index 5fee0a8..38f5f75 100644 --- a/tests/auth/errors.spec.ts +++ b/tests/auth/errors.spec.ts @@ -127,6 +127,21 @@ test.group('Errors | AuthenticationException', () => { assert.equal(ctx.response.getStatus(), 401) assert.equal(ctx.response.getBody(), 'Unauthorized access') }) + + test('handle basic auth exception with a prompt', async ({ assert }) => { + const sessionMiddleware = await new SessionMiddlewareFactory().create() + const error = AuthenticationException.E_INVALID_BASIC_AUTH_CREDENTIALS() + + const ctx = new HttpContextFactory().create() + await sessionMiddleware.handle(ctx, async () => { + return error.handle(error, ctx) + }) + + assert.equal( + ctx.response.getHeader('WWW-Authenticate'), + `Basic realm="Authenticate", charset="UTF-8"` + ) + }) }) test.group('Errors | InvalidCredentialsException', () => { diff --git a/tests/guards/session/authenticate.spec.ts b/tests/guards/session/authenticate.spec.ts index e270555..523d543 100644 --- a/tests/guards/session/authenticate.spec.ts +++ b/tests/guards/session/authenticate.spec.ts @@ -381,7 +381,9 @@ test.group('Session guard | authenticate', () => { const sessionGuard = new SessionGuardFactory().create(ctx).withEmitter(emitter) assert.deepEqual(await sessionGuard.authenticateAsClient(user), { - auth_web: user.id, + session: { + auth_web: user.id, + }, }) }) })