This commit is contained in:
baodev0
2026-10-08 11:25:28 +07:00
parent 1544629a1f
commit 8651dde6b1
45 changed files with 1543 additions and 357 deletions
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin\Customer;
use App\Models\Customer;
use App\Models\ModuleVmware;
use App\Models\Service;
use App\Models\User;
use App\Models\VirtualMachines;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class AssignServiceVmTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->assignRole('Full Administrator');
}
private function vm(string $vmId): VirtualMachines
{
Schema::disableForeignKeyConstraints();
return VirtualMachines::create([
'uuid' => (string) Str::uuid(),
'vm_id' => $vmId,
'name' => "name-{$vmId}",
'cpu' => 2,
'ram_mb' => 2048,
'is_template' => 0,
'is_system' => 0,
'datacenter_id' => 1,
'host_id' => 1,
]);
}
private function assign(Customer $customer, Service $service, VirtualMachines $vm)
{
return $this->actingAs($this->admin, 'admin')->putJson(
"/api/v1/administrator/customers/{$customer->uuid}/services/{$service->id}",
['virtual_machine_id' => $vm->id],
);
}
public function test_assigns_and_reassigns_vm_and_rejects_vm_of_another_service(): void
{
$customer = Customer::factory()->create();
$service = Service::create(['customer_id' => $customer->id, 'status' => 'active']);
$first = $this->vm('vm-1');
$second = $this->vm('vm-2');
$this->assign($customer, $service, $first)->assertOk();
$service->refresh();
$moduleId = $service->vm_id;
$this->assertNotNull($moduleId);
$this->assertSame($service->id, (int) ModuleVmware::find($moduleId)->service_id);
$this->assign($customer, $service, $second)->assertOk();
$service->refresh();
$this->assertSame($moduleId, $service->vm_id);
$this->assertSame('vm-2', ModuleVmware::find($moduleId)->vmware_id);
$other = Service::create(['customer_id' => $customer->id, 'status' => 'active']);
$this->assign($customer, $other, $second)->assertStatus(422);
$this->assertNull($other->refresh()->vm_id);
}
}
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Service;
use App\Http\Services\vCenter as VCenterService;
use App\Models\Customer;
use App\Models\ModuleVmware;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Service;
use App\Models\vCenter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
class ConsoleTicketResolvesVmTest extends TestCase
{
use RefreshDatabase;
public function test_console_ticket_uses_service_vm_when_order_is_soft_deleted(): void
{
$customer = Customer::factory()->create();
$order = Order::create(['order_num' => 'ORD-' . Str::random(10), 'customer_id' => $customer->id]);
$orderItem = OrderItem::create(['order_id' => $order->id]);
$vcenter = vCenter::create(['host' => 'vc.test', 'user' => 'u', 'password' => 'p', 'status' => 1]);
$vm = ModuleVmware::create([
'name' => 'vm',
'customer_id' => $customer->id,
'order_id' => $order->id,
'vmware_id' => 'vm-1',
'vcenter_id' => $vcenter->id,
'status' => 1,
]);
$service = Service::create([
'customer_id' => $customer->id,
'order_item_id' => $orderItem->id,
'vm_id' => $vm->id,
'status' => 'active',
]);
$order->delete();
$this->mock(VCenterService::class)->shouldReceive('loginServer')->once()->andReturnFalse();
$this->actingAs($customer, 'client')
->withHeaders(['x-access-token' => $this->buildClientAccessToken()])
->getJson("/v1/client/client-service/{$service->uuid}/console-ticket")
->assertOk()
->assertJsonPath('message', 'Không thể đăng nhập vào vCenter');
}
}