Skip to content

Commit

Permalink
test: improve tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 27, 2023
1 parent cdcefff commit 4690505
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
78 changes: 78 additions & 0 deletions tests/auth/authenticator_client.spec.ts
Original file line number Diff line number Diff line change
@@ -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<FactoryUser | undefined>()
})

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,
},
})
})
})
15 changes: 15 additions & 0 deletions tests/auth/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 3 additions & 1 deletion tests/guards/session/authenticate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
})
})

0 comments on commit 4690505

Please sign in to comment.