81 lines
2.5 KiB
PHP
81 lines
2.5 KiB
PHP
<?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);
|
|
}
|
|
}
|