Files

145 lines
5.3 KiB
PHP

<?php
namespace Tests\Feature\Superuser;
use App\Enums\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\Concerns\InspectsHtml;
use Tests\TestCase;
class ResetPasswordTest extends TestCase
{
use InspectsHtml;
use RefreshDatabase;
/** @return array<string, array{Role}> */
public static function otherRoles(): array
{
return [
'admin' => [Role::Admin],
'operator' => [Role::Operator],
'dosen' => [Role::Dosen],
'mahasiswa' => [Role::Mahasiswa],
];
}
private function superuser(): User
{
return $this->userWithRole(Role::Superuser, ['username' => 'superuser']);
}
private function target(): User
{
return $this->userWithRole(Role::Mahasiswa, ['name' => 'Rina Lestari', 'username' => 'D1041191001'], ['nim' => 'D1041191001']);
}
public function test_guest_is_redirected_to_login(): void
{
$this->get(route('superuser.reset-password'))->assertRedirect(route('login'));
$this->put(route('superuser.reset-password.update'))->assertRedirect(route('login'));
}
#[DataProvider('otherRoles')]
public function test_other_roles_are_forbidden(Role $role): void
{
$user = $this->userWithRole($role);
$this->actingAs($user)->get(route('superuser.reset-password'))->assertForbidden();
$this->actingAs($user)->put(route('superuser.reset-password.update'), ['username' => $user->username, 'acak' => '1'])->assertForbidden();
}
public function test_superuser_sees_the_page_and_menu_item(): void
{
$response = $this->actingAs($this->superuser())->get(route('superuser.reset-password'))->assertOk();
$headings = $this->dom($response)->query('//h1');
$this->assertSame(1, $headings->length);
$this->assertSame('Atur ulang kata sandi', trim($headings->item(0)->textContent));
$this->assertSame(1, $this->dom($response)->query('//a[@aria-current="page" and contains(@href, "reset-kata-sandi")]')->length);
}
public function test_search_shows_the_account(): void
{
$this->target();
$this->actingAs($this->superuser())
->get(route('superuser.reset-password', ['username' => 'D1041191001']))
->assertOk()
->assertSee('Rina Lestari')
->assertSee('Simpan kata sandi');
}
public function test_search_for_unknown_account_says_so(): void
{
$this->actingAs($this->superuser())
->get(route('superuser.reset-password', ['username' => 'tidak-ada']))
->assertOk()
->assertSee('Akun tidak ditemukan')
->assertDontSee('Simpan kata sandi');
}
public function test_superuser_sets_a_new_password(): void
{
$target = $this->target();
$this->actingAs($this->superuser())
->put(route('superuser.reset-password.update'), [
'username' => 'D1041191001',
'password' => 'SandiBaru2026',
'password_confirmation' => 'SandiBaru2026',
])
->assertRedirect(route('superuser.reset-password'))
->assertSessionHas('status', 'Kata sandi Rina Lestari berhasil diatur ulang.');
$this->assertTrue(Hash::check('SandiBaru2026', $target->fresh()->password));
}
public function test_random_password_is_shown_once_and_works(): void
{
$target = $this->target();
$superuser = $this->superuser();
$this->actingAs($superuser)
->put(route('superuser.reset-password.update'), ['username' => 'D1041191001', 'acak' => '1'])
->assertRedirect(route('superuser.reset-password'));
$password = session('generated_password')['password'];
$this->assertSame(10, strlen($password));
$this->assertTrue(Hash::check($password, $target->fresh()->password));
$this->actingAs($superuser)->get(route('superuser.reset-password'))->assertSee($password);
$this->actingAs($superuser)->get(route('superuser.reset-password'))->assertDontSee($password);
}
public function test_weak_or_mismatched_password_is_rejected(): void
{
$target = $this->target();
$before = $target->password;
$superuser = $this->superuser();
foreach ([
['password' => 'pendek1', 'password_confirmation' => 'pendek1'],
['password' => 'hanyahuruf', 'password_confirmation' => 'hanyahuruf'],
['password' => 'SandiBaru2026', 'password_confirmation' => 'SandiLain2026'],
['password' => 'Sandi Baru 2026', 'password_confirmation' => 'Sandi Baru 2026'],
] as $input) {
$this->actingAs($superuser)
->put(route('superuser.reset-password.update'), ['username' => 'D1041191001', ...$input])
->assertSessionHasErrors('password');
}
$this->assertSame($before, $target->fresh()->password);
}
public function test_unknown_account_cannot_be_reset(): void
{
$this->actingAs($this->superuser())
->put(route('superuser.reset-password.update'), ['username' => 'tidak-ada', 'acak' => '1'])
->assertSessionHasErrors('username')
->assertSessionMissing('generated_password');
}
}