CI/CD / preconditions (pull_request) Successful in 2s
Native JavaScript checks / Native launcher and release tests (pull_request) Successful in 1m17s
CI/CD / test_and_check_app (pull_request) Successful in 6m24s
CI/CD / Publish immutable image (pull_request) Skipped
macOS checks / Native Rust tests and unsigned build (pull_request) Successful in 1m27s
153 lines
4.6 KiB
PHP
153 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
use Laravel\Fortify\Features;
|
|
use Tests\TestCase;
|
|
|
|
class AuthenticationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_login_screen_can_be_rendered()
|
|
{
|
|
$response = $this->get(route('login'));
|
|
|
|
$response->assertOk();
|
|
}
|
|
|
|
public function test_users_can_authenticate_using_the_login_screen()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$this->assertAuthenticated();
|
|
$response->assertRedirect(route('dashboard', absolute: false));
|
|
}
|
|
|
|
public function test_users_who_must_change_their_password_are_redirected_to_the_dedicated_force_change_page_after_login()
|
|
{
|
|
$user = User::factory()->create([
|
|
'must_change_password_on_next_login' => true,
|
|
]);
|
|
|
|
$response = $this->followingRedirects()->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$this->assertAuthenticated();
|
|
$response->assertInertia(fn (Assert $page) => $page
|
|
->component('auth/ForcePasswordChange')
|
|
->where('status', 'You must change your password before continuing.'),
|
|
);
|
|
}
|
|
|
|
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge()
|
|
{
|
|
$this->skipUnlessFortifyFeature(Features::twoFactorAuthentication());
|
|
|
|
Features::twoFactorAuthentication([
|
|
'confirm' => true,
|
|
'confirmPassword' => true,
|
|
]);
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$user->forceFill([
|
|
'two_factor_secret' => encrypt('test-secret'),
|
|
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
|
|
'two_factor_confirmed_at' => now(),
|
|
])->save();
|
|
|
|
$response = $this->post(route('login'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertRedirect(route('two-factor.login'));
|
|
$response->assertSessionHas('login.id', $user->id);
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_users_can_not_authenticate_with_invalid_password()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'wrong-password',
|
|
])->assertSessionHasErrors('email');
|
|
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_users_can_logout()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)->post(route('logout'));
|
|
|
|
$this->assertGuest();
|
|
$response->assertRedirect(route('home'));
|
|
}
|
|
|
|
public function test_login_preserves_the_intended_destination_and_remember_choice(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->withSession(['url.intended' => route('dashboard')])
|
|
->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
'remember' => true,
|
|
])
|
|
->assertRedirect(route('dashboard'))
|
|
->assertCookie(Auth::guard('web')->getRecallerName());
|
|
|
|
$this->assertAuthenticatedAs($user);
|
|
}
|
|
|
|
public function test_workspace_start_without_a_valid_session_returns_to_dashboard_after_login(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
$this->get(route('dashboard'))
|
|
->assertRedirect(route('login'))
|
|
->assertSessionHas('url.intended', route('dashboard'));
|
|
|
|
$this->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'password',
|
|
])->assertRedirect(route('dashboard'));
|
|
|
|
$this->assertAuthenticatedAs($user);
|
|
$this->get(route('dashboard'))
|
|
->assertOk()
|
|
->assertInertia(fn (Assert $page) => $page->component('Dashboard'));
|
|
}
|
|
|
|
public function test_users_are_rate_limited()
|
|
{
|
|
$user = User::factory()->create();
|
|
|
|
RateLimiter::increment(md5('login'.implode('|', [$user->email, '127.0.0.1'])), amount: 5);
|
|
|
|
$response = $this->post(route('login.store'), [
|
|
'email' => $user->email,
|
|
'password' => 'wrong-password',
|
|
]);
|
|
|
|
$response->assertTooManyRequests();
|
|
}
|
|
}
|