fix(models): correct UserSetting import namespace and add test coverage

This commit is contained in:
2026-09-07 18:34:51 +02:00
parent 208efbb85d
commit 3502c3e85b
34 changed files with 2497 additions and 2 deletions
@@ -0,0 +1,30 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EmailNotificationTest extends TestCase
{
use RefreshDatabase;
public function test_verified_user_is_redirected(): void
{
$user = User::factory()->create();
$this->actingAs($user)
->post(route('verification.send'))
->assertRedirect(route('dashboard', absolute: false));
}
public function test_unverified_user_gets_notification(): void
{
$user = User::factory()->unverified()->create();
$this->actingAs($user)
->post(route('verification.send'))
->assertSessionHas('status', 'verification-link-sent');
}
}
@@ -55,4 +55,13 @@ class EmailVerificationTest extends TestCase
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
public function test_verified_user_is_redirected_from_prompt(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/verify-email');
$response->assertRedirect(route('dashboard', absolute: false));
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LoginRequestTest extends TestCase
{
use RefreshDatabase;
public function test_throttles_too_many_attempts(): void
{
$user = User::factory()->create();
for ($i = 0; $i < 6; $i++) {
$this->post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
}
$this->post('/login', [
'email' => $user->email,
'password' => 'password',
])->assertSessionHasErrors('email');
}
public function test_successful_login_redirects(): void
{
$user = User::factory()->create();
$this->post('/login', [
'email' => $user->email,
'password' => 'password',
])->assertRedirect(route('dashboard', absolute: false));
$this->assertAuthenticated();
}
}
@@ -0,0 +1,76 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class NewPasswordControllerTest extends TestCase
{
use RefreshDatabase;
public function test_password_reset_screen_can_be_rendered(): void
{
Notification::fake();
$user = User::factory()->create();
$this->post('/forgot-password', ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get('/reset-password/'.$notification->token);
$response->assertStatus(200);
return true;
});
}
public function test_password_can_be_reset(): void
{
Notification::fake();
$user = User::factory()->create();
$this->post('/forgot-password', ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('login'));
$this->assertTrue(
\Hash::check('new-password', $user->fresh()->password)
);
return true;
});
}
public function test_password_reset_fails_with_invalid_token(): void
{
$response = $this->post('/reset-password', [
'token' => 'invalid-token',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertSessionHasErrors('email');
}
public function test_password_reset_validates_required_fields(): void
{
$response = $this->post('/reset-password', []);
$response->assertSessionHasErrors(['token', 'email', 'password']);
}
}
@@ -0,0 +1,49 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class PasswordResetLinkControllerTest extends TestCase
{
use RefreshDatabase;
public function test_forgot_password_screen_can_be_rendered(): void
{
$response = $this->get('/forgot-password');
$response->assertStatus(200);
}
public function test_password_reset_link_can_be_requested(): void
{
Notification::fake();
$user = User::factory()->create();
$this->post('/forgot-password', ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class);
}
public function test_password_reset_link_fails_with_invalid_email(): void
{
$response = $this->post('/forgot-password', [
'email' => 'nonexistent@example.com',
]);
$response->assertSessionHasErrors('email');
}
public function test_password_reset_link_validates_email(): void
{
$response = $this->post('/forgot-password', [
'email' => '',
]);
$response->assertSessionHasErrors('email');
}
}
@@ -0,0 +1,64 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;
class VerifyEmailControllerTest extends TestCase
{
use RefreshDatabase;
public function test_already_verified_user_is_redirected(): void
{
$user = User::factory()->create();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
}
public function test_unverified_user_email_can_be_verified(): void
{
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
$this->assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
}
public function test_email_not_verified_with_invalid_hash(): void
{
$user = User::factory()->unverified()->create();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$this->actingAs($user)->get($verificationUrl);
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
}