added tests

login working as expected
This commit is contained in:
Sergio Brighenti 2024-08-25 01:29:02 +02:00
parent c981498810
commit 6d6923eb09
18 changed files with 1392 additions and 280 deletions

View file

@ -3,6 +3,7 @@
namespace App\Livewire\Auth;
use App\Livewire\Forms\LoginForm;
use Laravel\Fortify\Fortify;
use Livewire\Component;
use Mary\Traits\Toast;
@ -17,6 +18,8 @@ class Login extends Component
$this->validate();
$this->form->authenticate();
return redirect()->intended(Fortify::redirects('dashboard'));
}
public function render()

View file

@ -20,7 +20,7 @@
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0.1"
"pestphp/pest": "^2.35"
},
"autoload": {
"psr-4": {

1253
core/composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -73,7 +73,7 @@ return [
|
*/
'home' => '/home',
'home' => '/dashboard',
/*
|--------------------------------------------------------------------------

View file

@ -13,11 +13,10 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
'email' => 'a@a',
'password' => bcrypt('aaa'),
]);
}
}

View file

@ -22,8 +22,8 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>

View file

@ -30,7 +30,14 @@
<x-list-item :item="$user" value="name" sub-value="email" no-separator no-hover class="-mx-2 !-my-2 rounded">
<x-slot:actions>
<x-button icon="o-power" class="btn-circle btn-ghost btn-xs" tooltip-left="logoff" no-wire-navigate link="/logout"/>
<form method="POST" action="{{ route('logout') }}">
@csrf
<x-button icon="o-power" class="btn-circle btn-ghost btn-xs" tooltip-left="logoff" onclick="event.preventDefault();this.closest('form').submit();"/>
</form>
</x-slot:actions>
</x-list-item>

View file

@ -16,4 +16,6 @@ use Illuminate\Support\Facades\Route;
Route::redirect('/', '/dashboard');
Route::get('/dashboard', Welcome::class);
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', Welcome::class)->name('dashboard');
});

View file

@ -0,0 +1,72 @@
<?php
use App\Livewire\Auth\Login;
use App\Models\User;
test('login screen can be rendered', function () {
$response = $this->get('/login');
$response
->assertOk()
->assertSeeLivewire(Login::class);
});
test('users can authenticate using the login screen', function () {
$user = User::factory()->create();
$component = Livewire::test(Login::class)
->set('form.email', $user->email)
->set('form.password', 'password');
$component->call('authenticate');
$component
->assertHasNoErrors()
->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated();
});
test('users can not authenticate with invalid password', function () {
$user = User::factory()->create();
$component = Volt::test('pages.auth.login')
->set('form.email', $user->email)
->set('form.password', 'wrong-password');
$component->call('login');
$component
->assertHasErrors()
->assertNoRedirect();
$this->assertGuest();
});
test('navigation menu can be rendered', function () {
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->get('/dashboard');
$response
->assertOk()
->assertSeeVolt('layout.navigation');
});
test('users can logout', function () {
$user = User::factory()->create();
$this->actingAs($user);
$component = Volt::test('layout.navigation');
$component->call('logout');
$component
->assertHasNoErrors()
->assertRedirect('/');
$this->assertGuest();
});

View file

@ -0,0 +1,46 @@
<?php
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
test('email verification screen can be rendered', function () {
$user = User::factory()->unverified()->create();
$response = $this->actingAs($user)->get('/verify-email');
$response->assertStatus(200);
});
test('email can be verified', function () {
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
});
test('email is not verified with invalid hash', function () {
$user = User::factory()->unverified()->create();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$this->actingAs($user)->get($verificationUrl);
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
});

View file

@ -0,0 +1,46 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Livewire\Volt\Volt;
test('confirm password screen can be rendered', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/confirm-password');
$response
->assertSeeVolt('pages.auth.confirm-password')
->assertStatus(200);
});
test('password can be confirmed', function () {
$user = User::factory()->create();
$this->actingAs($user);
$component = Volt::test('pages.auth.confirm-password')
->set('password', 'password');
$component->call('confirmPassword');
$component
->assertRedirect('/dashboard')
->assertHasNoErrors();
});
test('password is not confirmed with invalid password', function () {
$user = User::factory()->create();
$this->actingAs($user);
$component = Volt::test('pages.auth.confirm-password')
->set('password', 'wrong-password');
$component->call('confirmPassword');
$component
->assertNoRedirect()
->assertHasErrors('password');
});

View file

@ -0,0 +1,73 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Livewire\Volt\Volt;
test('reset password link screen can be rendered', function () {
$response = $this->get('/forgot-password');
$response
->assertSeeVolt('pages.auth.forgot-password')
->assertStatus(200);
});
test('reset password link can be requested', function () {
Notification::fake();
$user = User::factory()->create();
Volt::test('pages.auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class);
});
test('reset password screen can be rendered', function () {
Notification::fake();
$user = User::factory()->create();
Volt::test('pages.auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get('/reset-password/'.$notification->token);
$response
->assertSeeVolt('pages.auth.reset-password')
->assertStatus(200);
return true;
});
});
test('password can be reset with valid token', function () {
Notification::fake();
$user = User::factory()->create();
Volt::test('pages.auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$component = Volt::test('pages.auth.reset-password', ['token' => $notification->token])
->set('email', $user->email)
->set('password', 'password')
->set('password_confirmation', 'password');
$component->call('resetPassword');
$component
->assertRedirect('/login')
->assertHasNoErrors();
return true;
});
});

View file

@ -0,0 +1,41 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Livewire\Volt\Volt;
test('password can be updated', function () {
$user = User::factory()->create();
$this->actingAs($user);
$component = Volt::test('profile.update-password-form')
->set('current_password', 'password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$component
->assertHasNoErrors()
->assertNoRedirect();
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
});
test('correct password must be provided to update password', function () {
$user = User::factory()->create();
$this->actingAs($user);
$component = Volt::test('profile.update-password-form')
->set('current_password', 'wrong-password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$component
->assertHasErrors(['current_password'])
->assertNoRedirect();
});

View file

@ -0,0 +1,27 @@
<?php
namespace Tests\Feature\Auth;
use Livewire\Volt\Volt;
test('registration screen can be rendered', function () {
$response = $this->get('/register');
$response
->assertOk()
->assertSeeVolt('pages.auth.register');
});
test('new users can register', function () {
$component = Volt::test('pages.auth.register')
->set('name', 'Test User')
->set('email', 'test@example.com')
->set('password', 'password')
->set('password_confirmation', 'password');
$component->call('register');
$component->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated();
});

View file

@ -1,19 +0,0 @@
<?php
namespace Tests\Feature;
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

47
core/tests/Pest.php Normal file
View file

@ -0,0 +1,47 @@
<?php
use Tests\TestCase;
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/
uses(TestCase::class)->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}

View file

@ -2,9 +2,10 @@
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
use RefreshDatabase;
}

View file

@ -1,16 +0,0 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}