Skip to content

Commit 9a28884

Browse files
committed
feat: configure AppServiceProvider.php and update tests
1 parent 06e0558 commit 9a28884

File tree

4 files changed

+96
-10
lines changed

4 files changed

+96
-10
lines changed

app/Providers/AppServiceProvider.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
namespace App\Providers;
66

7+
use Carbon\CarbonImmutable;
8+
use Illuminate\Cache\RateLimiting\Limit;
9+
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Support\Facades\Date;
12+
use Illuminate\Support\Facades\DB;
13+
use Illuminate\Support\Facades\RateLimiter;
14+
use Illuminate\Support\Facades\URL;
15+
use Illuminate\Support\Facades\Vite;
716
use Illuminate\Support\ServiceProvider;
17+
use Illuminate\Validation\Rules\Password;
818

919
class AppServiceProvider extends ServiceProvider
1020
{
@@ -21,6 +31,77 @@ public function register(): void
2131
*/
2232
public function boot(): void
2333
{
24-
//
34+
Vite::prefetch(concurrency: 3);
35+
36+
RateLimiter::for('api', function (Request $request) {
37+
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
38+
});
39+
40+
$this->configureUrl();
41+
$this->configureCommands();
42+
$this->configureModels();
43+
$this->configureDates();
44+
$this->configurePasswordValidation();
45+
46+
$this->registerCustomMacros();
47+
}
48+
49+
/**
50+
* Configure the password validation rules.
51+
*/
52+
public function configurePasswordValidation(): void
53+
{
54+
Password::defaults(function () {
55+
$rule = Password::min(8)
56+
->mixedCase()
57+
->numbers()
58+
->symbols();
59+
60+
return $this->app->isProduction()
61+
? $rule->uncompromised()
62+
: $rule;
63+
});
64+
}
65+
66+
/**
67+
* Register custom macros.
68+
*/
69+
private function registerCustomMacros(): void {}
70+
71+
/**
72+
* Configure the URL generation.
73+
*/
74+
private function configureUrl(): void
75+
{
76+
if ($this->app->isProduction()) {
77+
URL::forceScheme('https');
78+
}
79+
}
80+
81+
/**
82+
* Configure the application's commands.
83+
*/
84+
private function configureCommands(): void
85+
{
86+
DB::prohibitDestructiveCommands(
87+
$this->app->isProduction()
88+
);
89+
}
90+
91+
/**
92+
* Configure the models.
93+
*/
94+
private function configureModels(): void
95+
{
96+
Model::shouldBeStrict(! $this->app->isProduction());
97+
Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
98+
}
99+
100+
/**
101+
* Configure the dates.
102+
*/
103+
private function configureDates(): void
104+
{
105+
Date::use(CarbonImmutable::class);
25106
}
26107
}

tests/Feature/Auth/PasswordResetTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
$response->assertStatus(200);
1212
});
13+
1314
test('reset password link can be requested', function () {
1415
Notification::fake();
1516

@@ -19,6 +20,7 @@
1920

2021
Notification::assertSentTo($user, ResetPassword::class);
2122
});
23+
2224
test('reset password screen can be rendered', function () {
2325
Notification::fake();
2426

@@ -34,6 +36,7 @@
3436
return true;
3537
});
3638
});
39+
3740
test('password can be reset with valid token', function () {
3841
Notification::fake();
3942

@@ -45,8 +48,8 @@
4548
$response = $this->post('/reset-password', [
4649
'token' => $notification->token,
4750
'email' => $user->email,
48-
'password' => 'password',
49-
'password_confirmation' => 'password',
51+
'password' => 'Password123!',
52+
'password_confirmation' => 'Password123!',
5053
]);
5154

5255
$response

tests/Feature/Auth/RegistrationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
$response->assertStatus(200);
99
});
10+
1011
test('new users can register', function () {
1112
$response = $this->post('/register', [
1213
'name' => 'Test User',
1314
'email' => '[email protected]',
14-
'password' => 'password',
15-
'password_confirmation' => 'password',
15+
'password' => 'Password123!',
16+
'password_confirmation' => 'Password123!',
1617
]);
1718

1819
$this->assertAuthenticated();

tests/Feature/Settings/PasswordUpdateTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
->from('/settings/password')
1313
->put('/settings/password', [
1414
'current_password' => 'password',
15-
'password' => 'new-password',
16-
'password_confirmation' => 'new-password',
15+
'password' => 'New-Password123!',
16+
'password_confirmation' => 'New-Password123!',
1717
]);
1818

1919
$response
2020
->assertSessionHasNoErrors()
2121
->assertRedirect('/settings/password');
2222

23-
expect(Hash::check('new-password', $user->refresh()->password))->toBeTrue();
23+
expect(Hash::check('New-Password123!', $user->refresh()->password))->toBeTrue();
2424
});
25+
2526
test('correct password must be provided to update password', function () {
2627
$user = User::factory()->create();
2728

@@ -30,8 +31,8 @@
3031
->from('/settings/password')
3132
->put('/settings/password', [
3233
'current_password' => 'wrong-password',
33-
'password' => 'new-password',
34-
'password_confirmation' => 'new-password',
34+
'password' => 'New-Password123!',
35+
'password_confirmation' => 'New-Password123!',
3536
]);
3637

3738
$response

0 commit comments

Comments
 (0)