Files
BUT2/mini-crm/tests/Feature/Tp7AuthenticationTest.php
T

63 lines
1.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class Tp7AuthenticationTest extends TestCase
{
use RefreshDatabase;
public function test_guest_is_redirected_from_the_mini_crm_to_login(): void
{
$this->get(route('clients.index'))->assertRedirect(route('login'));
$this->get(route('products.index'))->assertRedirect(route('login'));
$this->get(route('dashboard'))->assertRedirect(route('login'));
}
public function test_user_can_log_in_with_valid_credentials(): void
{
$user = User::factory()->create([
'email' => 'jean@example.com',
'password' => 'password123',
]);
$this->post(route('login.store'), [
'email' => 'jean@example.com',
'password' => 'password123',
])->assertRedirect(route('dashboard'));
$this->assertAuthenticatedAs($user);
}
public function test_invalid_credentials_are_rejected_and_email_is_kept(): void
{
User::factory()->create([
'email' => 'jean@example.com',
'password' => 'password123',
]);
$this->from(route('login'))->post(route('login.store'), [
'email' => 'jean@example.com',
'password' => 'incorrect',
])->assertRedirect(route('login'))
->assertSessionHasErrors(['email' => 'Les identifiants sont incorrects.'])
->assertSessionHasInput('email', 'jean@example.com');
$this->assertGuest();
}
public function test_authenticated_user_can_log_out(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post(route('logout'))
->assertRedirect(route('login'));
$this->assertGuest();
}
}