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
+25 -36
View File
@@ -1,50 +1,39 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Livewire\Volt\Volt;
use Tests\TestCase;
class PasswordUpdateTest extends TestCase
{
use RefreshDatabase;
test('password can be updated', function () {
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
public function test_password_can_be_updated(): void
{
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.password')
->set('current_password', 'password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response = Volt::test('settings.password')
->set('current_password', 'password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response->assertHasNoErrors();
$response->assertHasNoErrors();
expect(Hash::check('new-password', $user->refresh()->password))->toBeTrue();
});
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
}
test('correct password must be provided to update password', function () {
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
public function test_correct_password_must_be_provided_to_update_password(): void
{
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.password')
->set('current_password', 'wrong-password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response = Volt::test('settings.password')
->set('current_password', 'wrong-password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response->assertHasErrors(['current_password']);
}
}
$response->assertHasErrors(['current_password']);
});
+48 -62
View File
@@ -1,89 +1,75 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Volt\Volt;
use Tests\TestCase;
class ProfileUpdateTest extends TestCase
{
use RefreshDatabase;
test('profile page is displayed', function () {
$this->actingAs($user = User::factory()->create());
public function test_profile_page_is_displayed(): void
{
$this->actingAs($user = User::factory()->create());
$this->get(route('profile.edit'))->assertOk();
});
$this->get(route('profile.edit'))->assertOk();
}
test('profile information can be updated', function () {
$user = User::factory()->create();
public function test_profile_information_can_be_updated(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.profile')
->set('name', 'Test User')
->set('email', 'test@example.com')
->call('updateProfileInformation');
$response = Volt::test('settings.profile')
->set('name', 'Test User')
->set('email', 'test@example.com')
->call('updateProfileInformation');
$response->assertHasNoErrors();
$response->assertHasNoErrors();
$user->refresh();
$user->refresh();
expect($user->name)->toEqual('Test User');
expect($user->email)->toEqual('test@example.com');
expect($user->email_verified_at)->toBeNull();
});
$this->assertEquals('Test User', $user->name);
$this->assertEquals('test@example.com', $user->email);
$this->assertNull($user->email_verified_at);
}
test('email verification status is unchanged when email address is unchanged', function () {
$user = User::factory()->create();
public function test_email_verification_status_is_unchanged_when_email_address_is_unchanged(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.profile')
->set('name', 'Test User')
->set('email', $user->email)
->call('updateProfileInformation');
$response = Volt::test('settings.profile')
->set('name', 'Test User')
->set('email', $user->email)
->call('updateProfileInformation');
$response->assertHasNoErrors();
$response->assertHasNoErrors();
expect($user->refresh()->email_verified_at)->not->toBeNull();
});
$this->assertNotNull($user->refresh()->email_verified_at);
}
test('user can delete their account', function () {
$user = User::factory()->create();
public function test_user_can_delete_their_account(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.delete-user-form')
->set('password', 'password')
->call('deleteUser');
$response = Volt::test('settings.delete-user-form')
->set('password', 'password')
->call('deleteUser');
$response
->assertHasNoErrors()
->assertRedirect('/');
$response
->assertHasNoErrors()
->assertRedirect('/');
expect($user->fresh())->toBeNull();
expect(auth()->check())->toBeFalse();
});
$this->assertNull($user->fresh());
$this->assertFalse(auth()->check());
}
test('correct password must be provided to delete account', function () {
$user = User::factory()->create();
public function test_correct_password_must_be_provided_to_delete_account(): void
{
$user = User::factory()->create();
$this->actingAs($user);
$this->actingAs($user);
$response = Volt::test('settings.delete-user-form')
->set('password', 'wrong-password')
->call('deleteUser');
$response = Volt::test('settings.delete-user-form')
->set('password', 'wrong-password')
->call('deleteUser');
$response->assertHasErrors(['password']);
$response->assertHasErrors(['password']);
$this->assertNotNull($user->fresh());
}
}
expect($user->fresh())->not->toBeNull();
});
@@ -1,86 +1,70 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Livewire\Volt\Volt;
use Tests\TestCase;
class TwoFactorAuthenticationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
beforeEach(function () {
if (! Features::canManageTwoFactorAuthentication()) {
$this->markTestSkipped('Two-factor authentication is not enabled.');
}
public function test_two_factor_settings_page_can_be_rendered(): void
{
$user = User::factory()->withoutTwoFactor()->create();
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
});
$this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('two-factor.show'))
->assertOk()
->assertSee('Two Factor Authentication')
->assertSee('Disabled');
}
test('two factor settings page can be rendered', function () {
$user = User::factory()->withoutTwoFactor()->create();
public function test_two_factor_settings_page_requires_password_confirmation_when_enabled(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('two-factor.show'))
->assertOk()
->assertSee('Two Factor Authentication')
->assertSee('Disabled');
});
$response = $this->actingAs($user)
->get(route('two-factor.show'));
test('two factor settings page requires password confirmation when enabled', function () {
$user = User::factory()->create();
$response->assertRedirect(route('password.confirm'));
}
$response = $this->actingAs($user)
->get(route('two-factor.show'));
public function test_two_factor_settings_page_returns_forbidden_response_when_two_factor_is_disabled(): void
{
config(['fortify.features' => []]);
$response->assertRedirect(route('password.confirm'));
});
$user = User::factory()->create();
test('two factor settings page returns forbidden response when two factor is disabled', function () {
config(['fortify.features' => []]);
$response = $this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('two-factor.show'));
$user = User::factory()->create();
$response->assertForbidden();
}
$response = $this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('two-factor.show'));
public function test_two_factor_authentication_disabled_when_confirmation_abandoned_between_requests(): void
{
$user = User::factory()->create();
$response->assertForbidden();
});
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => null,
])->save();
test('two factor authentication disabled when confirmation abandoned between requests', function () {
$user = User::factory()->create();
$this->actingAs($user);
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => null,
])->save();
$component = Volt::test('settings.two-factor');
$this->actingAs($user);
$component->assertSet('twoFactorEnabled', false);
$component = Volt::test('settings.two-factor');
$this->assertDatabaseHas('users', [
'id' => $user->id,
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
]);
}
}
$component->assertSet('twoFactorEnabled', false);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
]);
});