This commit is contained in:
Saeed Vaziry
2024-10-13 12:33:12 +02:00
parent 386d8e73a7
commit 224e0ac2b0
49 changed files with 3668 additions and 766 deletions

View File

@ -5,7 +5,10 @@
use App\Enums\QueueStatus;
use App\Facades\SSH;
use App\Models\Queue;
use App\Web\Pages\Servers\Sites\Pages\Queues\Index;
use App\Web\Pages\Servers\Sites\Pages\Queues\Widgets\QueuesList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class QueuesTest extends TestCase
@ -22,7 +25,7 @@ public function test_see_queues()
]);
$this->get(
route('servers.sites.queues', [
Index::getUrl([
'server' => $this->server,
'site' => $this->site,
])
@ -42,13 +45,12 @@ public function test_delete_queue()
'site_id' => $this->site->id,
]);
$this->delete(
route('servers.sites.queues.destroy', [
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertRedirect();
Livewire::test(QueuesList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('delete', $queue->id)
->assertSuccessful();
$this->assertDatabaseMissing('queues', [
'id' => $queue->id,
@ -61,19 +63,18 @@ public function test_create_queue()
$this->actingAs($this->user);
$this->post(
route('servers.sites.queues.store', [
'server' => $this->server,
'site' => $this->site,
]),
[
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
])
->callAction('create', [
'command' => 'php artisan queue:work',
'user' => 'vito',
'auto_start' => 1,
'auto_restart' => 1,
'numprocs' => 1,
]
)->assertSessionDoesntHaveErrors();
])
->assertSuccessful();
$this->assertDatabaseHas('queues', [
'server_id' => $this->server->id,
@ -99,14 +100,12 @@ public function test_start_queue(): void
'status' => QueueStatus::STOPPED,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'start',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
Livewire::test(QueuesList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('start', $queue->id)
->assertSuccessful();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
@ -126,14 +125,12 @@ public function test_stop_queue(): void
'status' => QueueStatus::RUNNING,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'stop',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
Livewire::test(QueuesList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('stop', $queue->id)
->assertSuccessful();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
@ -153,14 +150,12 @@ public function test_restart_queue(): void
'status' => QueueStatus::RUNNING,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'restart',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
Livewire::test(QueuesList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('restart', $queue->id)
->assertSuccessful();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
@ -180,14 +175,11 @@ public function test_show_logs(): void
'status' => QueueStatus::RUNNING,
]);
$this->get(
route('servers.sites.queues.logs', [
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)
->assertSessionDoesntHaveErrors()
->assertSessionHas('content', 'logs');
Livewire::test(QueuesList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('logs', $queue->id)
->assertSuccessful();
}
}

View File

@ -6,7 +6,12 @@
use App\Facades\SSH;
use App\Models\Script;
use App\Models\ScriptExecution;
use App\Web\Pages\Scripts\Executions;
use App\Web\Pages\Scripts\Index;
use App\Web\Pages\Scripts\Widgets\ScriptExecutionsList;
use App\Web\Pages\Scripts\Widgets\ScriptsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class ScriptTest extends TestCase
@ -21,9 +26,7 @@ public function test_see_scripts(): void
'user_id' => $this->user->id,
]);
$this->get(
route('scripts.index')
)
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($script->name);
}
@ -32,15 +35,12 @@ public function test_create_script(): void
{
$this->actingAs($this->user);
$this->post(
route('scripts.store'),
[
Livewire::test(Index::class)
->callAction('create', [
'name' => 'Test Script',
'content' => 'echo "Hello, World!"',
]
)
->assertSessionDoesntHaveErrors()
->assertHeader('HX-Redirect');
])
->assertSuccessful();
$this->assertDatabaseHas('scripts', [
'name' => 'Test Script',
@ -56,12 +56,12 @@ public function test_edit_script(): void
'user_id' => $this->user->id,
]);
$this->post(route('scripts.edit', ['script' => $script]), [
'name' => 'New Name',
'content' => 'echo "Hello, new World!"',
])
->assertSuccessful()
->assertHeader('HX-Redirect');
Livewire::test(ScriptsList::class)
->callTableAction('edit', $script->id, [
'name' => 'New Name',
'content' => 'echo "Hello, new World!"',
])
->assertSuccessful();
$this->assertDatabaseHas('scripts', [
'id' => $script->id,
@ -83,11 +83,9 @@ public function test_delete_script(): void
'status' => ScriptExecutionStatus::EXECUTING,
]);
$this->delete(
route('scripts.delete', [
'script' => $script,
])
)->assertRedirect();
Livewire::test(ScriptsList::class)
->callTableAction('delete', $script->id)
->assertSuccessful();
$this->assertDatabaseMissing('scripts', [
'id' => $script->id,
@ -108,17 +106,14 @@ public function test_execute_script_and_view_log(): void
'user_id' => $this->user->id,
]);
$this->post(
route('scripts.execute', [
'script' => $script,
]),
[
Livewire::test(Executions::class, [
'script' => $script,
])
->callAction('execute', [
'server' => $this->server->id,
'user' => 'root',
]
)
->assertSessionDoesntHaveErrors()
->assertHeader('HX-Redirect');
])
->assertSuccessful();
$this->assertDatabaseHas('script_executions', [
'script_id' => $script->id,
@ -131,14 +126,11 @@ public function test_execute_script_and_view_log(): void
$execution = $script->lastExecution;
$this->get(
route('scripts.log', [
'script' => $script,
'execution' => $execution,
])
)
->assertRedirect()
->assertSessionHas('content', 'script output');
Livewire::test(ScriptExecutionsList::class, [
'script' => $script,
])
->callTableAction('logs', $execution->id)
->assertSuccessful();
}
public function test_see_executions(): void
@ -154,11 +146,7 @@ public function test_see_executions(): void
'status' => ScriptExecutionStatus::EXECUTING,
]);
$this->get(
route('scripts.show', [
'script' => $script,
])
)
$this->get(Executions::getUrl(['script' => $script]))
->assertSuccessful()
->assertSee($scriptExecution->status);
}

View File

@ -1,58 +0,0 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SearchTest extends TestCase
{
use RefreshDatabase;
public function test_search_server(): void
{
$this->actingAs($this->user);
$this->get(route('search', ['q' => $this->server->name]))
->assertOk()
->assertJson([
'results' => [
[
'type' => 'server',
'url' => route('servers.show', ['server' => $this->site->server]),
'text' => $this->server->name,
'project' => $this->server->project->name,
],
],
]);
}
public function test_search_site(): void
{
$this->actingAs($this->user);
$this->get(route('search', ['q' => $this->site->domain]))
->assertOk()
->assertJson([
'results' => [
[
'type' => 'site',
'url' => route('servers.sites.show', ['server' => $this->site->server, 'site' => $this->site]),
'text' => $this->site->domain,
'project' => $this->site->server->project->name,
],
],
]);
}
public function test_search_has_no_results(): void
{
$this->actingAs($this->user);
$this->get(route('search', ['q' => 'nothing-will-found']))
->assertOk()
->assertJson([
'results' => [],
]);
}
}

View File

@ -5,7 +5,10 @@
use App\Enums\SshKeyStatus;
use App\Facades\SSH;
use App\Models\SshKey;
use App\Web\Pages\Servers\SSHKeys\Index;
use App\Web\Pages\Servers\SSHKeys\Widgets\SshKeysList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class ServerKeysTest extends TestCase
@ -26,7 +29,7 @@ public function test_see_server_keys()
'status' => SshKeyStatus::ADDED,
]);
$this->get(route('servers.ssh-keys', $this->server))
$this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful()
->assertSeeText('My first key');
}
@ -47,7 +50,11 @@ public function test_delete_ssh_key()
'status' => SshKeyStatus::ADDED,
]);
$this->delete(route('servers.ssh-keys.destroy', [$this->server, $sshKey]));
Livewire::test(SshKeysList::class, [
'server' => $this->server,
])
->callTableAction('delete', $sshKey->id)
->assertSuccessful();
$this->assertDatabaseMissing('server_ssh_keys', [
'server_id' => $this->server->id,
@ -61,10 +68,15 @@ public function test_add_new_ssh_key()
$this->actingAs($this->user);
$this->post(route('servers.ssh-keys.store', $this->server), [
'name' => 'My first key',
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3CCnyBbpCgOJ0AWUSfBZ+mYAsYzcQDegPkBx1kyE0bXT1yX4+6uYx1Jh6NxWgLyaU0BaP4nsClrK1u5FojQHd8J7ycc0N3H8B+v2NPzj1Q6bFnl40saastONVm+d4edbCg9BowGAafLcf9ALsognqqOWQbK/QOpAhg25IAe47eiY3IjDGMHlsvaZkMtkDhT4t1mK8ZLjxw5vjyVYgINJefR981bIxMFrXy+0xBCsYOZxMIoAJsgCkrAGlI4kQHKv0SQVccSyTE1eziIZa5b3QUlXj8ogxMfK/EOD7Aoqinw652k4S5CwFs/LLmjWcFqCKDM6CSggWpB78DZ729O6zFvQS9V99/9SsSV7Qc5ML7B0DKzJ/tbHkaAE8xdZnQnZFVUegUMtUmjvngMaGlYsxkAZrUKsFRoh7xfXVkDyRBaBSslRNe8LFsXw9f7Q+3jdZ5vhGhmp+TBXTlgxApwR023411+ABE9y0doCx8illya3m2olEiiMZkRclgqsWFSk=',
]);
Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('deploy', [
'type' => 'new',
'name' => 'My first key',
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3CCnyBbpCgOJ0AWUSfBZ+mYAsYzcQDegPkBx1kyE0bXT1yX4+6uYx1Jh6NxWgLyaU0BaP4nsClrK1u5FojQHd8J7ycc0N3H8B+v2NPzj1Q6bFnl40saastONVm+d4edbCg9BowGAafLcf9ALsognqqOWQbK/QOpAhg25IAe47eiY3IjDGMHlsvaZkMtkDhT4t1mK8ZLjxw5vjyVYgINJefR981bIxMFrXy+0xBCsYOZxMIoAJsgCkrAGlI4kQHKv0SQVccSyTE1eziIZa5b3QUlXj8ogxMfK/EOD7Aoqinw652k4S5CwFs/LLmjWcFqCKDM6CSggWpB78DZ729O6zFvQS9V99/9SsSV7Qc5ML7B0DKzJ/tbHkaAE8xdZnQnZFVUegUMtUmjvngMaGlYsxkAZrUKsFRoh7xfXVkDyRBaBSslRNe8LFsXw9f7Q+3jdZ5vhGhmp+TBXTlgxApwR023411+ABE9y0doCx8illya3m2olEiiMZkRclgqsWFSk=',
])
->assertSuccessful();
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
@ -84,9 +96,14 @@ public function test_add_existing_key()
'public_key' => 'public-key-content',
]);
$this->post(route('servers.ssh-keys.deploy', $this->server), [
'key_id' => $sshKey->id,
]);
Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('deploy', [
'type' => 'existing',
'key_id' => $sshKey->id,
])
->assertSuccessful();
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
@ -110,26 +127,35 @@ public function test_create_ssh_key_handles_invalid_or_partial_keys(array $postB
'public_key' => 'public-key-content',
]);
$response = $this->post(route('servers.ssh-keys.store', $this->server), $postBody);
$postBody['type'] = 'new';
$response = Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('deploy', $postBody);
if ($expectedToSucceed) {
$response->assertSessionDoesntHaveErrors();
$response->assertSuccessful();
$this->assertDatabaseHas('ssh_keys', [
'name' => $postBody['name'],
]);
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
'status' => SshKeyStatus::ADDED,
]);
} else {
$response->assertSessionHasErrors('public_key', 'Invalid key');
$response->assertHasActionErrors([
'public_key',
]);
$this->assertDatabaseMissing('server_ssh_keys', [
'server_id' => $this->server->id,
'status' => SshKeyStatus::ADDED,
]);
}
}
public static function ssh_key_data_provider(): array
{
return [
[
[
'name' => 'My first key',
// Key Already exists
'public_key' => 'public-key-content',
],
self::EXPECT_FAILURE,
],
[
[
'name' => 'My first key',

View File

@ -3,8 +3,11 @@
namespace Tests\Feature;
use App\Enums\ServerProvider;
use App\Web\Pages\Settings\ServerProviders\Index;
use App\Web\Pages\Settings\ServerProviders\Widgets\ServerProvidersList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class ServerProvidersTest extends TestCase
@ -27,7 +30,10 @@ public function test_connect_provider(string $provider, array $input): void
],
$input
);
$this->post(route('settings.server-providers.connect'), $data)->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('create', $data)
->assertHasNoActionErrors()
->assertSuccessful();
$this->assertDatabaseHas('server_providers', [
'provider' => $provider,
@ -54,7 +60,10 @@ public function test_cannot_connect_to_provider(string $provider, array $input):
],
$input
);
$this->post(route('settings.server-providers.connect'), $data)->assertSessionHasErrors();
Livewire::test(Index::class)
->callAction('create', $data)
->assertActionHalted('create')
->assertNotified();
$this->assertDatabaseMissing('server_providers', [
'provider' => $provider,
@ -70,7 +79,7 @@ public function test_see_providers_list(): void
'user_id' => $this->user->id,
]);
$this->get(route('settings.server-providers'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($provider->profile);
}
@ -87,8 +96,9 @@ public function test_delete_provider(string $provider): void
'provider' => $provider,
]);
$this->delete(route('settings.server-providers.delete', $provider))
->assertSessionDoesntHaveErrors();
Livewire::test(ServerProvidersList::class)
->callTableAction('delete', $provider->id)
->assertSuccessful();
$this->assertDatabaseMissing('server_providers', [
'id' => $provider->id,
@ -111,10 +121,9 @@ public function test_cannot_delete_provider(string $provider): void
'provider_id' => $provider->id,
]);
$this->delete(route('settings.server-providers.delete', $provider))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This server provider is being used by a server.');
Livewire::test(ServerProvidersList::class)
->callTableAction('delete', $provider->id)
->assertNotified('This server provider is being used by a server.');
$this->assertDatabaseHas('server_providers', [
'id' => $provider->id,

View File

@ -10,10 +10,18 @@
use App\Enums\ServiceStatus;
use App\Enums\Webserver;
use App\Facades\SSH;
use App\Models\Server;
use App\NotificationChannels\Email\NotificationMail;
use App\Web\Pages\Servers\Index;
use App\Web\Pages\Servers\Settings;
use App\Web\Pages\Servers\Widgets\ServerDetails;
use App\Web\Pages\Servers\Widgets\ServerSummary;
use App\Web\Pages\Servers\Widgets\UpdateServerInfo;
use Filament\Notifications\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Livewire\Livewire;
use Tests\TestCase;
class ServerTest extends TestCase
@ -26,17 +34,19 @@ public function test_create_regular_server(): void
SSH::fake('Active: active'); // fake output for service installations
$this->post(route('servers.create'), [
'type' => ServerType::REGULAR,
'provider' => ServerProvider::CUSTOM,
'name' => 'test',
'ip' => '1.1.1.1',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'webserver' => Webserver::NGINX,
'database' => Database::MYSQL80,
'php' => '8.2',
])->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('create', [
'type' => ServerType::REGULAR,
'provider' => ServerProvider::CUSTOM,
'name' => 'test',
'ip' => '1.1.1.1',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'webserver' => Webserver::NGINX,
'database' => Database::MYSQL80,
'php' => '8.2',
])
->assertSuccessful();
$this->assertDatabaseHas('servers', [
'name' => 'test',
@ -82,17 +92,19 @@ public function test_create_database_server(): void
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();
Livewire::test(Index::class)
->callAction('create', [
'type' => ServerType::DATABASE,
'provider' => ServerProvider::CUSTOM,
'name' => 'test',
'ip' => '2.2.2.2',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'database' => Database::MYSQL80,
])
->assertSuccessful();
$server = \App\Models\Server::query()->where('ip', '2.2.2.2')->first();
$server = Server::query()->where('ip', '2.2.2.2')->first();
$this->assertDatabaseHas('servers', [
'name' => 'test',
@ -138,8 +150,11 @@ public function test_delete_server(): void
SSH::fake();
$this->delete(route('servers.delete', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(Settings::class, [
'server' => $this->server,
])->callAction('delete')
->assertSuccessful()
->assertRedirect(Index::getUrl());
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
@ -172,8 +187,11 @@ public function test_cannot_delete_on_provider(): void
],
]);
$this->delete(route('servers.delete', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(Settings::class, [
'server' => $this->server,
])->callAction('delete')
->assertSuccessful()
->assertRedirect(Index::getUrl());
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
@ -190,8 +208,12 @@ public function test_check_connection_is_ready(): void
$this->server->update(['status' => ServerStatus::DISCONNECTED]);
$this->post(route('servers.settings.check-connection', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(ServerSummary::class, [
'server' => $this->server,
])
->callInfolistAction('status', 'check-status')
->assertSuccessful()
->assertNotified('Server is '.ServerStatus::READY);
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -207,8 +229,12 @@ public function test_connection_failed(): void
$this->server->update(['status' => ServerStatus::READY]);
$this->post(route('servers.settings.check-connection', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(ServerSummary::class, [
'server' => $this->server,
])
->callInfolistAction('status', 'check-status')
->assertSuccessful()
->assertNotified('Server is '.ServerStatus::DISCONNECTED);
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -222,8 +248,11 @@ public function test_reboot_server(): void
$this->actingAs($this->user);
$this->post(route('servers.settings.reboot', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(Settings::class, [
'server' => $this->server,
])
->callAction('reboot')
->assertSuccessful();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -233,11 +262,20 @@ public function test_reboot_server(): void
public function test_edit_server(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'name' => 'new-name',
])->assertSessionDoesntHaveErrors();
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
])
->fill([
'name' => 'new-name',
'ip' => $this->server->ip,
'port' => $this->server->port,
])
->call('submit')
->assertSuccessful();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -251,9 +289,16 @@ public function test_edit_server_ip_address(): void
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'ip' => '2.2.2.2',
])->assertSessionDoesntHaveErrors();
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
])
->fill([
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => $this->server->port,
])
->call('submit')
->assertSuccessful();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -268,10 +313,16 @@ public function test_edit_server_ip_address_and_disconnect(): void
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'ip' => '2.2.2.2',
'port' => 2222,
])->assertSessionDoesntHaveErrors();
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
])
->fill([
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => 2222,
])
->call('submit')
->assertSuccessful();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -287,8 +338,17 @@ public function test_check_updates(): void
$this->actingAs($this->user);
$this->post(route('servers.settings.check-updates', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('last_updated_check', 'check-update')
->assertSuccessful()
->assertNotified(
Notification::make()
->info()
->title('Available updates:')
->body(9)
);
$this->server->refresh();
$this->assertEquals(9, $this->server->updates);
@ -300,8 +360,11 @@ public function test_update_server(): void
$this->actingAs($this->user);
$this->post(route('servers.settings.update', $this->server))
->assertSessionDoesntHaveErrors();
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('updates', 'update-server')
->assertSuccessful();
$this->server->refresh();

View File

@ -5,9 +5,12 @@
use App\Enums\ServiceStatus;
use App\Facades\SSH;
use App\Models\Server;
use App\Web\Pages\Servers\Services\Index;
use App\Web\Pages\Servers\Services\Widgets\ServicesList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class ServicesTest extends TestCase
@ -18,14 +21,13 @@ public function test_see_services_list(): void
{
$this->actingAs($this->user);
$this->get(route('servers.services', $this->server))
$this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful()
->assertSee('mysql')
->assertSee('nginx')
->assertSee('php')
->assertSee('supervisor')
->assertSee('redis')
->assertSee('vito-agent')
->assertSee('ufw');
}
@ -37,13 +39,16 @@ public function test_restart_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::STOPPED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.restart', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('restart', $service->id)
->assertSuccessful();
$service->refresh();
@ -61,10 +66,11 @@ public function test_failed_to_restart_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.restart', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('restart', $service->id)
->assertSuccessful();
$service->refresh();
@ -82,10 +88,11 @@ public function test_stop_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.stop', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('stop', $service->id)
->assertSuccessful();
$service->refresh();
@ -103,10 +110,11 @@ public function test_failed_to_stop_service(string $name): void
SSH::fake('Active: active');
$this->get(route('servers.services.stop', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('stop', $service->id)
->assertSuccessful();
$service->refresh();
@ -121,13 +129,16 @@ public function test_start_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::STOPPED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.start', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('start', $service->id)
->assertSuccessful();
$service->refresh();
@ -145,10 +156,11 @@ public function test_failed_to_start_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.start', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('start', $service->id)
->assertSuccessful();
$service->refresh();
@ -163,13 +175,16 @@ public function test_enable_service(string $name): void
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
$service->status = ServiceStatus::DISABLED;
$service->save();
SSH::fake('Active: active');
$this->get(route('servers.services.enable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('enable', $service->id)
->assertSuccessful();
$service->refresh();
@ -187,10 +202,11 @@ public function test_failed_to_enable_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.enable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('enable', $service->id)
->assertSuccessful();
$service->refresh();
@ -208,10 +224,11 @@ public function test_disable_service(string $name): void
SSH::fake('Active: inactive');
$this->get(route('servers.services.disable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('disable', $service->id)
->assertSuccessful();
$service->refresh();
@ -229,10 +246,11 @@ public function test_failed_to_disable_service(string $name): void
SSH::fake('Active: active');
$this->get(route('servers.services.disable', [
Livewire::test(ServicesList::class, [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
])
->callTableAction('disable', $service->id)
->assertSuccessful();
$service->refresh();
@ -262,13 +280,15 @@ public function test_install_service(string $name, string $type, string $version
if (! File::exists($keys['public_key_path']) || ! File::exists($keys['private_key_path'])) {
$server->provider()->generateKeyPair();
}
$this->post(route('servers.services.install', [
Livewire::test(Index::class, [
'server' => $server,
]), [
'name' => $name,
'type' => $type,
'version' => $version,
])->assertSessionDoesntHaveErrors();
])
->callAction('install', [
'name' => $name,
'version' => $version,
])
->assertSuccessful();
$this->assertDatabaseHas('services', [
'server_id' => $server->id,

View File

@ -7,8 +7,13 @@
use App\Enums\SourceControl;
use App\Facades\SSH;
use App\Models\Site;
use App\Web\Pages\Servers\Sites\Index;
use App\Web\Pages\Servers\Sites\Settings;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\SiteDetails;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class SitesTest extends TestCase
@ -36,12 +41,15 @@ public function test_create_site(array $inputs): void
$inputs['source_control'] = $sourceControl->id;
$this->post(route('servers.sites.create', [
Livewire::test(Index::class, [
'server' => $this->server,
]), $inputs)->assertSessionDoesntHaveErrors();
])
->callAction('create', $inputs)
->assertHasNoActionErrors()
->assertSuccessful();
$this->assertDatabaseHas('sites', [
'domain' => 'example.com',
'domain' => $inputs['domain'],
'aliases' => json_encode($inputs['aliases'] ?? []),
'status' => SiteStatus::READY,
]);
@ -79,9 +87,12 @@ public function test_create_site_failed_due_to_source_control(int $status): void
$inputs['source_control'] = $sourceControl->id;
$this->post(route('servers.sites.create', [
Livewire::test(Index::class, [
'server' => $this->server,
]), $inputs)->assertSessionHasErrors();
])
->callAction('create', $inputs)
->assertNotified()
->assertSuccessful();
$this->assertDatabaseMissing('sites', [
'domain' => 'example.com',
@ -97,9 +108,7 @@ public function test_see_sites_list(): void
'server_id' => $this->server->id,
]);
$this->get(route('servers.sites', [
'server' => $this->server,
]))
$this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful()
->assertSee($site->domain);
}
@ -114,10 +123,13 @@ public function test_delete_site(): void
'server_id' => $this->server->id,
]);
$this->delete(route('servers.sites.destroy', [
Livewire::test(Settings::class, [
'server' => $this->server,
'site' => $site,
]))->assertRedirect();
])
->callAction('delete')
->assertHasNoActionErrors()
->assertSuccessful();
$this->assertDatabaseMissing('sites', [
'id' => $site->id,
@ -134,12 +146,14 @@ public function test_change_php_version(): void
'server_id' => $this->server->id,
]);
$this->post(route('servers.sites.settings.php', [
'server' => $this->server,
Livewire::test(SiteDetails::class, [
'site' => $site,
]), [
'version' => '8.2',
])->assertSessionDoesntHaveErrors();
])
->callInfolistAction('php_version', 'edit_php_version', [
'version' => '8.2',
])
->assertHasNoActionErrors()
->assertSuccessful();
$site->refresh();
@ -162,12 +176,14 @@ public function test_update_source_control(): void
'provider' => SourceControl::GITHUB,
]);
$this->post(route('servers.sites.settings.source-control', [
'server' => $this->server,
Livewire::test(SiteDetails::class, [
'site' => $this->site,
]), [
'source_control' => $sourceControl->id,
])->assertSessionDoesntHaveErrors();
])
->callInfolistAction('source_control_id', 'edit_source_control', [
'source_control' => $sourceControl->id,
])
->assertHasNoActionErrors()
->assertSuccessful();
$this->site->refresh();
@ -190,12 +206,13 @@ public function test_failed_to_update_source_control(): void
'provider' => SourceControl::GITHUB,
]);
$this->post(route('servers.sites.settings.source-control', [
'server' => $this->server,
Livewire::test(SiteDetails::class, [
'site' => $this->site,
]), [
'source_control' => $sourceControl->id,
])->assertSessionHasErrors();
])
->callInfolistAction('source_control_id', 'edit_source_control', [
'source_control' => $sourceControl->id,
])
->assertHasActionErrors();
}
public function test_update_v_host(): void
@ -208,10 +225,26 @@ public function test_update_v_host(): void
'server_id' => $this->server->id,
]);
$this->get(route('servers.sites.settings.vhost', [
Livewire::test(Settings::class, [
'server' => $this->server,
'site' => $site,
]))->assertSessionDoesntHaveErrors();
])
->callAction('vhost', [
'vhost' => 'test',
])
->assertNotified('VHost updated!');
}
public function test_see_logs(): void
{
$this->actingAs($this->user);
$this->get(View::getUrl([
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee('Logs');
}
public static function create_data(): array
@ -273,16 +306,4 @@ public static function create_failure_data(): array
[404],
];
}
public function test_see_logs(): void
{
$this->actingAs($this->user);
$this->get(route('servers.sites.logs', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee('Vito Logs');
}
}

View File

@ -3,8 +3,11 @@
namespace Tests\Feature;
use App\Models\SourceControl;
use App\Web\Pages\Settings\SourceControls\Index;
use App\Web\Pages\Settings\SourceControls\Widgets\SourceControlsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class SourceControlsTest extends TestCase
@ -28,8 +31,10 @@ public function test_connect_provider(string $provider, ?string $customUrl, arra
if ($customUrl !== null) {
$input['url'] = $customUrl;
}
$this->post(route('settings.source-controls.connect'), $input)
->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('connect', $input)
->assertSuccessful();
$this->assertDatabaseHas('source_controls', [
'provider' => $provider,
@ -64,10 +69,11 @@ public function test_delete_provider(string $provider): void
'profile' => 'test',
]);
$this->delete(route('settings.source-controls.delete', $sourceControl->id))
->assertSessionDoesntHaveErrors();
Livewire::test(SourceControlsList::class)
->callTableAction('delete', $sourceControl->id)
->assertSuccessful();
$this->assertDatabaseMissing('source_controls', [
$this->assertSoftDeleted('source_controls', [
'id' => $sourceControl->id,
]);
}
@ -89,12 +95,11 @@ public function test_cannot_delete_provider(string $provider): void
'source_control_id' => $sourceControl->id,
]);
$this->delete(route('settings.source-controls.delete', $sourceControl->id))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This source control is being used by a site.');
Livewire::test(SourceControlsList::class)
->callTableAction('delete', $sourceControl->id)
->assertNotified('This source control is being used by a site.');
$this->assertDatabaseHas('source_controls', [
$this->assertNotSoftDeleted('source_controls', [
'id' => $sourceControl->id,
]);
}
@ -115,10 +120,15 @@ public function test_edit_source_control(string $provider, ?string $url, array $
'url' => $url,
]);
$this->post(route('settings.source-controls.update', $sourceControl->id), array_merge([
'name' => 'new-name',
'url' => $url,
], $input))->assertSessionDoesntHaveErrors();
Livewire::test(SourceControlsList::class)
->callTableAction('edit', $sourceControl->id, [
'name' => 'new-name',
'token' => 'test', // for GitHub and Gitlab
'username' => 'test', // for Bitbucket
'password' => 'test', // for Bitbucket
'url' => $url, // for Gitlab
])
->assertSuccessful();
$sourceControl->refresh();

View File

@ -3,7 +3,10 @@
namespace Tests\Feature;
use App\Models\SshKey;
use App\Web\Pages\Settings\SSHKeys\Index;
use App\Web\Pages\Settings\SSHKeys\Widgets\SshKeysList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class SshKeysTest extends TestCase
@ -14,10 +17,12 @@ public function test_create_ssh_key(): void
{
$this->actingAs($this->user);
$this->post(route('settings.ssh-keys.add'), [
'name' => 'test',
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== test@test.local',
])->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('add', [
'name' => 'test',
'public_key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== test@test.local',
])
->assertSuccessful();
}
public function test_get_public_keys_list(): void
@ -28,7 +33,7 @@ public function test_get_public_keys_list(): void
'user_id' => $this->user->id,
]);
$this->get(route('settings.ssh-keys'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($key->name);
}
@ -41,10 +46,11 @@ public function test_delete_key(): void
'user_id' => $this->user->id,
]);
$this->delete(route('settings.ssh-keys.delete', $key->id))
->assertSessionDoesntHaveErrors();
Livewire::test(SshKeysList::class)
->callTableAction('delete', $key->id)
->assertSuccessful();
$this->assertDatabaseMissing('ssh_keys', [
$this->assertSoftDeleted('ssh_keys', [
'id' => $key->id,
]);
}
@ -63,12 +69,13 @@ public function test_create_ssh_key_handles_invalid_or_partial_keys(array $postB
'public_key' => 'public-key-content',
]);
$response = $this->post(route('settings.ssh-keys.add'), $postBody);
$response = Livewire::test(Index::class)
->callAction('add', $postBody);
if ($expectedToSucceed) {
$response->assertSessionDoesntHaveErrors();
$response->assertHasNoActionErrors();
} else {
$response->assertSessionHasErrors('public_key', 'Invalid key');
$response->assertHasActionErrors();
}
}

View File

@ -6,7 +6,10 @@
use App\Enums\SslType;
use App\Facades\SSH;
use App\Models\Ssl;
use App\Web\Pages\Servers\Sites\Pages\SSL\Index;
use App\Web\Pages\Servers\Sites\Pages\SSL\Widgets\SslsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class SslTest extends TestCase
@ -21,7 +24,7 @@ public function test_see_ssls_list()
'site_id' => $this->site->id,
]);
$this->get(route('servers.sites.ssl', [
$this->get(Index::getUrl([
'server' => $this->server,
'site' => $this->site,
]))
@ -29,30 +32,20 @@ public function test_see_ssls_list()
->assertSee($ssl->type);
}
public function test_see_ssls_list_with_no_ssls()
{
$this->actingAs($this->user);
$this->get(route('servers.sites.ssl', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSeeText(__("You don't have any SSL certificates yet!"));
}
public function test_letsencrypt_ssl()
{
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
$this->post(route('servers.sites.ssl.store', [
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
]), [
'type' => SslType::LETSENCRYPT,
])->assertSessionDoesntHaveErrors();
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
])
->assertSuccessful();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
@ -68,13 +61,15 @@ public function test_letsencrypt_ssl_with_aliases()
$this->actingAs($this->user);
$this->post(route('servers.sites.ssl.store', [
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
]), [
'type' => SslType::LETSENCRYPT,
'aliases' => '1',
])->assertSessionDoesntHaveErrors();
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
'aliases' => true,
])
->assertSuccessful();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
@ -90,15 +85,17 @@ public function test_custom_ssl()
$this->actingAs($this->user);
$this->post(route('servers.sites.ssl.store', [
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
]), [
'type' => SslType::CUSTOM,
'certificate' => 'certificate',
'private' => 'private',
'expires_at' => now()->addYear()->format('Y-m-d'),
])->assertSessionDoesntHaveErrors();
])
->callAction('create', [
'type' => SslType::CUSTOM,
'certificate' => 'certificate',
'private' => 'private',
'expires_at' => now()->addYear()->format('Y-m-d'),
])
->assertSuccessful();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
@ -117,11 +114,11 @@ public function test_delete_ssl()
'site_id' => $this->site->id,
]);
$this->delete(route('servers.sites.ssl.destroy', [
'server' => $this->server,
Livewire::test(SslsList::class, [
'site' => $this->site,
'ssl' => $ssl,
]))->assertRedirect();
])
->callTableAction('delete', $ssl->id)
->assertSuccessful();
$this->assertDatabaseMissing('ssls', [
'id' => $ssl->id,

View File

@ -7,12 +7,11 @@
use App\Models\Backup;
use App\Models\Database;
use App\Models\StorageProvider as StorageProviderModel;
use App\StorageProviders\S3;
use App\StorageProviders\Wasabi;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use App\Web\Pages\Settings\StorageProviders\Index;
use App\Web\Pages\Settings\StorageProviders\Widgets\StorageProvidersList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class StorageProvidersTest extends TestCase
@ -34,8 +33,9 @@ public function test_create(array $input): void
FTP::fake();
}
$this->post(route('settings.storage-providers.connect'), $input)
->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('connect', $input)
->assertSuccessful();
if ($input['provider'] === StorageProvider::FTP) {
FTP::assertConnected($input['host']);
@ -52,30 +52,30 @@ public function test_see_providers_list(): void
{
$this->actingAs($this->user);
$provider = \App\Models\StorageProvider::factory()->create([
$provider = StorageProviderModel::factory()->create([
'user_id' => $this->user->id,
'provider' => StorageProvider::DROPBOX,
]);
$this->get(route('settings.storage-providers'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($provider->profile);
$provider = \App\Models\StorageProvider::factory()->create([
$provider = StorageProviderModel::factory()->create([
'user_id' => $this->user->id,
'provider' => StorageProvider::S3,
]);
$this->get(route('settings.storage-providers'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($provider->profile);
$provider = \App\Models\StorageProvider::factory()->create([
$provider = StorageProviderModel::factory()->create([
'user_id' => $this->user->id,
'provider' => StorageProvider::WASABI,
]);
$this->get(route('settings.storage-providers'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($provider->profile);
}
@ -84,12 +84,13 @@ public function test_delete_provider(): void
{
$this->actingAs($this->user);
$provider = \App\Models\StorageProvider::factory()->create([
$provider = StorageProviderModel::factory()->create([
'user_id' => $this->user->id,
]);
$this->delete(route('settings.storage-providers.delete', $provider->id))
->assertSessionDoesntHaveErrors();
Livewire::test(StorageProvidersList::class)
->callTableAction('delete', $provider->id)
->assertSuccessful();
$this->assertDatabaseMissing('storage_providers', [
'id' => $provider->id,
@ -104,7 +105,7 @@ public function test_cannot_delete_provider(): void
'server_id' => $this->server,
]);
$provider = \App\Models\StorageProvider::factory()->create([
$provider = StorageProviderModel::factory()->create([
'user_id' => $this->user->id,
]);
@ -114,188 +115,15 @@ public function test_cannot_delete_provider(): void
'storage_id' => $provider->id,
]);
$this->delete(route('settings.storage-providers.delete', $provider->id))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This storage provider is being used by a backup.');
Livewire::test(StorageProvidersList::class)
->callTableAction('delete', $provider->id)
->assertNotified('This storage provider is being used by a backup.');
$this->assertDatabaseHas('storage_providers', [
'id' => $provider->id,
]);
}
public function test_s3_connect_successful()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::S3,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method
$s3ClientMock->expects($this->once())
->method('execute')
->willReturn(['Buckets' => []]);
// Mock the S3 class to return the mocked S3Client
$s3 = $this->getMockBuilder(S3::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$s3->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertTrue($s3->connect());
}
public function test_s3_connect_failure()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::S3,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method to throw an S3Exception
$s3ClientMock->expects($this->once())
->method('execute')
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
// Mock the S3 class to return the mocked S3Client
$s3 = $this->getMockBuilder(S3::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$s3->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertFalse($s3->connect());
}
public function test_wasabi_connect_successful()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method
$s3ClientMock->expects($this->once())
->method('execute')
->willReturn(['Buckets' => []]);
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertTrue($wasabi->connect());
}
public function test_wasabi_connect_failure()
{
$storageProvider = StorageProviderModel::factory()->create([
'provider' => StorageProvider::WASABI,
'credentials' => [
'key' => 'fake-key',
'secret' => 'fake-secret',
'region' => 'us-east-1',
'bucket' => 'fake-bucket',
'path' => '/',
],
]);
// Mock the S3Client (Wasabi uses S3-compatible API)
$s3ClientMock = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['getCommand', 'execute'])
->getMock();
// Mock the getCommand method
$s3ClientMock->expects($this->once())
->method('getCommand')
->with('listBuckets')
->willReturn(new \Aws\Command('listBuckets'));
// Mock the execute method to throw an S3Exception
$s3ClientMock->expects($this->once())
->method('execute')
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
// Mock the Wasabi class to return the mocked S3Client
$wasabi = $this->getMockBuilder(Wasabi::class)
->setConstructorArgs([$storageProvider])
->onlyMethods(['getClient'])
->getMock();
$wasabi->expects($this->once())
->method('getClient')
->willReturn($s3ClientMock);
$this->assertFalse($wasabi->connect());
}
/**
* @TODO: complete FTP tests
*/

View File

@ -2,10 +2,13 @@
namespace Tests\Feature;
use App\Models\Server;
use App\Models\Site;
use App\Models\Tag;
use App\Web\Pages\Servers\Sites\Widgets\SiteDetails;
use App\Web\Pages\Servers\Widgets\ServerDetails;
use App\Web\Pages\Settings\Tags\Index;
use App\Web\Pages\Settings\Tags\Widgets\TagsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class TagsTest extends TestCase
@ -16,10 +19,12 @@ public function test_create_tag(): void
{
$this->actingAs($this->user);
$this->post(route('settings.tags.create'), [
'name' => 'test',
'color' => config('core.tag_colors')[0],
])->assertSessionDoesntHaveErrors();
Livewire::test(Index::class)
->callAction('create', [
'name' => 'test',
'color' => config('core.tag_colors')[0],
])
->assertSuccessful();
$this->assertDatabaseHas('tags', [
'project_id' => $this->user->current_project_id,
@ -36,7 +41,7 @@ public function test_get_tags_list(): void
'project_id' => $this->user->current_project_id,
]);
$this->get(route('settings.tags'))
$this->get(Index::getUrl())
->assertSuccessful()
->assertSee($tag->name);
}
@ -49,8 +54,9 @@ public function test_delete_tag(): void
'project_id' => $this->user->current_project_id,
]);
$this->delete(route('settings.tags.delete', $tag->id))
->assertSessionDoesntHaveErrors();
Livewire::test(TagsList::class)
->callTableAction('delete', $tag->id)
->assertSuccessful();
$this->assertDatabaseMissing('tags', [
'id' => $tag->id,
@ -61,20 +67,24 @@ public function test_create_tag_handles_invalid_color(): void
{
$this->actingAs($this->user);
$this->post(route('settings.tags.create'), [
'name' => 'test',
'color' => 'invalid-color',
])->assertSessionHasErrors('color');
Livewire::test(Index::class)
->callAction('create', [
'name' => 'test',
'color' => 'invalid-color',
])
->assertHasActionErrors();
}
public function test_create_tag_handles_invalid_name(): void
{
$this->actingAs($this->user);
$this->post(route('settings.tags.create'), [
'name' => '',
'color' => config('core.tag_colors')[0],
])->assertSessionHasErrors('name');
Livewire::test(Index::class)
->callAction('create', [
'name' => '',
'color' => config('core.tag_colors')[0],
])
->assertHasActionErrors();
}
public function test_edit_tag(): void
@ -85,11 +95,12 @@ public function test_edit_tag(): void
'project_id' => $this->user->current_project_id,
]);
$this->post(route('settings.tags.update', ['tag' => $tag]), [
'name' => 'New Name',
'color' => config('core.tag_colors')[1],
])
->assertSessionDoesntHaveErrors();
Livewire::test(TagsList::class)
->callTableAction('edit', $tag->id, [
'name' => 'New Name',
'color' => config('core.tag_colors')[1],
])
->assertSuccessful();
$this->assertDatabaseHas('tags', [
'id' => $tag->id,
@ -98,104 +109,101 @@ public function test_edit_tag(): void
]);
}
/**
* @dataProvider data
*/
public function test_attach_existing_tag_to_taggable(array $input): void
public function test_attach_existing_tag_to_server(): void
{
$this->actingAs($this->user);
$tag = Tag::factory()->create([
'project_id' => $this->user->current_project_id,
'name' => $input['name'],
'name' => 'staging',
]);
$input['taggable_id'] = match ($input['taggable_type']) {
Server::class => $this->server->id,
Site::class => $this->site->id,
default => $this->fail('Unknown taggable type'),
};
$this->post(route('tags.attach'), $input)->assertSessionDoesntHaveErrors();
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('tags.*', 'edit_tags', [
'tags' => [$tag->id],
])
->assertSuccessful();
$this->assertDatabaseHas('taggables', [
'taggable_id' => $input['taggable_id'],
'taggable_id' => $this->server->id,
'tag_id' => $tag->id,
]);
}
/**
* @dataProvider data
*/
public function test_attach_new_tag_to_taggable(array $input): void
{
$this->actingAs($this->user);
$input['taggable_id'] = match ($input['taggable_type']) {
Server::class => $this->server->id,
Site::class => $this->site->id,
default => $this->fail('Unknown taggable type'),
};
$this->post(route('tags.attach'), $input)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('tags', [
'name' => $input['name'],
]);
$tag = Tag::query()->where('name', $input['name'])->firstOrFail();
$this->assertDatabaseHas('taggables', [
'taggable_id' => $input['taggable_id'],
'tag_id' => $tag->id,
]);
}
/**
* @dataProvider data
*/
public function test_detach_tag(array $input): void
public function test_detach_tag_from_server(): void
{
$this->actingAs($this->user);
$tag = Tag::factory()->create([
'project_id' => $this->user->current_project_id,
'name' => $input['name'],
'name' => 'staging',
]);
$taggable = match ($input['taggable_type']) {
Server::class => $this->server,
Site::class => $this->site,
default => $this->fail('Unknown taggable type'),
};
$this->server->tags()->attach($tag);
$input['taggable_id'] = $taggable->id;
$taggable->tags()->attach($tag);
$this->post(route('tags.detach', $tag->id), $input)->assertSessionDoesntHaveErrors();
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('tags.*', 'edit_tags', [
'tags' => [],
])
->assertSuccessful();
$this->assertDatabaseMissing('taggables', [
'taggable_id' => $input['taggable_id'],
'taggable_id' => $this->server->id,
'tag_id' => $tag->id,
]);
}
public static function data(): array
public function test_attach_existing_tag_to_site(): void
{
return [
[
[
'taggable_type' => Server::class,
'name' => 'staging',
],
],
[
[
'taggable_type' => Site::class,
'name' => 'production',
],
],
];
$this->actingAs($this->user);
$tag = Tag::factory()->create([
'project_id' => $this->user->current_project_id,
'name' => 'staging',
]);
Livewire::test(SiteDetails::class, [
'server' => $this->server,
'site' => $this->site,
])
->callInfolistAction('tags.*', 'edit_tags', [
'tags' => [$tag->id],
])
->assertSuccessful();
$this->assertDatabaseHas('taggables', [
'taggable_id' => $this->site->id,
'tag_id' => $tag->id,
]);
}
public function test_detach_tag_from_site(): void
{
$this->actingAs($this->user);
$tag = Tag::factory()->create([
'project_id' => $this->user->current_project_id,
'name' => 'staging',
]);
$this->site->tags()->attach($tag);
Livewire::test(SiteDetails::class, [
'server' => $this->server,
'site' => $this->site,
])
->callInfolistAction('tags.*', 'edit_tags', [
'tags' => [],
])
->assertSuccessful();
$this->assertDatabaseMissing('taggables', [
'taggable_id' => $this->site->id,
'tag_id' => $tag->id,
]);
}
}