admin add user, password

This commit is contained in:
Saufi
2026-05-26 12:02:16 +08:00
parent 6ca0bd82fd
commit 5fbededf8f
12 changed files with 531 additions and 0 deletions

View File

@@ -6,7 +6,9 @@ use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Tests\TestCase;
class RateMasWorkflowTest extends TestCase
@@ -87,6 +89,18 @@ class RateMasWorkflowTest extends TestCase
$this->get('/admin/ratemas/upload')->assertForbidden();
}
public function test_regular_user_cannot_open_admin_user_management(): void
{
$this->actingAs(User::create([
'name' => 'User Cukai',
'username' => 'cukai',
'email' => 'cukai@example.local',
'password' => '123',
]));
$this->get('/admin/users')->assertForbidden();
}
public function test_admin_can_upload_csv_and_create_year_table(): void
{
$this->actingAs(User::create([
@@ -116,6 +130,116 @@ class RateMasWorkflowTest extends TestCase
->assertSee('Siti Aminah');
}
public function test_authenticated_user_can_update_email_and_password(): void
{
$user = User::create([
'name' => 'User Cukai',
'username' => 'cukai',
'email' => 'cukai@example.local',
'password' => '12345678',
]);
$this->actingAs($user);
$this->put('/profile/email', [
'email' => 'baru@example.local',
])->assertRedirect();
$this->assertSame('baru@example.local', $user->fresh()->email);
$this->put('/profile/password', [
'current_password' => '12345678',
'password' => 'password-baru',
'password_confirmation' => 'password-baru',
])->assertRedirect();
$this->assertTrue(Hash::check('password-baru', $user->fresh()->password));
}
public function test_guest_can_request_and_complete_password_reset(): void
{
$user = User::create([
'name' => 'User Cukai',
'username' => 'cukai',
'email' => 'cukai@example.local',
'password' => '12345678',
]);
$this->post('/forgot-password', [
'email' => $user->email,
])->assertRedirect();
$this->assertDatabaseHas('password_reset_tokens', [
'email' => $user->email,
]);
$token = Str::random(64);
DB::table('password_reset_tokens')->updateOrInsert([
'email' => $user->email,
], [
'token' => Hash::make($token),
'created_at' => now(),
]);
$this->post('/reset-password', [
'token' => $token,
'email' => $user->email,
'password' => 'reset-baru',
'password_confirmation' => 'reset-baru',
])->assertRedirect('/login');
$this->assertTrue(Hash::check('reset-baru', $user->fresh()->password));
$this->assertDatabaseMissing('password_reset_tokens', [
'email' => $user->email,
]);
}
public function test_admin_can_create_regular_user_by_default(): void
{
$this->actingAs(User::create([
'name' => 'Admin',
'username' => 'admin',
'role' => 'admin',
'email' => 'admin@example.local',
'password' => 'admin123',
]));
$this->post('/admin/users', [
'name' => 'User Baru',
'username' => 'userbaru',
'email' => 'userbaru@example.local',
'password' => 'password123',
'password_confirmation' => 'password123',
])->assertRedirect('/admin/users');
$user = User::where('username', 'userbaru')->firstOrFail();
$this->assertSame('user', $user->role);
$this->assertTrue(Hash::check('password123', $user->password));
}
public function test_admin_can_create_new_admin_user(): void
{
$this->actingAs(User::create([
'name' => 'Admin',
'username' => 'admin',
'role' => 'admin',
'email' => 'admin@example.local',
'password' => 'admin123',
]));
$this->post('/admin/users', [
'name' => 'Admin Baru',
'username' => 'adminbaru',
'email' => 'adminbaru@example.local',
'password' => 'password123',
'password_confirmation' => 'password123',
'role' => 'admin',
])->assertRedirect('/admin/users');
$this->assertTrue(User::where('username', 'adminbaru')->firstOrFail()->isAdmin());
}
private function createRateMasTable(string $table): void
{
Schema::create($table, function ($table) {