Install Pest

This commit is contained in:
Bob Molitor
2025-10-16 14:22:58 +02:00
parent 81a98dd9f4
commit e8db0d9a5a
16 changed files with 1618 additions and 805 deletions
+59 -73
View File
@@ -1,91 +1,77 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
test('login screen can be rendered', function () {
$response = $this->get(route('login'));
public function test_login_screen_can_be_rendered(): void
{
$response = $this->get(route('login'));
$response->assertStatus(200);
});
$response->assertStatus(200);
test('users can authenticate using the login screen', function () {
$user = User::factory()->withoutTwoFactor()->create();
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login');
$response
->assertHasNoErrors()
->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated();
});
test('users can not authenticate with invalid password', function () {
$user = User::factory()->create();
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'wrong-password')
->call('login');
$response->assertHasErrors('email');
$this->assertGuest();
});
test('users with two factor enabled are redirected to two factor challenge', function () {
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
public function test_users_can_authenticate_using_the_login_screen(): void
{
$user = User::factory()->withoutTwoFactor()->create();
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login');
$user = User::factory()->create();
$response
->assertHasNoErrors()
->assertRedirect(route('dashboard', absolute: false));
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => now(),
])->save();
$this->assertAuthenticated();
}
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login');
public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();
$response->assertRedirect(route('two-factor.login'));
$response->assertSessionHas('login.id', $user->id);
$this->assertGuest();
});
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'wrong-password')
->call('login');
test('users can logout', function () {
$user = User::factory()->create();
$response->assertHasErrors('email');
$response = $this->actingAs($user)->post(route('logout'));
$this->assertGuest();
}
$response->assertRedirect(route('home'));
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge(): void
{
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$user = User::factory()->create();
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => now(),
])->save();
$response = LivewireVolt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login');
$response->assertRedirect(route('two-factor.login'));
$response->assertSessionHas('login.id', $user->id);
$this->assertGuest();
}
public function test_users_can_logout(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('logout'));
$response->assertRedirect(route('home'));
$this->assertGuest();
}
}
$this->assertGuest();
});
+43 -56
View File
@@ -1,80 +1,67 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;
class EmailVerificationTest extends TestCase
{
use RefreshDatabase;
test('email verification screen can be rendered', function () {
$user = User::factory()->unverified()->create();
public function test_email_verification_screen_can_be_rendered(): void
{
$user = User::factory()->unverified()->create();
$response = $this->actingAs($user)->get(route('verification.notice'));
$response = $this->actingAs($user)->get(route('verification.notice'));
$response->assertStatus(200);
});
$response->assertStatus(200);
}
test('email can be verified', function () {
$user = User::factory()->unverified()->create();
public function test_email_can_be_verified(): void
{
$user = User::factory()->unverified()->create();
Event::fake();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
Event::assertDispatched(Verified::class);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
});
$this->assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
}
test('email is not verified with invalid hash', function () {
$user = User::factory()->unverified()->create();
public function test_email_is_not_verified_with_invalid_hash(): void
{
$user = User::factory()->unverified()->create();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$this->actingAs($user)->get($verificationUrl);
$this->actingAs($user)->get($verificationUrl);
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
});
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
test('already verified user visiting verification link is redirected without firing event again', function () {
$user = User::factory()->create([
'email_verified_at' => now(),
]);
public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void
{
$user = User::factory()->create([
'email_verified_at' => now(),
]);
Event::fake();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$this->actingAs($user)->get($verificationUrl)
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
$this->actingAs($user)->get($verificationUrl)
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
$this->assertTrue($user->fresh()->hasVerifiedEmail());
Event::assertNotDispatched(Verified::class);
}
}
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
Event::assertNotDispatched(Verified::class);
});
@@ -1,21 +1,11 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PasswordConfirmationTest extends TestCase
{
use RefreshDatabase;
test('confirm password screen can be rendered', function () {
$user = User::factory()->create();
public function test_confirm_password_screen_can_be_rendered(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('password.confirm'));
$response = $this->actingAs($user)->get(route('password.confirm'));
$response->assertStatus(200);
}
}
$response->assertStatus(200);
});
+50 -63
View File
@@ -1,79 +1,66 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Livewire\Volt\Volt;
use Tests\TestCase;
class PasswordResetTest extends TestCase
{
use RefreshDatabase;
test('reset password link screen can be rendered', function () {
$response = $this->get(route('password.request'));
public function test_reset_password_link_screen_can_be_rendered(): void
{
$response = $this->get(route('password.request'));
$response->assertStatus(200);
});
test('reset password link can be requested', function () {
Notification::fake();
$user = User::factory()->create();
Volt::test('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('auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get(route('password.reset', $notification->token));
$response->assertStatus(200);
}
public function test_reset_password_link_can_be_requested(): void
{
Notification::fake();
return true;
});
});
$user = User::factory()->create();
test('password can be reset with valid token', function () {
Notification::fake();
Volt::test('auth.forgot-password')
$user = User::factory()->create();
Volt::test('auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = Volt::test('auth.reset-password', ['token' => $notification->token])
->set('email', $user->email)
->call('sendPasswordResetLink');
->set('password', 'password')
->set('password_confirmation', 'password')
->call('resetPassword');
Notification::assertSentTo($user, ResetPassword::class);
}
$response
->assertHasNoErrors()
->assertRedirect(route('login', absolute: false));
public function test_reset_password_screen_can_be_rendered(): void
{
Notification::fake();
$user = User::factory()->create();
Volt::test('auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get(route('password.reset', $notification->token));
$response->assertStatus(200);
return true;
});
}
public function test_password_can_be_reset_with_valid_token(): void
{
Notification::fake();
$user = User::factory()->create();
Volt::test('auth.forgot-password')
->set('email', $user->email)
->call('sendPasswordResetLink');
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = Volt::test('auth.reset-password', ['token' => $notification->token])
->set('email', $user->email)
->set('password', 'password')
->set('password_confirmation', 'password')
->call('resetPassword');
$response
->assertHasNoErrors()
->assertRedirect(route('login', absolute: false));
return true;
});
}
}
return true;
});
});
+16 -27
View File
@@ -1,35 +1,24 @@
<?php
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Volt\Volt;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
test('registration screen can be rendered', function () {
$response = $this->get(route('register'));
public function test_registration_screen_can_be_rendered(): void
{
$response = $this->get(route('register'));
$response->assertStatus(200);
});
$response->assertStatus(200);
}
test('new users can register', function () {
$response = Volt::test('auth.register')
->set('name', 'Test User')
->set('email', 'test@example.com')
->set('password', 'password')
->set('password_confirmation', 'password')
->call('register');
public function test_new_users_can_register(): void
{
$response = Volt::test('auth.register')
->set('name', 'Test User')
->set('email', 'test@example.com')
->set('password', 'password')
->set('password_confirmation', 'password')
->call('register');
$response
->assertHasNoErrors()
->assertRedirect(route('dashboard', absolute: false));
$response
->assertHasNoErrors()
->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated();
}
}
$this->assertAuthenticated();
});
+30 -41
View File
@@ -1,52 +1,41 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Livewire\Volt\Volt;
use Tests\TestCase;
class TwoFactorChallengeTest extends TestCase
{
use RefreshDatabase;
public function test_two_factor_challenge_redirects_to_login_when_not_authenticated(): void
{
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
$response = $this->get(route('two-factor.login'));
$response->assertRedirect(route('login'));
test('two factor challenge redirects to login when not authenticated', function () {
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
public function test_two_factor_challenge_can_be_rendered(): void
{
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
$response = $this->get(route('two-factor.login'));
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$response->assertRedirect(route('login'));
});
$user = User::factory()->create();
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => now(),
])->save();
Volt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login')
->assertRedirect(route('two-factor.login'))
->assertOk();
test('two factor challenge can be rendered', function () {
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
}
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$user = User::factory()->create();
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => now(),
])->save();
Volt::test('auth.login')
->set('email', $user->email)
->set('password', 'password')
->call('login')
->assertRedirect(route('two-factor.login'))
->assertOk();
});