Skip to content

Commit

Permalink
[2.x] Fixes database test (#78)
Browse files Browse the repository at this point in the history
* [2.x] Fixes database tests.

* Apply fixes from StyleCI

[ci skip] [skip ci]

* [2.x] Fixes database tests.

* Apply fixes from StyleCI

[ci skip] [skip ci]

---------

Co-authored-by: Italo <[email protected]>
  • Loading branch information
DarkGhostHunter and DarkGhostHunter authored Mar 18, 2024
1 parent f1053f8 commit a067268
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 150 deletions.
3 changes: 2 additions & 1 deletion src/Http/Requests/AssertionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ class AssertionRequest extends FormRequest

/**
* Validate the class instance.
*
* @return void
*/
public function validateResolved(): void
{
//
}

/**
Expand Down
11 changes: 0 additions & 11 deletions src/Http/Requests/AttestationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ class AttestationRequest extends FormRequest
{
/**
* The attestation instance that would be returned.
*
* @var \Laragear\WebAuthn\Attestation\Creator\AttestationCreation
*/
protected AttestationCreation $attestation;

/**
* Validate the class instance.
*
* @return void
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function validateResolved(): void
{
Expand All @@ -38,9 +34,6 @@ public function validateResolved(): void

/**
* Determine if the user is authorized to make this request.
*
* @param \Laragear\WebAuthn\Contracts\WebAuthnAuthenticatable|null $user
* @return bool
*/
public function authorize(?WebAuthnAuthenticatable $user): bool
{
Expand All @@ -49,8 +42,6 @@ public function authorize(?WebAuthnAuthenticatable $user): bool

/**
* Returns the existing attestation instance.
*
* @return \Laragear\WebAuthn\Attestation\Creator\AttestationCreation
*/
protected function attestation(): AttestationCreation
{
Expand Down Expand Up @@ -107,8 +98,6 @@ public function allowDuplicates(): static

/**
* Returns a response with the instructions to create a WebAuthn Credential.
*
* @return \Illuminate\Contracts\Support\Responsable
*/
public function toCreate(): Responsable
{
Expand Down
28 changes: 16 additions & 12 deletions tests/Assertion/CreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,44 @@
use Laragear\WebAuthn\Assertion\Creator\AssertionCreator;
use Laragear\WebAuthn\Challenge;
use Laragear\WebAuthn\Enums\UserVerification;
use Orchestra\Testbench\Attributes\WithMigration;
use Ramsey\Uuid\Uuid;
use Tests\DatabaseTestCase;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;

use function config;
use function in_array;
use function now;
use function session;

#[WithMigration]
class CreatorTest extends TestCase
class CreatorTest extends DatabaseTestCase
{
protected Request $request;
protected WebAuthnAuthenticatableUser $user;
protected AssertionCreation $creation;
protected AssertionCreator $creator;

protected function setUp(): void
protected function defineDatabaseSeeders(): void
{
parent::setUp();

$this->request = Request::create('https://test.app/webauthn/create', 'POST');
$this->user = WebAuthnAuthenticatableUser::forceCreate([
'name' => 'test',
'email' => '[email protected]',
'password' => 'test_password',
]);
}

$this->creator = new AssertionCreator($this->app);
$this->creation = new AssertionCreation($this->request);
protected function setUp(): void
{
$this->afterApplicationCreated(function (): void {
$this->request = Request::create('https://test.app/webauthn/create', 'POST');

$this->creator = new AssertionCreator($this->app);
$this->creation = new AssertionCreation($this->request);

$this->startSession();
$this->request->setLaravelSession($this->app->make('session.store'));
$this->startSession();
$this->request->setLaravelSession($this->app->make('session.store'));
});

parent::setUp();
}

protected function response(): TestResponse
Expand Down
62 changes: 34 additions & 28 deletions tests/Assertion/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
use Laragear\WebAuthn\Exceptions\AssertionException;
use Laragear\WebAuthn\Models\WebAuthnCredential;
use Mockery;
use Orchestra\Testbench\Attributes\WithMigration;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\DatabaseTestCase;
use Tests\FakeAuthenticator;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;
use Throwable;

use function base64_decode;
Expand All @@ -35,46 +34,23 @@
use function now;
use function session;

#[WithMigration]
class ValidationTest extends TestCase
class ValidationTest extends DatabaseTestCase
{
protected Request $request;
protected WebAuthnAuthenticatableUser $user;
protected AssertionValidation $validation;
protected AssertionValidator $validator;
protected Challenge $challenge;

protected function setUp(): void
protected function defineDatabaseSeeders(): void
{
parent::setUp();

// Force booting the model if not booted previously.
WebAuthnCredential::make();

$this->request = Request::create(
'https://test.app/webauthn/create', 'POST', content: json_encode(FakeAuthenticator::assertionResponse())
);

$this->user = WebAuthnAuthenticatableUser::forceCreate([
'name' => FakeAuthenticator::ATTESTATION_USER['displayName'],
'email' => FakeAuthenticator::ATTESTATION_USER['name'],
'password' => 'test_password',
]);

$this->validator = new AssertionValidator($this->app);
$this->validation = new AssertionValidation($this->request);

$this->travelTo(now()->startOfSecond());

$this->challenge = new Challenge(
new ByteBuffer(base64_decode(FakeAuthenticator::ASSERTION_CHALLENGE)), 60, false,
);

$this->session(['_webauthn' => $this->challenge]);

$this->request->setLaravelSession($this->app->make('session.store'));

$this->credential = DB::table('webauthn_credentials')->insert([
DB::table('webauthn_credentials')->insert([
'id' => FakeAuthenticator::CREDENTIAL_ID,
'authenticatable_type' => WebAuthnAuthenticatableUser::class,
'authenticatable_id' => 1,
Expand All @@ -90,6 +66,36 @@ protected function setUp(): void
]);
}

protected function defineEnvironment($app): void
{
$this->travelTo(now()->startOfSecond());
}

protected function setUp(): void
{
$this->afterApplicationCreated(function (): void {
// Force booting the model if not booted previously.
WebAuthnCredential::make();

$this->request = Request::create(
'https://test.app/webauthn/create', 'POST', content: json_encode(FakeAuthenticator::assertionResponse())
);

$this->validator = new AssertionValidator($this->app);
$this->validation = new AssertionValidation($this->request);

$this->challenge = new Challenge(
new ByteBuffer(base64_decode(FakeAuthenticator::ASSERTION_CHALLENGE)), 60, false,
);

$this->session(['_webauthn' => $this->challenge]);

$this->request->setLaravelSession($this->app->make('session.store'));
});

parent::setUp();
}

protected function validate(): AssertionValidation
{
return $this->validator->send($this->validation)->thenReturn();
Expand Down
28 changes: 16 additions & 12 deletions tests/Attestation/CreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,43 @@
use Laragear\WebAuthn\Challenge;
use Laragear\WebAuthn\Enums\ResidentKey;
use Laragear\WebAuthn\Enums\UserVerification;
use Orchestra\Testbench\Attributes\WithMigration;
use Ramsey\Uuid\Uuid;
use Tests\DatabaseTestCase;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;

use function config;
use function now;
use function session;

#[WithMigration]
class CreatorTest extends TestCase
class CreatorTest extends DatabaseTestCase
{
protected Request $request;
protected WebAuthnAuthenticatableUser $user;
protected AttestationCreation $creation;
protected AttestationCreator $creator;

protected function setUp(): void
protected function defineDatabaseSeeders(): void
{
parent::setUp();

$this->request = Request::create('https://test.app/webauthn/create', 'POST');
$this->user = WebAuthnAuthenticatableUser::forceCreate([
'name' => 'test',
'email' => '[email protected]',
'password' => 'test_password',
]);
}

$this->creator = new AttestationCreator($this->app);
$this->creation = new AttestationCreation($this->user, $this->request);
protected function setUp(): void
{
$this->afterApplicationCreated(function (): void {
$this->request = Request::create('https://test.app/webauthn/create', 'POST');

$this->creator = new AttestationCreator($this->app);
$this->creation = new AttestationCreation($this->user, $this->request);

$this->startSession();
$this->request->setLaravelSession($this->app->make('session.store'));
$this->startSession();
$this->request->setLaravelSession($this->app->make('session.store'));
});

parent::setUp();
}

protected function response(): TestResponse
Expand Down
50 changes: 28 additions & 22 deletions tests/Attestation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
use Laragear\WebAuthn\Exceptions\AttestationException;
use Laragear\WebAuthn\Models\WebAuthnCredential;
use Mockery;
use Orchestra\Testbench\Attributes\WithMigration;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\ParameterBag;
use Tests\DatabaseTestCase;
use Tests\FakeAuthenticator;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;

use function base64_decode;
use function base64_encode;
Expand All @@ -42,44 +41,51 @@
*
* @see https://cbor.me
*/
#[WithMigration]
class ValidationTest extends TestCase
class ValidationTest extends DatabaseTestCase
{
protected Request $request;
protected WebAuthnAuthenticatableUser $user;
protected AttestationValidation $validation;
protected AttestationValidator $validator;
protected Challenge $challenge;

protected function setUp(): void
protected function defineDatabaseSeeders(): void
{
parent::setUp();

$this->request = Request::create(
'https://test.app/webauthn/create', 'POST', content: json_encode(FakeAuthenticator::attestationResponse())
);

$this->user = WebAuthnAuthenticatableUser::forceCreate([
'name' => FakeAuthenticator::ATTESTATION_USER['displayName'],
'email' => FakeAuthenticator::ATTESTATION_USER['name'],
'password' => 'test_password',
]);
}

$this->validator = new AttestationValidator($this->app);
$this->validation = new AttestationValidation($this->user, $this->request);

protected function defineEnvironment($app)
{
$this->travelTo(now()->startOfSecond());
}

$this->challenge = new Challenge(
new ByteBuffer(base64_decode(FakeAuthenticator::ATTESTATION_CHALLENGE)),
60,
false,
['user_uuid' => FakeAuthenticator::ATTESTATION_USER['id']]
);
protected function setUp(): void
{
$this->afterApplicationCreated(function (): void {
$this->request = Request::create(
'https://test.app/webauthn/create', 'POST', content: json_encode(FakeAuthenticator::attestationResponse())
);

$this->session(['_webauthn' => $this->challenge]);
$this->validator = new AttestationValidator($this->app);
$this->validation = new AttestationValidation($this->user, $this->request);

$this->challenge = new Challenge(
new ByteBuffer(base64_decode(FakeAuthenticator::ATTESTATION_CHALLENGE)),
60,
false,
['user_uuid' => FakeAuthenticator::ATTESTATION_USER['id']]
);

$this->request->setLaravelSession($this->app->make('session.store'));
$this->session(['_webauthn' => $this->challenge]);

$this->request->setLaravelSession($this->app->make('session.store'));
});

parent::setUp();
}

protected function validate(): AttestationValidation
Expand Down
8 changes: 3 additions & 5 deletions tests/Auth/EloquentWebAuthnProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@
use Laragear\WebAuthn\Exceptions\AssertionException;
use Laragear\WebAuthn\Models\WebAuthnCredential;
use Mockery;
use Orchestra\Testbench\Attributes\WithMigration;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Tests\DatabaseTestCase;
use Tests\FakeAuthenticator;
use Tests\Stubs\WebAuthnAuthenticatableUser;
use Tests\TestCase;

#[WithMigration]
class EloquentWebAuthnProviderTest extends TestCase
class EloquentWebAuthnProviderTest extends DatabaseTestCase
{
protected function defineEnvironment($app): void
{
$app->make('config')->set('auth.providers.users.driver', 'eloquent-webauthn');
$app->make('config')->set('auth.providers.users.model', WebAuthnAuthenticatableUser::class);
}

protected function afterRefreshingDatabase(): void
protected function defineDatabaseSeeders(): void
{
WebAuthnAuthenticatableUser::forceCreate([
'name' => FakeAuthenticator::ATTESTATION_USER['displayName'],
Expand Down
Loading

0 comments on commit a067268

Please sign in to comment.