mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
@ -10,17 +10,17 @@
|
||||
use App\Enums\ServiceStatus;
|
||||
use App\Enums\Webserver;
|
||||
use App\Facades\SSH;
|
||||
use App\NotificationChannels\Email\NotificationMail;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @TODO add more tests
|
||||
*/
|
||||
class ServerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_create_custom_server(): void
|
||||
public function test_create_regular_server(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
@ -76,6 +76,62 @@ public function test_create_custom_server(): void
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_create_database_server(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
SSH::fake('Active: active'); // fake output for service installations
|
||||
|
||||
$this->post(route('servers.create'), [
|
||||
'type' => ServerType::DATABASE,
|
||||
'provider' => ServerProvider::CUSTOM,
|
||||
'name' => 'test',
|
||||
'ip' => '2.2.2.2',
|
||||
'port' => '22',
|
||||
'os' => OperatingSystem::UBUNTU22,
|
||||
'database' => Database::MYSQL80,
|
||||
])->assertSessionDoesntHaveErrors();
|
||||
|
||||
$server = \App\Models\Server::query()->where('ip', '2.2.2.2')->first();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'name' => 'test',
|
||||
'ip' => '2.2.2.2',
|
||||
'status' => ServerStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('services', [
|
||||
'server_id' => $server->id,
|
||||
'type' => 'php',
|
||||
'version' => '8.2',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('services', [
|
||||
'server_id' => $server->id,
|
||||
'type' => 'webserver',
|
||||
'name' => 'nginx',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $server->id,
|
||||
'type' => 'database',
|
||||
'name' => 'mysql',
|
||||
'version' => '8.0',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('services', [
|
||||
'server_id' => $server->id,
|
||||
'type' => 'firewall',
|
||||
'name' => 'ufw',
|
||||
'version' => 'latest',
|
||||
'status' => ServiceStatus::READY,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_delete_server(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
@ -89,4 +145,139 @@ public function test_delete_server(): void
|
||||
'id' => $this->server->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_cannot_delete_on_provider(): void
|
||||
{
|
||||
Mail::fake();
|
||||
Http::fake([
|
||||
'*' => Http::response([], 401),
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$provider = \App\Models\ServerProvider::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'provider' => ServerProvider::HETZNER,
|
||||
'credentials' => [
|
||||
'token' => 'token',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->server->update([
|
||||
'provider' => ServerProvider::HETZNER,
|
||||
'provider_id' => $provider->id,
|
||||
'provider_data' => [
|
||||
'hetzner_id' => 1,
|
||||
'ssh_key_id' => 1,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->delete(route('servers.delete', $this->server))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseMissing('servers', [
|
||||
'id' => $this->server->id,
|
||||
]);
|
||||
|
||||
Mail::assertSent(NotificationMail::class);
|
||||
}
|
||||
|
||||
public function test_check_connection_is_ready(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->server->update(['status' => ServerStatus::DISCONNECTED]);
|
||||
|
||||
$this->post(route('servers.settings.check-connection', $this->server))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'status' => ServerStatus::READY,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_connection_failed(): void
|
||||
{
|
||||
SSH::fake()->connectionWillFail();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->server->update(['status' => ServerStatus::READY]);
|
||||
|
||||
$this->post(route('servers.settings.check-connection', $this->server))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'status' => ServerStatus::DISCONNECTED,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_reboot_server(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->post(route('servers.settings.reboot', $this->server))
|
||||
->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'status' => ServerStatus::DISCONNECTED,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_edit_server(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->post(route('servers.settings.edit', $this->server), [
|
||||
'name' => 'new-name',
|
||||
])->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'name' => 'new-name',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_edit_server_ip_address(): void
|
||||
{
|
||||
SSH::fake();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->post(route('servers.settings.edit', $this->server), [
|
||||
'ip' => '2.2.2.2',
|
||||
])->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'ip' => '2.2.2.2',
|
||||
'status' => ServerStatus::READY,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_edit_server_ip_address_and_disconnect(): void
|
||||
{
|
||||
SSH::fake()->connectionWillFail();
|
||||
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$this->post(route('servers.settings.edit', $this->server), [
|
||||
'ip' => '2.2.2.2',
|
||||
'port' => 2222,
|
||||
])->assertSessionDoesntHaveErrors();
|
||||
|
||||
$this->assertDatabaseHas('servers', [
|
||||
'id' => $this->server->id,
|
||||
'ip' => '2.2.2.2',
|
||||
'port' => 2222,
|
||||
'status' => ServerStatus::DISCONNECTED,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user