Commit awal: prototipe UI SIAKAD Untan
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\Concerns\InspectsHtml;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LoginTest extends TestCase
|
||||
{
|
||||
use InspectsHtml;
|
||||
use RefreshDatabase;
|
||||
|
||||
/** @return array<string, array{Role}> */
|
||||
public static function roles(): array
|
||||
{
|
||||
return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases()));
|
||||
}
|
||||
|
||||
public function test_login_page_renders_for_guests(): void
|
||||
{
|
||||
$this->get(route('login'))
|
||||
->assertOk()
|
||||
->assertSee('Masuk ke SIAKAD');
|
||||
}
|
||||
|
||||
public function test_login_page_uses_indonesian_html_language(): void
|
||||
{
|
||||
$this->get(route('login'))->assertSee('<html lang="id"', false);
|
||||
}
|
||||
|
||||
public function test_login_form_has_the_required_fields(): void
|
||||
{
|
||||
$xpath = $this->dom($this->get(route('login')));
|
||||
|
||||
$form = $xpath->query("//form[@action='".route('login.store')."']");
|
||||
$this->assertSame(1, $form->length);
|
||||
$this->assertSame('post', strtolower($this->attr($form, 'method')));
|
||||
$formText = $this->textOf($form->item(0));
|
||||
|
||||
$username = $xpath->query("//form//input[@name='username']");
|
||||
$this->assertSame(1, $username->length);
|
||||
$this->assertSame('username', $this->attr($username, 'autocomplete'));
|
||||
|
||||
$password = $xpath->query("//form//input[@name='password']");
|
||||
$this->assertSame(1, $password->length);
|
||||
$this->assertSame('password', $this->attr($password, 'type'));
|
||||
$this->assertSame('current-password', $this->attr($password, 'autocomplete'));
|
||||
|
||||
$this->assertSame(1, $xpath->query("//form//input[@name='remember'][@type='checkbox']")->length);
|
||||
$this->assertSame(1, $xpath->query("//form//input[@name='_token']")->length);
|
||||
|
||||
$this->assertStringContainsString('Username / NIM / NIP', $formText);
|
||||
$this->assertStringContainsString('Kata sandi', $formText);
|
||||
$this->assertStringContainsString('Ingat saya', $formText);
|
||||
|
||||
$submit = $xpath->query("//form//button[@type='submit']");
|
||||
$this->assertSame(1, $submit->length);
|
||||
$this->assertSame('Masuk', $this->textOf($submit->item(0)));
|
||||
}
|
||||
|
||||
public function test_login_page_has_no_dead_links(): void
|
||||
{
|
||||
$response = $this->get(route('login'));
|
||||
|
||||
$response->assertDontSee('Lupa', false);
|
||||
$response->assertDontSee('Daftar', false);
|
||||
$response->assertDontSee('href="#"', false);
|
||||
$this->assertSame(0, $this->dom($response)->query('//a[not(@href)]')->length);
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_each_role_logs_in_and_lands_on_its_own_dashboard(Role $role): void
|
||||
{
|
||||
$user = $this->userWithRole($role, ['username' => 'akun-'.$role->value]);
|
||||
|
||||
$response = $this->post(route('login.store'), [
|
||||
'username' => 'akun-'.$role->value,
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route($role->dashboardRoute()));
|
||||
$this->assertAuthenticatedAs($user);
|
||||
$this->assertNotNull($user->fresh()->last_login_at);
|
||||
}
|
||||
|
||||
public function test_login_redirects_back_to_the_intended_page(): void
|
||||
{
|
||||
$this->userWithRole(Role::Dosen, ['username' => 'dosen']);
|
||||
|
||||
$this->get(route('dosen.dashboard'))->assertRedirect(route('login'));
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password'])
|
||||
->assertRedirect(route('dosen.dashboard'));
|
||||
}
|
||||
|
||||
public function test_wrong_password_is_rejected_with_a_generic_message(): void
|
||||
{
|
||||
$this->userWithRole(Role::Admin, ['username' => 'admin']);
|
||||
|
||||
$this->from(route('login'))
|
||||
->post(route('login.store'), ['username' => 'admin', 'password' => 'salah'])
|
||||
->assertRedirect(route('login'))
|
||||
->assertSessionHasErrors(['username' => trans('auth.failed')]);
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_unknown_username_gets_the_same_message_as_a_wrong_password(): void
|
||||
{
|
||||
$this->post(route('login.store'), ['username' => 'tidak-ada', 'password' => 'password'])
|
||||
->assertSessionHasErrors(['username' => trans('auth.failed')]);
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_failed_login_shows_the_error_on_the_page_accessibly(): void
|
||||
{
|
||||
$this->userWithRole(Role::Admin, ['username' => 'admin']);
|
||||
|
||||
$response = $this->followingRedirects()
|
||||
->from(route('login'))
|
||||
->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertSee(trans('auth.failed'));
|
||||
|
||||
$alerts = $this->dom($response)
|
||||
->query("//*[@role='alert' or @aria-live][contains(., '".trans('auth.failed')."')]");
|
||||
$this->assertGreaterThanOrEqual(1, $alerts->length, 'Pesan galat harus berada dalam wilayah role=alert atau aria-live.');
|
||||
}
|
||||
|
||||
public function test_inactive_account_cannot_log_in_and_gets_the_same_generic_message(): void
|
||||
{
|
||||
$this->userWithRole(Role::Mahasiswa, ['username' => 'nonaktif', 'is_active' => false]);
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'nonaktif', 'password' => 'password'])
|
||||
->assertSessionHasErrors(['username' => trans('auth.failed')]);
|
||||
|
||||
$this->assertGuest();
|
||||
|
||||
// Pesan sama persis dengan kata sandi salah pada akun aktif; status akun tidak bocor.
|
||||
$this->assertSame(trans('auth.failed'), session('errors')->first('username'));
|
||||
$this->assertStringNotContainsStringIgnoringCase('aktif', session('errors')->first('username'));
|
||||
}
|
||||
|
||||
public function test_required_fields_are_validated_in_indonesian(): void
|
||||
{
|
||||
$this->post(route('login.store'), [])
|
||||
->assertSessionHasErrors(['username', 'password']);
|
||||
|
||||
$this->assertStringContainsString('wajib diisi', session('errors')->first('username'));
|
||||
$this->assertStringContainsStringIgnoringCase('kata sandi', session('errors')->first('password'));
|
||||
}
|
||||
|
||||
public function test_login_is_throttled_after_five_failed_attempts(): void
|
||||
{
|
||||
Event::fake([Lockout::class]);
|
||||
$this->userWithRole(Role::Admin, ['username' => 'admin']);
|
||||
|
||||
for ($attempt = 1; $attempt <= 5; $attempt++) {
|
||||
$this->post(route('login.store'), ['username' => 'admin', 'password' => 'salah'])
|
||||
->assertSessionHasErrors(['username' => trans('auth.failed')]);
|
||||
}
|
||||
|
||||
// Percobaan ke-6 terkunci, bahkan dengan kata sandi yang benar.
|
||||
$this->post(route('login.store'), ['username' => 'admin', 'password' => 'password'])
|
||||
->assertSessionHasErrors('username');
|
||||
|
||||
$this->assertStringContainsString('Terlalu banyak percobaan', session('errors')->first('username'));
|
||||
$this->assertStringContainsString('detik', session('errors')->first('username'));
|
||||
$this->assertGuest();
|
||||
Event::assertDispatched(Lockout::class);
|
||||
}
|
||||
|
||||
public function test_throttle_is_per_username_and_case_insensitive(): void
|
||||
{
|
||||
$this->userWithRole(Role::Admin, ['username' => 'admin']);
|
||||
$this->userWithRole(Role::Dosen, ['username' => 'dosen']);
|
||||
|
||||
foreach (['admin', 'ADMIN', 'Admin', 'admin', 'AdMiN'] as $username) {
|
||||
$this->post(route('login.store'), ['username' => $username, 'password' => 'salah']);
|
||||
}
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'admin', 'password' => 'password'])
|
||||
->assertSessionHasErrors('username');
|
||||
$this->assertGuest();
|
||||
|
||||
// Username lain tidak ikut terkunci.
|
||||
$this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password'])
|
||||
->assertRedirect(route('dosen.dashboard'));
|
||||
}
|
||||
|
||||
public function test_four_failures_do_not_lock_and_success_clears_the_counter(): void
|
||||
{
|
||||
$this->userWithRole(Role::Admin, ['username' => 'admin']);
|
||||
$key = 'admin|127.0.0.1';
|
||||
|
||||
for ($attempt = 1; $attempt <= 4; $attempt++) {
|
||||
$this->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']);
|
||||
}
|
||||
$this->assertSame(4, RateLimiter::attempts($key));
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'admin', 'password' => 'password'])
|
||||
->assertRedirect(route('admin.dashboard'));
|
||||
|
||||
$this->assertSame(0, RateLimiter::attempts($key));
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_authenticated_users_are_redirected_away_from_the_login_page(Role $role): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole($role))
|
||||
->get(route('login'))
|
||||
->assertRedirect(route('dashboard'));
|
||||
}
|
||||
|
||||
public function test_authenticated_users_cannot_post_to_login_again(): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole(Role::Admin))
|
||||
->post(route('login.store'), ['username' => 'x', 'password' => 'y'])
|
||||
->assertRedirect(route('dashboard'));
|
||||
}
|
||||
|
||||
public function test_an_account_deactivated_mid_session_is_logged_out_on_the_next_request(): void
|
||||
{
|
||||
$user = $this->userWithRole(Role::Dosen, ['username' => 'dosen']);
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password']);
|
||||
$this->assertAuthenticatedAs($user);
|
||||
$this->get(route('dosen.dashboard'))->assertOk();
|
||||
|
||||
User::whereKey($user->id)->update(['is_active' => false]);
|
||||
// Permintaan berikutnya berjalan di proses PHP baru; di tes guard harus dilepas agar akun dibaca ulang dari DB.
|
||||
Auth::forgetGuards();
|
||||
|
||||
$this->get(route('dosen.dashboard'))
|
||||
->assertRedirect(route('login'))
|
||||
->assertSessionHasErrors(['username' => trans('auth.inactive')]);
|
||||
|
||||
$this->assertGuest();
|
||||
$this->get(route('dosen.dashboard'))->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_deactivated_account_of_every_role_is_logged_out_on_a_protected_route(Role $role): void
|
||||
{
|
||||
$user = $this->userWithRole($role, ['is_active' => false]);
|
||||
|
||||
$this->actingAs($user)->get(route('dashboard'))
|
||||
->assertRedirect(route('login'));
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_root_redirects_guests_to_the_login_page(): void
|
||||
{
|
||||
$this->get('/')->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_root_sends_every_signed_in_role_to_its_dashboard(Role $role): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole($role));
|
||||
|
||||
$this->get('/')->assertRedirect(route('dashboard'));
|
||||
$this->get(route('dashboard'))->assertRedirect(route($role->dashboardRoute()));
|
||||
$this->followingRedirects()->get('/')->assertOk()->assertSee('Selamat Datang,');
|
||||
}
|
||||
|
||||
public function test_demo_accounts_panel_is_absent_outside_local(): void
|
||||
{
|
||||
foreach (['testing', 'production', 'staging'] as $environment) {
|
||||
$this->app->detectEnvironment(fn (): string => $environment);
|
||||
|
||||
$this->get(route('login'))
|
||||
->assertOk()
|
||||
->assertDontSee('Akun demo');
|
||||
}
|
||||
}
|
||||
|
||||
public function test_demo_accounts_panel_lists_the_five_roles_in_local(): void
|
||||
{
|
||||
$this->app->detectEnvironment(fn (): string => 'local');
|
||||
|
||||
$response = $this->get(route('login'))->assertOk()->assertSee('Akun demo');
|
||||
$xpath = $this->dom($response);
|
||||
|
||||
$details = $xpath->query('//details');
|
||||
$this->assertSame(1, $details->length);
|
||||
$panel = $this->textOf($details->item(0));
|
||||
|
||||
foreach (Role::cases() as $role) {
|
||||
$this->assertStringContainsString($role->label(), $panel);
|
||||
$this->assertStringContainsString($role->value, $panel);
|
||||
}
|
||||
$this->assertSame(5, $xpath->query('//details//button')->length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user