mirror of
https://github.com/10h30/Test-Laravel-Auth-Basics.git
synced 2026-06-05 15:07:43 +09:00
Email verification test
This commit is contained in:
@@ -57,3 +57,14 @@ If the password is filled in, also update that.
|
||||
|
||||
Test methods: `test_profile_name_email_update_successful()` and `test_profile_password_update_successful()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 5. Email Verification.
|
||||
|
||||
Make the URL `/secretpage` available only to those who verified their email.
|
||||
You need to make changes to two files.
|
||||
|
||||
In file `routes/web.php` add a Middleware to `/secretpage` URL.
|
||||
And enable email verification in the `app/Models/User.php` file.
|
||||
|
||||
Test method: `test_email_can_be_verified()`.
|
||||
@@ -0,0 +1,17 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Secret page') }}
|
||||
</h2>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 bg-white border-b border-gray-200">
|
||||
This page should be visible only for those who verified their email.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
@@ -23,4 +23,9 @@ Route::get('users', [\App\Http\Controllers\UserController::class, 'index'])->nam
|
||||
Route::get('profile', [\App\Http\Controllers\ProfileController::class, 'show'])->name('profile.show');
|
||||
Route::put('profile', [\App\Http\Controllers\ProfileController::class, 'update'])->name('profile.update');
|
||||
|
||||
// Task: this "/secretpage" URL should be visible only for those who VERIFIED their email
|
||||
// Add some middleware here, and change some code in app/Models/User.php to enable this
|
||||
Route::view('/secretpage', 'secretpage')
|
||||
->name('secretpage');
|
||||
|
||||
require __DIR__.'/auth.php';
|
||||
|
||||
@@ -3,9 +3,13 @@
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
|
||||
class AuthenticationTest extends TestCase
|
||||
{
|
||||
@@ -78,4 +82,38 @@ class AuthenticationTest extends TestCase
|
||||
'password' => 'newpassword'
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_email_can_be_verified()
|
||||
{
|
||||
$newData = [
|
||||
'name' => 'New name',
|
||||
'email' => 'new@email.com',
|
||||
'password' => 'newpassword',
|
||||
'password_confirmation' => 'newpassword'
|
||||
];
|
||||
$response = $this->post('/register', $newData);
|
||||
$response->assertRedirect('/');
|
||||
|
||||
$response = $this->get('/secretpage');
|
||||
$response->assertRedirect('/verify-email');
|
||||
|
||||
$user = User::factory()->create([
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)]
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl);
|
||||
Event::assertDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
|
||||
$response = $this->get('/secretpage');
|
||||
$response->assertOk();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user