Skip to content

Commit f3ee5b5

Browse files
committed
Merge branch 'replace_password_reset_with_fortify' into repalce_register_with_fortify
# Conflicts: # app/Providers/FortifyServiceProvider.php # config/fortify.php # tests/Feature/Auth/PasswordResetTest.php # tests/Feature/Settings/PasswordUpdateTest.php
2 parents 5ea8cc5 + 034974c commit f3ee5b5

File tree

8 files changed

+103
-13
lines changed

8 files changed

+103
-13
lines changed

config/fortify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
*/
145145

146146
'features' => [
147-
Features::registration(),
147+
// Features::registration(),
148148
Features::resetPasswords(),
149149
Features::emailVerification(),
150150
Features::twoFactorAuthentication([

resources/js/layouts/settings/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Button } from '@/components/ui/button';
44
import { Separator } from '@/components/ui/separator';
55
import { toUrl, urlIsActive } from '@/lib/utils';
66
import { edit as editAppearance } from '@/routes/appearance';
7-
import { edit as editPassword } from '@/routes/password';
87
import { edit as editProfile } from '@/routes/profile';
98
import { show } from '@/routes/two-factor';
9+
import { edit as editPassword } from '@/routes/user-password';
1010
import { type NavItem } from '@/types';
1111
import { Link } from '@inertiajs/vue3';
1212

resources/js/pages/settings/Password.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PasswordController from '@/actions/App/Http/Controllers/Settings/Password
33
import InputError from '@/components/InputError.vue';
44
import AppLayout from '@/layouts/AppLayout.vue';
55
import SettingsLayout from '@/layouts/settings/Layout.vue';
6-
import { edit } from '@/routes/password';
6+
import { edit } from '@/routes/user-password';
77
import { Form, Head } from '@inertiajs/vue3';
88
import { ref } from 'vue';
99

routes/settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Route::patch('settings/profile', [ProfileController::class, 'update'])->name('profile.update');
1414
Route::delete('settings/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
1515

16-
Route::get('settings/password', [PasswordController::class, 'edit'])->name('password.edit');
16+
Route::get('settings/password', [PasswordController::class, 'edit'])->name('user-password.edit');
1717

1818
Route::put('settings/password', [PasswordController::class, 'update'])
1919
->middleware('throttle:6,1')

tests/Feature/Auth/AuthenticationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Support\Facades\RateLimiter;
78
use Laravel\Fortify\Features;
89
use Tests\TestCase;
910

@@ -81,4 +82,18 @@ public function test_users_can_logout()
8182
$this->assertGuest();
8283
$response->assertRedirect(route('home'));
8384
}
85+
86+
public function test_users_are_rate_limited()
87+
{
88+
$user = User::factory()->create();
89+
90+
RateLimiter::increment(md5('login'.implode('|', [$user->email, '127.0.0.1'])), amount: 5);
91+
92+
$response = $this->post(route('login.store'), [
93+
'email' => $user->email,
94+
'password' => 'wrong-password',
95+
]);
96+
97+
$response->assertTooManyRequests();
98+
}
8499
}

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,42 @@ public function test_reset_password_screen_can_be_rendered()
4646
return true;
4747
});
4848
}
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(route('password.email'), ['email' => $user->email]);
57+
58+
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
59+
$response = $this->post(route('password.update'), [
60+
'token' => $notification->token,
61+
'email' => $user->email,
62+
'password' => 'password',
63+
'password_confirmation' => 'password',
64+
]);
65+
66+
$response
67+
->assertSessionHasNoErrors()
68+
->assertRedirect(route('login'));
69+
70+
return true;
71+
});
72+
}
73+
74+
public function test_password_cannot_be_reset_with_invalid_token(): void
75+
{
76+
$user = User::factory()->create();
77+
78+
$response = $this->post(route('password.update'), [
79+
'token' => 'invalid-token',
80+
'email' => $user->email,
81+
'password' => 'newpassword123',
82+
'password_confirmation' => 'newpassword123',
83+
]);
84+
85+
$response->assertSessionHasErrors('email');
86+
}
4987
}

tests/Feature/Auth/TwoFactorChallengeTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\User;
66
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Inertia\Testing\AssertableInertia;
78
use Laravel\Fortify\Features;
89
use Tests\TestCase;
910

@@ -35,17 +36,15 @@ public function test_two_factor_challenge_can_be_rendered(): void
3536

3637
$user = User::factory()->create();
3738

38-
$user->forceFill([
39-
'two_factor_secret' => encrypt('test-secret'),
40-
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
41-
'two_factor_confirmed_at' => now(),
42-
])->save();
43-
44-
$response = $this->post(route('login'), [
39+
$this->post(route('login'), [
4540
'email' => $user->email,
4641
'password' => 'password',
4742
]);
4843

49-
$response->assertRedirect(route('two-factor.login'));
44+
$this->get(route('two-factor.login'))
45+
->assertOk()
46+
->assertInertia(fn (AssertableInertia $page) => $page
47+
->component('auth/TwoFactorChallenge')
48+
);
5049
}
5150
}

tests/Feature/Settings/PasswordUpdateTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,46 @@ public function test_password_update_page_is_displayed()
1616

1717
$response = $this
1818
->actingAs($user)
19-
->get(route('password.edit'));
19+
->get(route('user-password.edit'));
2020

2121
$response->assertStatus(200);
2222
}
23+
24+
public function test_password_can_be_updated()
25+
{
26+
$user = User::factory()->create();
27+
28+
$response = $this
29+
->actingAs($user)
30+
->from(route('user-password.edit'))
31+
->put(route('user-password.update'), [
32+
'current_password' => 'password',
33+
'password' => 'new-password',
34+
'password_confirmation' => 'new-password',
35+
]);
36+
37+
$response
38+
->assertSessionHasNoErrors()
39+
->assertRedirect(route('user-password.edit'));
40+
41+
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
42+
}
43+
44+
public function test_correct_password_must_be_provided_to_update_password()
45+
{
46+
$user = User::factory()->create();
47+
48+
$response = $this
49+
->actingAs($user)
50+
->from(route('user-password.edit'))
51+
->put(route('user-password.update'), [
52+
'current_password' => 'wrong-password',
53+
'password' => 'new-password',
54+
'password_confirmation' => 'new-password',
55+
]);
56+
57+
$response
58+
->assertSessionHasErrors('current_password')
59+
->assertRedirect(route('user-password.edit'));
60+
}
2361
}

0 commit comments

Comments
 (0)