CluPilotCloud/tests/Feature/Admin/HostManagementTest.php

123 lines
4.4 KiB
PHP

<?php
use App\Livewire\Admin\ConfirmRemoveHost;
use App\Livewire\Admin\HostCreate;
use App\Livewire\Admin\HostDetail;
use App\Models\Host;
use App\Models\ProvisioningRun;
use App\Models\User;
use App\Provisioning\Jobs\AdvanceRunJob;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
function admin(): User
{
return User::factory()->create(['is_admin' => true]);
}
it('gates the add-host page', function () {
$this->get(route('admin.hosts.create'))->assertRedirect('/login');
$this->actingAs(User::factory()->create(['is_admin' => false]))->get(route('admin.hosts.create'))->assertForbidden();
$this->actingAs(admin())->get(route('admin.hosts.create'))->assertOk()->assertSee(__('hosts.create_title'));
});
it('gates the host detail page', function () {
$host = Host::factory()->create();
$this->get(route('admin.hosts.show', $host))->assertRedirect('/login');
$this->actingAs(User::factory()->create(['is_admin' => false]))->get(route('admin.hosts.show', $host))->assertForbidden();
$this->actingAs(admin())->get(route('admin.hosts.show', $host))->assertOk()->assertSee($host->name);
});
it('lists real hosts for admins', function () {
Host::factory()->active()->create(['name' => 'pve-zzz-1']);
$this->actingAs(admin())->get(route('admin.hosts'))->assertOk()->assertSee('pve-zzz-1');
});
it('creates a host and starts onboarding with an encrypted password', function () {
Queue::fake();
Livewire::actingAs(admin())
->test(HostCreate::class)
->set('name', 'pve-fsn-7')
->set('datacenter', 'fsn')
->set('public_ip', '203.0.113.7')
->set('root_password', 'supersecret')
->call('save')
->assertRedirect();
$host = Host::query()->where('name', 'pve-fsn-7')->first();
expect($host)->not->toBeNull()->and($host->status)->toBe('pending');
$run = ProvisioningRun::query()->where('subject_id', $host->id)->first();
expect($run)->not->toBeNull()
->and($run->pipeline)->toBe('host')
->and(Crypt::decryptString($run->context('root_password')))->toBe('supersecret');
Queue::assertPushed(AdvanceRunJob::class);
});
it('rejects a duplicate public ip', function () {
Host::factory()->create(['public_ip' => '203.0.113.99']);
Livewire::actingAs(admin())
->test(HostCreate::class)
->set('name', 'pve-dup')
->set('datacenter', 'fsn')
->set('public_ip', '203.0.113.99')
->set('root_password', 'supersecret')
->call('save')
->assertHasErrors(['public_ip']);
});
it('validates the add-host form', function () {
Livewire::actingAs(admin())
->test(HostCreate::class)
->set('name', '')
->set('public_ip', 'not-an-ip')
->set('root_password', 'short')
->call('save')
->assertHasErrors(['name', 'public_ip', 'root_password']);
});
it('retries a failed run from the detail page', function () {
Queue::fake();
$host = Host::factory()->create(['status' => 'error']);
$run = ProvisioningRun::factory()->forHost($host)->create([
'status' => 'failed', 'current_step' => 3, 'error' => 'boom',
'started_at' => now()->subHour(),
]);
Livewire::actingAs(admin())->test(HostDetail::class, ['host' => $host])->call('retry');
$run->refresh();
expect($run->status)->toBe('running')
->and($run->error)->toBeNull()
->and($run->started_at->greaterThan(now()->subMinute()))->toBeTrue()
->and($host->fresh()->status)->toBe('onboarding');
Queue::assertPushed(AdvanceRunJob::class);
});
it('removes the host record without wiping the server', function () {
$host = Host::factory()->create();
ProvisioningRun::factory()->forHost($host)->create();
Livewire::actingAs(admin())
->test(ConfirmRemoveHost::class, ['uuid' => $host->uuid])
->call('remove')
->assertRedirect(route('admin.hosts'));
expect(Host::query()->find($host->id))->toBeNull();
});
it('renders the live stepper for a running host', function () {
$host = Host::factory()->create(['status' => 'onboarding']);
ProvisioningRun::factory()->forHost($host)->create(['status' => 'running', 'current_step' => 2]);
$this->actingAs(admin())->get(route('admin.hosts.show', $host))
->assertOk()
->assertSee(__('hosts.step.prepare_base_system'))
->assertSee(__('hosts.progress'));
});