-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdcefff
commit 4690505
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters