56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Notifications\VerifyEmail;
|
|
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Tests\TestCase;
|
|
|
|
class AuthenticationTest extends TestCase
|
|
{
|
|
use LazilyRefreshDatabase;
|
|
|
|
public function test_a_user_can_register_and_is_asked_to_verify_email(): void
|
|
{
|
|
Notification::fake();
|
|
|
|
$response = $this->post(route('register.store'), [
|
|
'name' => 'Ada Lovelace',
|
|
'email' => 'ada@example.com',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
]);
|
|
|
|
$user = User::query()->where('email', 'ada@example.com')->firstOrFail();
|
|
|
|
$response->assertRedirect('/dashboard');
|
|
$this->assertAuthenticatedAs($user);
|
|
$this->assertNull($user->email_verified_at);
|
|
Notification::assertSentTo($user, VerifyEmail::class);
|
|
}
|
|
|
|
public function test_an_unverified_user_cannot_open_the_dashboard(): void
|
|
{
|
|
$user = User::factory()->unverified()->create();
|
|
|
|
$this->actingAs($user)->get(route('dashboard'))
|
|
->assertRedirect(route('verification.notice'));
|
|
}
|
|
|
|
public function test_a_verified_user_can_log_in_and_log_out(): void
|
|
{
|
|
$user = User::factory()->create(['password' => 'password']);
|
|
|
|
$this->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
])->assertRedirect('/dashboard');
|
|
$this->assertAuthenticatedAs($user);
|
|
|
|
$this->post(route('logout'))->assertRedirect('/');
|
|
$this->assertGuest();
|
|
}
|
|
}
|