homeos/tests/Feature/AuthTest.php

76 lines
2.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Auth\Login;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Tests\TestCase;
class AuthTest extends TestCase
{
use RefreshDatabase;
public function test_root_redirects_to_dashboard(): void
{
$this->get('/')->assertRedirect(route('dashboard'));
}
public function test_guest_is_redirected_from_dashboard_to_login(): void
{
$this->get('/dashboard')->assertRedirect(route('login'));
}
public function test_login_screen_renders(): void
{
$this->get('/login')->assertOk()->assertSee(__('auth.title'));
}
public function test_user_can_authenticate(): void
{
$user = User::factory()->create(['password' => Hash::make('secret-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'secret-password')
->call('login')
->assertHasNoErrors()
->assertRedirect(route('dashboard'));
$this->assertAuthenticatedAs($user);
}
public function test_login_fails_with_wrong_password(): void
{
$user = User::factory()->create(['password' => Hash::make('secret-password')]);
Livewire::test(Login::class)
->set('email', $user->email)
->set('password', 'wrong-password')
->call('login')
->assertHasErrors('email');
$this->assertGuest();
}
public function test_authenticated_user_can_view_dashboard(): void
{
$user = User::factory()->create();
$this->actingAs($user)->get('/dashboard')
->assertOk()
->assertSee(__('dashboard.title'));
}
public function test_user_can_log_out(): void
{
$user = User::factory()->create();
$this->actingAs($user)->post('/logout')->assertRedirect(route('login'));
$this->assertGuest();
}
}