Skip to content

Commit e9211c6

Browse files
author
ahmadhuss
committed
breeze:install Added tests/Feature
1 parent 1bbaaa3 commit e9211c6

File tree

5 files changed

+257
-0
lines changed

5 files changed

+257
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\User;
6+
use App\Providers\RouteServiceProvider;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Tests\TestCase;
9+
10+
class AuthenticationTest extends TestCase
11+
{
12+
use RefreshDatabase;
13+
14+
public function test_login_screen_can_be_rendered()
15+
{
16+
$response = $this->get('/login');
17+
18+
$response->assertStatus(200);
19+
}
20+
21+
public function test_users_can_authenticate_using_the_login_screen()
22+
{
23+
$user = User::factory()->create();
24+
25+
$response = $this->post('/login', [
26+
'email' => $user->email,
27+
'password' => 'password',
28+
]);
29+
30+
$this->assertAuthenticated();
31+
$response->assertRedirect(RouteServiceProvider::HOME);
32+
}
33+
34+
public function test_users_can_not_authenticate_with_invalid_password()
35+
{
36+
$user = User::factory()->create();
37+
38+
$this->post('/login', [
39+
'email' => $user->email,
40+
'password' => 'wrong-password',
41+
]);
42+
43+
$this->assertGuest();
44+
}
45+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\User;
6+
use App\Providers\RouteServiceProvider;
7+
use Illuminate\Auth\Events\Verified;
8+
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Facades\Event;
10+
use Illuminate\Support\Facades\URL;
11+
use Tests\TestCase;
12+
13+
class EmailVerificationTest extends TestCase
14+
{
15+
use RefreshDatabase;
16+
17+
public function test_email_verification_screen_can_be_rendered()
18+
{
19+
$user = User::factory()->create([
20+
'email_verified_at' => null,
21+
]);
22+
23+
$response = $this->actingAs($user)->get('/verify-email');
24+
25+
$response->assertStatus(200);
26+
}
27+
28+
public function test_email_can_be_verified()
29+
{
30+
$user = User::factory()->create([
31+
'email_verified_at' => null,
32+
]);
33+
34+
Event::fake();
35+
36+
$verificationUrl = URL::temporarySignedRoute(
37+
'verification.verify',
38+
now()->addMinutes(60),
39+
['id' => $user->id, 'hash' => sha1($user->email)]
40+
);
41+
42+
$response = $this->actingAs($user)->get($verificationUrl);
43+
44+
Event::assertDispatched(Verified::class);
45+
$this->assertTrue($user->fresh()->hasVerifiedEmail());
46+
$response->assertRedirect(RouteServiceProvider::HOME.'?verified=1');
47+
}
48+
49+
public function test_email_is_not_verified_with_invalid_hash()
50+
{
51+
$user = User::factory()->create([
52+
'email_verified_at' => null,
53+
]);
54+
55+
$verificationUrl = URL::temporarySignedRoute(
56+
'verification.verify',
57+
now()->addMinutes(60),
58+
['id' => $user->id, 'hash' => sha1('wrong-email')]
59+
);
60+
61+
$this->actingAs($user)->get($verificationUrl);
62+
63+
$this->assertFalse($user->fresh()->hasVerifiedEmail());
64+
}
65+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
class PasswordConfirmationTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
public function test_confirm_password_screen_can_be_rendered()
14+
{
15+
$user = User::factory()->create();
16+
17+
$response = $this->actingAs($user)->get('/confirm-password');
18+
19+
$response->assertStatus(200);
20+
}
21+
22+
public function test_password_can_be_confirmed()
23+
{
24+
$user = User::factory()->create();
25+
26+
$response = $this->actingAs($user)->post('/confirm-password', [
27+
'password' => 'password',
28+
]);
29+
30+
$response->assertRedirect();
31+
$response->assertSessionHasNoErrors();
32+
}
33+
34+
public function test_password_is_not_confirmed_with_invalid_password()
35+
{
36+
$user = User::factory()->create();
37+
38+
$response = $this->actingAs($user)->post('/confirm-password', [
39+
'password' => 'wrong-password',
40+
]);
41+
42+
$response->assertSessionHasErrors();
43+
}
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\User;
6+
use Illuminate\Auth\Notifications\ResetPassword;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Notification;
9+
use Tests\TestCase;
10+
11+
class PasswordResetTest extends TestCase
12+
{
13+
use RefreshDatabase;
14+
15+
public function test_reset_password_link_screen_can_be_rendered()
16+
{
17+
$response = $this->get('/forgot-password');
18+
19+
$response->assertStatus(200);
20+
}
21+
22+
public function test_reset_password_link_can_be_requested()
23+
{
24+
Notification::fake();
25+
26+
$user = User::factory()->create();
27+
28+
$this->post('/forgot-password', ['email' => $user->email]);
29+
30+
Notification::assertSentTo($user, ResetPassword::class);
31+
}
32+
33+
public function test_reset_password_screen_can_be_rendered()
34+
{
35+
Notification::fake();
36+
37+
$user = User::factory()->create();
38+
39+
$this->post('/forgot-password', ['email' => $user->email]);
40+
41+
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
42+
$response = $this->get('/reset-password/'.$notification->token);
43+
44+
$response->assertStatus(200);
45+
46+
return true;
47+
});
48+
}
49+
50+
public function test_password_can_be_reset_with_valid_token()
51+
{
52+
Notification::fake();
53+
54+
$user = User::factory()->create();
55+
56+
$this->post('/forgot-password', ['email' => $user->email]);
57+
58+
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
59+
$response = $this->post('/reset-password', [
60+
'token' => $notification->token,
61+
'email' => $user->email,
62+
'password' => 'password',
63+
'password_confirmation' => 'password',
64+
]);
65+
66+
$response->assertSessionHasNoErrors();
67+
68+
return true;
69+
});
70+
}
71+
}

tests/Feature/RegistrationTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Providers\RouteServiceProvider;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
class RegistrationTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
public function test_registration_screen_can_be_rendered()
14+
{
15+
$response = $this->get('/register');
16+
17+
$response->assertStatus(200);
18+
}
19+
20+
public function test_new_users_can_register()
21+
{
22+
$response = $this->post('/register', [
23+
'name' => 'Test User',
24+
'email' => 'test@example.com',
25+
'password' => 'password',
26+
'password_confirmation' => 'password',
27+
]);
28+
29+
$this->assertAuthenticated();
30+
$response->assertRedirect(RouteServiceProvider::HOME);
31+
}
32+
}

0 commit comments

Comments
 (0)