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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Enums\Role;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\Concerns\InspectsHtml;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LogoutTest 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()));
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_post_logout_ends_the_session_and_redirects_to_login(Role $role): void
|
||||
{
|
||||
$user = $this->userWithRole($role, ['username' => 'akun']);
|
||||
|
||||
$this->post(route('login.store'), ['username' => 'akun', 'password' => 'password']);
|
||||
$this->assertAuthenticatedAs($user);
|
||||
$oldSessionId = session()->getId();
|
||||
|
||||
$this->post(route('logout'))->assertRedirect(route('login'));
|
||||
|
||||
$this->assertGuest();
|
||||
$this->assertNotSame($oldSessionId, session()->getId(), 'Id sesi harus diganti saat keluar.');
|
||||
$this->get(route($role->dashboardRoute()))->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_logout_invalidates_session_data(): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole(Role::Admin))
|
||||
->withSession(['rahasia' => 'nilai'])
|
||||
->post(route('logout'));
|
||||
|
||||
$this->assertNull(session('rahasia'));
|
||||
}
|
||||
|
||||
public function test_logout_is_post_only(): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole(Role::Admin));
|
||||
|
||||
$this->get('/logout')->assertStatus(405);
|
||||
$this->assertAuthenticated();
|
||||
}
|
||||
|
||||
public function test_guests_posting_to_logout_are_sent_to_login(): void
|
||||
{
|
||||
$this->post(route('logout'))->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
#[DataProvider('roles')]
|
||||
public function test_sidebar_has_a_logout_form_posting_to_the_logout_route(Role $role): void
|
||||
{
|
||||
$response = $this->actingAs($this->userWithRole($role))->get(route($role->dashboardRoute()));
|
||||
$xpath = $this->dom($response);
|
||||
$formPath = "//aside//form[@action='".route('logout')."']";
|
||||
|
||||
$form = $xpath->query($formPath);
|
||||
$this->assertSame(1, $form->length);
|
||||
$this->assertSame('post', strtolower($this->attr($form, 'method')));
|
||||
$this->assertSame(1, $xpath->query($formPath."//input[@name='_token']")->length);
|
||||
$this->assertSame('Keluar', $this->textOf($xpath->query($formPath.'//button')->item(0)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Enums\Role;
|
||||
use App\Http\Middleware\EnsureUserHasRole;
|
||||
use Illuminate\Foundation\Http\Kernel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RoleAccessTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/** @return array<string, array{Role, Role}> semua pasangan (akun, dashboard yang dibuka) */
|
||||
public static function matrix(): array
|
||||
{
|
||||
$cases = [];
|
||||
|
||||
foreach (Role::cases() as $actor) {
|
||||
foreach (Role::cases() as $target) {
|
||||
$cases["{$actor->value} membuka {$target->value}"] = [$actor, $target];
|
||||
}
|
||||
}
|
||||
|
||||
return $cases;
|
||||
}
|
||||
|
||||
/** @return array<string, array{string, string}> */
|
||||
public static function protectedRoutes(): array
|
||||
{
|
||||
return [
|
||||
'dashboard' => ['get', '/dashboard'],
|
||||
'logout' => ['post', '/logout'],
|
||||
'superuser dashboard' => ['get', '/superuser/dashboard'],
|
||||
'admin dashboard' => ['get', '/admin/dashboard'],
|
||||
'operator dashboard' => ['get', '/operator/dashboard'],
|
||||
'dosen dashboard' => ['get', '/dosen/dashboard'],
|
||||
'mahasiswa dashboard' => ['get', '/mahasiswa/dashboard'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('matrix')]
|
||||
public function test_a_user_can_only_open_the_dashboard_of_their_own_role(Role $actor, Role $target): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole($actor));
|
||||
|
||||
$response = $this->get(route($target->dashboardRoute()));
|
||||
|
||||
if ($actor === $target) {
|
||||
$response->assertOk();
|
||||
} else {
|
||||
$response->assertForbidden();
|
||||
}
|
||||
}
|
||||
|
||||
#[DataProvider('protectedRoutes')]
|
||||
public function test_guests_are_redirected_to_login_from_every_protected_route(string $method, string $uri): void
|
||||
{
|
||||
$this->{$method}($uri)->assertRedirect(route('login'));
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_route_names_and_paths_match_the_contract(): void
|
||||
{
|
||||
$this->assertSame('/login', route('login', [], false));
|
||||
$this->assertSame('/logout', route('logout', [], false));
|
||||
$this->assertSame('/dashboard', route('dashboard', [], false));
|
||||
|
||||
foreach (Role::cases() as $role) {
|
||||
$this->assertSame("/{$role->value}/dashboard", route($role->dashboardRoute(), [], false));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_every_role_group_is_protected_by_auth_active_and_its_own_role_middleware(): void
|
||||
{
|
||||
foreach (Role::cases() as $role) {
|
||||
$middleware = Route::getRoutes()->getByName($role->dashboardRoute())->gatherMiddleware();
|
||||
|
||||
$this->assertContains('auth', $middleware);
|
||||
$this->assertContains('active', $middleware);
|
||||
$this->assertContains("role:{$role->value}", $middleware);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_dashboard_redirect_sends_each_role_to_its_own_dashboard(): void
|
||||
{
|
||||
foreach (Role::cases() as $role) {
|
||||
$this->actingAs($this->userWithRole($role, ['username' => 'akun-'.$role->value]))
|
||||
->get(route('dashboard'))
|
||||
->assertRedirect(route($role->dashboardRoute()));
|
||||
}
|
||||
}
|
||||
|
||||
public function test_forbidden_page_is_the_indonesian_error_view(): void
|
||||
{
|
||||
$this->actingAs($this->userWithRole(Role::Mahasiswa))
|
||||
->get(route('admin.dashboard'))
|
||||
->assertForbidden()
|
||||
->assertSee('Akses ditolak')
|
||||
->assertSee('Kode galat');
|
||||
}
|
||||
|
||||
public function test_role_middleware_accepts_several_roles_and_has_no_implicit_superuser_bypass(): void
|
||||
{
|
||||
Route::middleware(['web', 'auth', EnsureUserHasRole::class.':admin,operator'])
|
||||
->get('/_uji/multi-role', fn (): string => 'ok');
|
||||
|
||||
$this->actingAs($this->userWithRole(Role::Admin))->get('/_uji/multi-role')->assertOk();
|
||||
$this->actingAs($this->userWithRole(Role::Operator))->get('/_uji/multi-role')->assertOk();
|
||||
$this->actingAs($this->userWithRole(Role::Superuser))->get('/_uji/multi-role')->assertForbidden();
|
||||
$this->actingAs($this->userWithRole(Role::Dosen))->get('/_uji/multi-role')->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_role_middleware_alias_and_active_alias_are_registered(): void
|
||||
{
|
||||
$aliases = app(Kernel::class)->getMiddlewareAliases();
|
||||
|
||||
$this->assertSame(EnsureUserHasRole::class, $aliases['role']);
|
||||
$this->assertArrayHasKey('active', $aliases);
|
||||
}
|
||||
|
||||
public function test_unknown_pages_return_the_404_view_for_guests_and_users(): void
|
||||
{
|
||||
$this->get('/halaman-tidak-ada')->assertNotFound()->assertSee('Kode galat');
|
||||
|
||||
$this->actingAs($this->userWithRole(Role::Admin))
|
||||
->get('/halaman-tidak-ada')
|
||||
->assertNotFound()
|
||||
->assertSee('Kode galat');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user