coolify/tests/Feature/HetznerServerCreationTest.php

188 lines
5.5 KiB
PHP

<?php
use App\Livewire\Server\New\ByHetzner;
use App\Models\Team;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
// Note: Full Livewire integration tests require database setup
// These tests verify the SSH key merging logic and public_net configuration
it('validates public_net configuration with IPv4 and IPv6 enabled', function () {
$enableIpv4 = true;
$enableIpv6 = true;
$publicNet = [
'enable_ipv4' => $enableIpv4,
'enable_ipv6' => $enableIpv6,
];
expect($publicNet)->toBe([
'enable_ipv4' => true,
'enable_ipv6' => true,
]);
});
it('validates public_net configuration with IPv4 only', function () {
$enableIpv4 = true;
$enableIpv6 = false;
$publicNet = [
'enable_ipv4' => $enableIpv4,
'enable_ipv6' => $enableIpv6,
];
expect($publicNet)->toBe([
'enable_ipv4' => true,
'enable_ipv6' => false,
]);
});
it('validates public_net configuration with IPv6 only', function () {
$enableIpv4 = false;
$enableIpv6 = true;
$publicNet = [
'enable_ipv4' => $enableIpv4,
'enable_ipv6' => $enableIpv6,
];
expect($publicNet)->toBe([
'enable_ipv4' => false,
'enable_ipv6' => true,
]);
});
it('validates IP address selection prefers IPv4 when both are enabled', function () {
$enableIpv4 = true;
$enableIpv6 = true;
$hetznerServer = [
'public_net' => [
'ipv4' => ['ip' => '1.2.3.4'],
'ipv6' => ['ip' => '2001:db8::1'],
],
];
$ipAddress = null;
if ($enableIpv4 && isset($hetznerServer['public_net']['ipv4']['ip'])) {
$ipAddress = $hetznerServer['public_net']['ipv4']['ip'];
} elseif ($enableIpv6 && isset($hetznerServer['public_net']['ipv6']['ip'])) {
$ipAddress = $hetznerServer['public_net']['ipv6']['ip'];
}
expect($ipAddress)->toBe('1.2.3.4');
});
it('validates IP address selection uses IPv6 when only IPv6 is enabled', function () {
$enableIpv4 = false;
$enableIpv6 = true;
$hetznerServer = [
'public_net' => [
'ipv4' => ['ip' => '1.2.3.4'],
'ipv6' => ['ip' => '2001:db8::1'],
],
];
$ipAddress = null;
if ($enableIpv4 && isset($hetznerServer['public_net']['ipv4']['ip'])) {
$ipAddress = $hetznerServer['public_net']['ipv4']['ip'];
} elseif ($enableIpv6 && isset($hetznerServer['public_net']['ipv6']['ip'])) {
$ipAddress = $hetznerServer['public_net']['ipv6']['ip'];
}
expect($ipAddress)->toBe('2001:db8::1');
});
it('validates SSH key array merging logic with Coolify key', function () {
$coolifyKeyId = 123;
$selectedHetznerKeys = [];
$sshKeys = array_merge(
[$coolifyKeyId],
$selectedHetznerKeys
);
$sshKeys = array_unique($sshKeys);
$sshKeys = array_values($sshKeys);
expect($sshKeys)->toBe([123])
->and(count($sshKeys))->toBe(1);
});
it('validates SSH key array merging with additional Hetzner keys', function () {
$coolifyKeyId = 123;
$selectedHetznerKeys = [456, 789];
$sshKeys = array_merge(
[$coolifyKeyId],
$selectedHetznerKeys
);
$sshKeys = array_unique($sshKeys);
$sshKeys = array_values($sshKeys);
expect($sshKeys)->toBe([123, 456, 789])
->and(count($sshKeys))->toBe(3);
});
it('validates deduplication when Coolify key is also in selected keys', function () {
$coolifyKeyId = 123;
$selectedHetznerKeys = [123, 456, 789];
$sshKeys = array_merge(
[$coolifyKeyId],
$selectedHetznerKeys
);
$sshKeys = array_unique($sshKeys);
$sshKeys = array_values($sshKeys);
expect($sshKeys)->toBe([123, 456, 789])
->and(count($sshKeys))->toBe(3);
});
describe('Boarding Flow Integration', function () {
uses(RefreshDatabase::class);
beforeEach(function () {
// Create a team with owner that has boarding enabled
$this->team = Team::factory()->create([
'show_boarding' => true,
]);
$this->user = User::factory()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
// Set current team and act as user
$this->actingAs($this->user);
session(['currentTeam' => $this->team]);
});
test('completes boarding when server is created from onboarding', function () {
// Verify boarding is initially enabled
expect($this->team->fresh()->show_boarding)->toBeTrue();
// Mount the component with from_onboarding flag
$component = Livewire::test(ByHetzner::class)
->set('from_onboarding', true);
// Verify the from_onboarding property is set
expect($component->get('from_onboarding'))->toBeTrue();
// After successful server creation in the actual component,
// the boarding should be marked as complete
// Note: We can't fully test the createServer method without mocking Hetzner API
// but we can verify the boarding completion logic is in place
});
test('boarding flag remains unchanged when not from onboarding', function () {
// Verify boarding is initially enabled
expect($this->team->fresh()->show_boarding)->toBeTrue();
// Mount the component without from_onboarding flag (default false)
Livewire::test(ByHetzner::class)
->set('from_onboarding', false);
// Boarding should still be enabled since it wasn't created from onboarding
expect($this->team->fresh()->show_boarding)->toBeTrue();
});
});