Files
portal-api/tests/Feature/Api/Service/VmOwnershipTest.php
T
2026-09-23 15:17:18 +07:00

39 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Models\Customer;
use App\Models\ModuleVmware;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class VmOwnershipTest extends TestCase
{
use RefreshDatabase;
public function test_customer_cannot_read_power_state_of_another_customers_vm_by_numeric_id(): void
{
$owner = Customer::factory()->create();
$attacker = Customer::factory()->create();
$vm = ModuleVmware::create([
'name' => 'owner-vm',
'customer_id' => $owner->id,
'vmware_id' => 'vm-1001',
'status' => 1,
]);
$response = $this->actingAs($attacker, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()])
->getJson('/v1/system/vm-power-state?product=' . $vm->id);
// Query đã lọc theo customer_id nên VM của người khác không được tìm thấy (404)
// thay vì tìm thấy rồi mới bị chặn bởi check quyền sở hữu phía sau (403) — cả hai đều
// an toàn, nhưng 404 xác nhận query đã được scope đúng ngay từ đầu (defense-in-depth).
$response->assertStatus(404);
}
}