From 355d40f627517297da42010fbf3ea58d07769d43 Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 1 Nov 2021 08:21:55 +0200 Subject: [PATCH] Email verification test --- README.md | 11 ++++++++ resources/views/secretpage.blade.php | 17 +++++++++++++ routes/web.php | 5 ++++ tests/Feature/AuthenticationTest.php | 38 ++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 resources/views/secretpage.blade.php diff --git a/README.md b/README.md index d7ac311..f5da876 100644 --- a/README.md +++ b/README.md @@ -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()`. \ No newline at end of file diff --git a/resources/views/secretpage.blade.php b/resources/views/secretpage.blade.php new file mode 100644 index 0000000..6720a14 --- /dev/null +++ b/resources/views/secretpage.blade.php @@ -0,0 +1,17 @@ + + +

+ {{ __('Secret page') }} +

+
+ +
+
+
+
+ This page should be visible only for those who verified their email. +
+
+
+
+
diff --git a/routes/web.php b/routes/web.php index 967cf2d..1cd68ec 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'; diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php index 2343cbb..df79777 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/Feature/AuthenticationTest.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(); + } }