This commit is contained in:
Saeed Vaziry
2025-06-04 08:08:20 +02:00
parent efacadba10
commit c3f69f3247
114 changed files with 4032 additions and 765 deletions

View File

@ -6,46 +6,40 @@
use App\Facades\SSH;
use App\Models\GitHook;
use App\Notifications\DeploymentCompleted;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\DeploymentsList;
use Exception;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Notification;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class ApplicationTest extends TestCase
{
use RefreshDatabase;
public function test_visit_application()
public function test_visit_application(): void
{
$this->actingAs($this->user);
$this->get(
View::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
->assertSuccessful()
->assertSee($this->site->domain)
->assertSee('Deployments')
->assertSee('Actions');
}
public function test_update_deployment_script()
{
$this->actingAs($this->user);
Livewire::test(View::class, [
$this->get(route('application', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertInertia(fn (AssertableInertia $page) => $page->component('application/index'));
}
public function test_update_deployment_script(): void
{
$this->actingAs($this->user);
$this->put(route('application.update-deployment-script', [
'server' => $this->server,
'site' => $this->site,
]), [
'script' => 'some script',
])
->callAction('deployment-script', [
'script' => 'some script',
])
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('deployment_scripts', [
'site_id' => $this->site->id,
@ -53,6 +47,9 @@ public function test_update_deployment_script()
]);
}
/**
* @throws Exception
*/
public function test_deploy(): void
{
SSH::fake('fake output');
@ -75,13 +72,11 @@ public function test_deploy(): void
$this->actingAs($this->user);
Livewire::test(View::class, [
$this->post(route('application.deploy', [
'server' => $this->server,
'site' => $this->site,
])
->callAction('deploy')
->assertSuccessful()
->assertNotified('Deployment started!');
]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('deployments', [
'site_id' => $this->site->id,
@ -92,49 +87,9 @@ public function test_deploy(): void
SSH::assertExecutedContains('git pull');
Notification::assertSentTo($this->notificationChannel, DeploymentCompleted::class);
$this->get(
View::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
->assertSuccessful()
->assertSee('test commit message');
$deployment = $this->site->deployments()->first();
Livewire::test(DeploymentsList::class, [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('view', $deployment->id)
->assertSuccessful();
}
public function test_change_branch()
{
SSH::fake();
$this->actingAs($this->user);
Livewire::test(View::class, [
'server' => $this->server,
'site' => $this->site,
])
->callAction('branch', [
'branch' => 'master',
])
->assertSuccessful()
->assertNotified('Branch updated!');
$this->site->refresh();
$this->assertEquals('master', $this->site->branch);
SSH::assertExecutedContains('git checkout -f master');
}
public function test_enable_auto_deployment()
public function test_enable_auto_deployment(): void
{
Http::fake([
'github.com/*' => Http::response([
@ -144,20 +99,17 @@ public function test_enable_auto_deployment()
$this->actingAs($this->user);
Livewire::test(View::class, [
$this->post(route('application.enable-auto-deployment', [
'server' => $this->server,
'site' => $this->site,
])
->callAction('auto-deployment')
->assertSuccessful()
->assertNotified('Auto deployment enabled!');
]))->assertSessionDoesntHaveErrors();
$this->site->refresh();
$this->assertTrue($this->site->isAutoDeployment());
}
public function test_disable_auto_deployment()
public function test_disable_auto_deployment(): void
{
Http::fake([
'api.github.com/repos/organization/repository' => Http::response([
@ -173,13 +125,10 @@ public function test_disable_auto_deployment()
'source_control_id' => $this->site->source_control_id,
]);
Livewire::test(View::class, [
$this->post(route('application.disable-auto-deployment', [
'server' => $this->server,
'site' => $this->site,
])
->callAction('auto-deployment')
->assertSuccessful()
->assertNotified('Auto deployment disabled!');
]))->assertSessionDoesntHaveErrors();
$this->site->refresh();
@ -192,21 +141,18 @@ public function test_update_env_file(): void
$this->actingAs($this->user);
Livewire::test(View::class, [
$this->put(route('application.update-env', [
'server' => $this->server,
'site' => $this->site,
]), [
'env' => 'APP_ENV="production"',
])
->callAction('dot-env', [
'env' => 'APP_ENV="production"',
])
->assertSuccessful()
->assertNotified('.env updated!');
SSH::assertExecutedContains('tee /home/vito/vito.test/.env << \'VITO_SSH_EOF\'');
SSH::assertExecutedContains('APP_ENV="production"');
->assertSessionDoesntHaveErrors();
}
/**
* @param array<string, mixed> $webhook
* @param array<string, mixed> $payload
* @dataProvider hookData
*/
public function test_git_hook_deployment(string $provider, array $webhook, string $url, array $payload, bool $skip): void
@ -288,6 +234,9 @@ public function test_git_hook_deployment_invalid_secret(): void
]);
}
/**
* @return array<array<int, mixed>>
*/
public static function hookData(): array
{
return [

View File

@ -3,10 +3,9 @@
namespace Tests\Feature;
use App\Facades\SSH;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\Commands;
use App\Models\Command;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class CommandsTest extends TestCase
@ -17,28 +16,26 @@ public function test_see_commands(): void
{
$this->actingAs($this->user);
$this->get(
View::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
$this->get(route('commands', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee($this->site->domain)
->assertSee('Commands');
->assertInertia(fn (AssertableInertia $page) => $page->component('commands/index'));
}
public function test_create_command(): void
{
$this->actingAs($this->user);
Livewire::test(Commands::class, ['site' => $this->site])
->assertTableHeaderActionsExistInOrder(['new-command'])
->callTableAction('new-command', null, [
'name' => 'Test Command',
'command' => 'echo "${MESSAGE}"',
])
->assertSuccessful();
$this->post(route('commands.store', [
'server' => $this->server,
'site' => $this->site,
]), [
'name' => 'Test Command',
'command' => 'echo "${MESSAGE}"',
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('commands', [
'site_id' => $this->site->id,
@ -56,12 +53,15 @@ public function test_edit_command(): void
'command' => 'echo "${MESSAGE}"',
]);
Livewire::test(Commands::class, ['site' => $this->site])
->callTableAction('edit', $command->id, [
'name' => 'Updated Command',
'command' => 'ls -la',
])
->assertSuccessful();
$this->put(route('commands.update', [
'server' => $this->server,
'site' => $this->site,
'command' => $command,
]), [
'name' => 'Updated Command',
'command' => 'ls -la',
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('commands', [
'id' => $command->id,
@ -80,9 +80,12 @@ public function test_delete_command(): void
'command' => 'echo "${MESSAGE}"',
]);
Livewire::test(Commands::class, ['site' => $this->site])
->callTableAction('delete', $command->id)
->assertSuccessful();
$this->delete(route('commands.destroy', [
'server' => $this->server,
'site' => $this->site,
'command' => $command,
]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('commands', [
'id' => $command->id,
@ -95,18 +98,20 @@ public function test_execute_command(): void
$this->actingAs($this->user);
/** @var Command $command */
$command = $this->site->commands()->create([
'name' => 'Test Command',
'command' => 'echo "${MESSAGE}"',
]);
Livewire::test(Commands::class, ['site' => $this->site])
->callTableAction('execute', $command->id, [
'variables' => [
'MESSAGE' => 'Hello, world!',
],
])
->assertSuccessful();
$this->post(route('commands.execute', [
'server' => $this->server,
'site' => $this->site,
'command' => $command,
]), [
'MESSAGE' => 'Hello, world!',
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('command_executions', [
'command_id' => $command->id,
@ -123,8 +128,11 @@ public function test_execute_command_validation_error(): void
'command' => 'echo "${MESSAGE}"',
]);
Livewire::test(Commands::class, ['site' => $this->site])
->callTableAction('execute', $command->id, [])
->assertHasActionErrors();
$this->post(route('commands.execute', [
'server' => $this->server,
'site' => $this->site,
'command' => $command,
]))
->assertSessionHasErrors();
}
}

View File

@ -1,138 +0,0 @@
<?php
namespace Tests\Feature;
use App\Facades\SSH;
use App\Models\File;
use App\Web\Pages\Servers\FileManager\Index;
use App\Web\Pages\Servers\FileManager\Widgets\FilesList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Livewire\Livewire;
use Tests\TestCase;
class FileManagerTest extends TestCase
{
use RefreshDatabase;
public function test_see_files(): void
{
SSH::fake(<<<'EOF'
total 32
drwxr-xr-x 7 vito vito 4096 Feb 2 19:42 .
drwxr-xr-x 3 root root 4096 Feb 1 18:44 ..
drwx------ 3 vito vito 4096 Feb 1 18:45 .cache
drwxrwxr-x 3 vito vito 4096 Feb 1 18:45 .config
-rw-rw-r-- 1 vito vito 82 Feb 2 14:13 .gitconfig
drwxrwxr-x 3 vito vito 4096 Feb 1 18:45 .local
drwxr-xr-x 2 vito vito 4096 Feb 2 14:13 .ssh
drwxrwxr-x 3 vito vito 4096 Feb 2 21:25 test.vitodeploy.com
EOF
);
$this->actingAs($this->user);
$this->get(
Index::getUrl([
'server' => $this->server,
])
)
->assertSuccessful()
->assertSee('.cache')
->assertSee('.config');
}
public function test_upload_file(): void
{
SSH::fake();
$this->actingAs($this->user);
Livewire::test(FilesList::class, [
'server' => $this->server,
])
->callTableAction('upload', null, [
'file' => UploadedFile::fake()->create('test.txt'),
])
->assertSuccessful();
}
public function test_create_file(): void
{
SSH::fake(<<<'EOF'
total 3
drwxr-xr-x 7 vito vito 4096 Feb 2 19:42 .
drwxr-xr-x 3 root root 4096 Feb 1 18:44 ..
-rw-rw-r-- 1 vito vito 82 Feb 2 14:13 test.txt
EOF
);
$this->actingAs($this->user);
Livewire::test(FilesList::class, [
'server' => $this->server,
])
->callTableAction('new-file', null, [
'name' => 'test.txt',
'content' => 'Hello, world!',
])
->assertSuccessful();
$this->assertDatabaseHas('files', [
'name' => 'test.txt',
]);
}
public function test_create_directory(): void
{
SSH::fake(<<<'EOF'
total 3
drwxr-xr-x 7 vito vito 4096 Feb 2 19:42 .
drwxr-xr-x 3 root root 4096 Feb 1 18:44 ..
drwxr-xr-x 2 vito vito 4096 Feb 2 14:13 test
EOF
);
$this->actingAs($this->user);
Livewire::test(FilesList::class, [
'server' => $this->server,
])
->callTableAction('new-directory', null, [
'name' => 'test',
])
->assertSuccessful();
$this->assertDatabaseHas('files', [
'name' => 'test',
]);
}
public function test_download_file(): void
{
SSH::fake(<<<'EOF'
total 3
drwxr-xr-x 7 vito vito 4096 Feb 2 19:42 .
drwxr-xr-x 3 root root 4096 Feb 1 18:44 ..
-rw-rw-r-- 1 vito vito 82 Feb 2 14:13 test.txt
EOF
);
$this->actingAs($this->user);
$this->get(
Index::getUrl([
'server' => $this->server,
])
)->assertSuccessful();
$file = File::query()->where('name', 'test.txt')->firstOrFail();
Livewire::test(FilesList::class, [
'server' => $this->server,
])
->assertTableActionVisible('download', $file)
->callTableAction('download', $file)
->assertSuccessful();
}
}

View File

@ -5,10 +5,7 @@
use App\Enums\LoadBalancerMethod;
use App\Facades\SSH;
use App\Models\Server;
use App\Web\Pages\Servers\Sites\View;
use App\Web\Pages\Servers\Sites\Widgets\LoadBalancerServers;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
use Tests\Traits\PrepareLoadBalancer;
@ -24,22 +21,7 @@ protected function setUp(): void
$this->prepare();
}
public function test_visit_load_balancer_servers(): void
{
$this->actingAs($this->user);
$this->get(
View::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
->assertSuccessful()
->assertSee($this->site->domain)
->assertSee('Load Balancer Servers');
}
public function test_update_load_balancer_servers()
public function test_update_load_balancer_servers(): void
{
SSH::fake();
@ -48,29 +30,27 @@ public function test_update_load_balancer_servers()
$servers = Server::query()->where('id', '!=', $this->server->id)->get();
$this->assertEquals(2, $servers->count());
Livewire::test(LoadBalancerServers::class, [
'site' => $this->site,
])
->assertFormExists()
->fillForm([
'method' => LoadBalancerMethod::ROUND_ROBIN,
'servers' => [
[
'server' => $servers[0]->local_ip,
'port' => 80,
'weight' => 1,
'backup' => false,
],
[
'server' => $servers[1]->local_ip,
'port' => 80,
'weight' => 1,
'backup' => false,
],
$this->post(route('application.update-load-balancer', [
'server' => $this->server->id,
'site' => $this->site->id,
]), [
'method' => LoadBalancerMethod::ROUND_ROBIN,
'servers' => [
[
'server' => $servers[0]->local_ip,
'port' => 80,
'weight' => 1,
'backup' => false,
],
])
->call('save')
->assertSuccessful();
[
'server' => $servers[1]->local_ip,
'port' => 80,
'weight' => 1,
'backup' => false,
],
],
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('load_balancer_servers', [
'load_balancer_id' => $this->site->id,

View File

@ -5,10 +5,8 @@
use App\Enums\RedirectStatus;
use App\Facades\SSH;
use App\Models\Redirect;
use App\Web\Pages\Servers\Sites\Pages\Redirects\Index;
use App\Web\Pages\Servers\Sites\Pages\Redirects\Widgets\RedirectsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class RedirectsTest extends TestCase
@ -19,18 +17,17 @@ public function test_see_redirects(): void
{
$this->actingAs($this->user);
$redirect = Redirect::factory()->create([
Redirect::factory()->create([
'site_id' => $this->site->id,
]);
$this->get(
Index::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
$this->get(route('redirects', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee($redirect->from);
->assertInertia(fn (AssertableInertia $page) => $page->component('redirects/index'));
}
public function test_delete_redirect(): void
@ -43,12 +40,12 @@ public function test_delete_redirect(): void
'site_id' => $this->site->id,
]);
Livewire::test(RedirectsList::class, [
$this->delete(route('redirects.destroy', [
'server' => $this->server,
'site' => $this->site,
])
->callTableAction('delete', $redirect->id)
->assertSuccessful();
'redirect' => $redirect,
]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('redirects', [
'id' => $redirect->id,
@ -61,16 +58,15 @@ public function test_create_redirect(): void
$this->actingAs($this->user);
Livewire::test(Index::class, [
$this->post(route('redirects.store', [
'server' => $this->server,
'site' => $this->site,
]), [
'from' => 'some-path',
'to' => 'https://example.com/redirect',
'mode' => 301,
])
->callAction('create', [
'from' => 'some-path',
'to' => 'https://example.com/redirect',
'mode' => 301,
])
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('redirects', [
'from' => 'some-path',

View File

@ -10,16 +10,9 @@
use App\Enums\Webserver;
use App\Facades\SSH;
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
@ -32,18 +25,17 @@ public function test_create_regular_server(): void
SSH::fake('Active: active'); // fake output for service installations
Livewire::test(Index::class)
->callAction('create', [
'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->post(route('servers.store', [
'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();
$this->assertDatabaseHas('servers', [
'name' => 'test',
@ -89,18 +81,17 @@ public function test_create_regular_server_with_caddy(): void
SSH::fake('Active: active'); // fake output for service installations
Livewire::test(Index::class)
->callAction('create', [
'provider' => ServerProvider::CUSTOM,
'name' => 'caddy-test',
'ip' => '2.2.2.2',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'webserver' => Webserver::CADDY,
'database' => Database::MYSQL80,
'php' => '8.2',
])
->assertSuccessful();
$this->post(route('servers.store', [
'provider' => ServerProvider::CUSTOM,
'name' => 'caddy-test',
'ip' => '2.2.2.2',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'webserver' => Webserver::CADDY,
'database' => Database::MYSQL80,
'php' => '8.2',
]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'name' => 'caddy-test',
@ -146,11 +137,10 @@ public function test_delete_server(): void
SSH::fake();
Livewire::test(Settings::class, [
'server' => $this->server,
])->callAction('delete')
->assertSuccessful()
->assertRedirect(Index::getUrl());
$this->delete(route('servers.destroy', $this->server), [
'name' => $this->server->name,
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
@ -183,11 +173,10 @@ public function test_cannot_delete_on_provider(): void
],
]);
Livewire::test(Settings::class, [
'server' => $this->server,
])->callAction('delete')
->assertSuccessful()
->assertRedirect(Index::getUrl());
$this->delete(route('servers.destroy', $this->server), [
'name' => $this->server->name,
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
@ -204,12 +193,8 @@ public function test_check_connection_is_ready(): void
$this->server->update(['status' => ServerStatus::DISCONNECTED]);
Livewire::test(ServerSummary::class, [
'server' => $this->server,
])
->callInfolistAction('status', 'check-status')
->assertSuccessful()
->assertNotified('Server is '.ServerStatus::READY);
$this->patch(route('servers.status', $this->server))
->assertSessionHas('success', 'Server status is '.ServerStatus::READY);
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -225,12 +210,8 @@ public function test_connection_failed(): void
$this->server->update(['status' => ServerStatus::READY]);
Livewire::test(ServerSummary::class, [
'server' => $this->server,
])
->callInfolistAction('status', 'check-status')
->assertSuccessful()
->assertNotified('Server is '.ServerStatus::DISCONNECTED);
$this->patch(route('servers.status', $this->server))
->assertSessionHas('gray', 'Server status is '.ServerStatus::DISCONNECTED);
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -244,11 +225,8 @@ public function test_reboot_server(): void
$this->actingAs($this->user);
Livewire::test(Settings::class, [
'server' => $this->server,
])
->callAction('reboot')
->assertSuccessful();
$this->post(route('servers.reboot', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -262,16 +240,12 @@ public function test_edit_server(): void
$this->actingAs($this->user);
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
$this->patch(route('server-settings.update', $this->server), [
'name' => 'new-name',
'ip' => $this->server->ip,
'port' => $this->server->port,
])
->fill([
'name' => 'new-name',
'ip' => $this->server->ip,
'port' => $this->server->port,
])
->call('submit')
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -285,16 +259,12 @@ public function test_edit_server_ip_address(): void
$this->actingAs($this->user);
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
$this->patch(route('server-settings.update', $this->server), [
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => $this->server->port,
])
->fill([
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => $this->server->port,
])
->call('submit')
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -309,16 +279,12 @@ public function test_edit_server_ip_address_and_disconnect(): void
$this->actingAs($this->user);
Livewire::test(UpdateServerInfo::class, [
'server' => $this->server,
$this->patch(route('server-settings.update', $this->server), [
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => 2222,
])
->fill([
'name' => $this->server->name,
'ip' => '2.2.2.2',
'port' => 2222,
])
->call('submit')
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
@ -334,17 +300,8 @@ public function test_check_updates(): void
$this->actingAs($this->user);
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('last_updated_check', 'check-update')
->assertSuccessful()
->assertNotified(
Notification::make()
->info()
->title('Available updates:')
->body(9)
);
$this->post(route('servers.check-for-updates', $this->server))
->assertSessionDoesntHaveErrors();
$this->server->refresh();
$this->assertEquals(9, $this->server->updates);
@ -356,11 +313,8 @@ public function test_update_server(): void
$this->actingAs($this->user);
Livewire::test(ServerDetails::class, [
'server' => $this->server,
])
->callInfolistAction('updates', 'update-server')
->assertSuccessful();
$this->post(route('servers.update', $this->server))
->assertSessionDoesntHaveErrors();
$this->server->refresh();

View File

@ -1,37 +0,0 @@
<?php
namespace Tests\Feature;
use App\Models\ServerLog;
use App\Web\Pages\Servers\Sites\Pages\Logs\Index;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* This uses the server logs.
* All scenarios are covered in \Tests\Feature\LogsTest
*/
class SiteLogsTest extends TestCase
{
use RefreshDatabase;
public function test_see_logs()
{
$this->actingAs($this->user);
/** @var ServerLog $lastLog */
$lastLog = ServerLog::factory()->create([
'server_id' => $this->server->id,
'site_id' => $this->site->id,
]);
$this->get(
Index::getUrl([
'server' => $this->server,
'site' => $this->site,
])
)
->assertSuccessful()
->assertSee($lastLog->name);
}
}

View File

@ -8,13 +8,9 @@
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 Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class SitesTest extends TestCase
@ -22,6 +18,8 @@ class SitesTest extends TestCase
use RefreshDatabase;
/**
* @param array<string, mixed> $inputs
*
* @dataProvider create_data
*/
public function test_create_site(array $inputs): void
@ -42,12 +40,8 @@ public function test_create_site(array $inputs): void
$inputs['source_control'] = $sourceControl->id;
Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('create', $inputs)
->assertHasNoActionErrors()
->assertSuccessful();
$this->post(route('sites.store', ['server' => $this->server]), $inputs)
->assertSessionDoesntHaveErrors();
$expectedUser = empty($inputs['user']) ? $this->server->getSshUser() : $inputs['user'];
$this->assertDatabaseHas('sites', [
@ -60,6 +54,8 @@ public function test_create_site(array $inputs): void
}
/**
* @param array<string, mixed> $inputs
*
* @dataProvider failure_create_data
*/
public function test_isolated_user_failure(array $inputs): void
@ -67,11 +63,8 @@ public function test_isolated_user_failure(array $inputs): void
SSH::fake();
$this->actingAs($this->user);
Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('create', $inputs)
->assertHasActionErrors();
$this->post(route('sites.store', ['server' => $this->server]), $inputs)
->assertSessionHasErrors();
}
/**
@ -106,12 +99,8 @@ public function test_create_site_failed_due_to_source_control(int $status): void
$inputs['source_control'] = $sourceControl->id;
Livewire::test(Index::class, [
'server' => $this->server,
])
->callAction('create', $inputs)
->assertNotified()
->assertSuccessful();
$this->post(route('sites.store', ['server' => $this->server]), $inputs)
->assertSessionHasErrors();
$this->assertDatabaseMissing('sites', [
'domain' => 'example.com',
@ -123,13 +112,15 @@ public function test_see_sites_list(): void
{
$this->actingAs($this->user);
$site = Site::factory()->create([
Site::factory()->create([
'server_id' => $this->server->id,
]);
$this->get(Index::getUrl(['server' => $this->server]))
$this->get(route('sites', [
'server' => $this->server,
]))
->assertSuccessful()
->assertSee($site->domain);
->assertInertia(fn (AssertableInertia $page) => $page->component('sites/index'));
}
public function test_delete_site(): void
@ -142,13 +133,13 @@ public function test_delete_site(): void
'server_id' => $this->server->id,
]);
Livewire::test(Settings::class, [
'server' => $this->server,
'site' => $site,
$this->delete(route('site-settings.destroy', [
'server' => $this->server->id,
'site' => $site->id,
]), [
'domain' => $site->domain,
])
->callAction('delete')
->assertHasNoActionErrors()
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('sites', [
'id' => $site->id,
@ -165,14 +156,13 @@ public function test_change_php_version(): void
'server_id' => $this->server->id,
]);
Livewire::test(SiteDetails::class, [
'site' => $site,
$this->delete(route('site-settings.update-php-version', [
'server' => $this->server->id,
'site' => $site->id,
]), [
'version' => '8.2',
])
->callInfolistAction('php_version', 'edit_php_version', [
'version' => '8.2',
])
->assertHasNoActionErrors()
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$site->refresh();
@ -195,14 +185,13 @@ public function test_update_source_control(): void
'provider' => SourceControl::GITHUB,
]);
Livewire::test(SiteDetails::class, [
$this->patch(route('site-settings.update-source-control', [
'server' => $this->server->id,
'site' => $this->site,
]), [
'source_control' => $sourceControl->id,
])
->callInfolistAction('source_control_id', 'edit_source_control', [
'source_control' => $sourceControl->id,
])
->assertHasNoActionErrors()
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->site->refresh();
@ -225,13 +214,13 @@ public function test_failed_to_update_source_control(): void
'provider' => SourceControl::GITHUB,
]);
Livewire::test(SiteDetails::class, [
$this->patch(route('site-settings.update-source-control', [
'server' => $this->server->id,
'site' => $this->site,
]), [
'source_control' => $sourceControl->id,
])
->callInfolistAction('source_control_id', 'edit_source_control', [
'source_control' => $sourceControl->id,
])
->assertNotified('Repository not found');
->assertSessionHasErrors();
}
public function test_update_v_host(): void
@ -240,32 +229,54 @@ public function test_update_v_host(): void
$this->actingAs($this->user);
$site = Site::factory()->create([
Site::factory()->create([
'server_id' => $this->server->id,
]);
Livewire::test(Settings::class, [
'server' => $this->server,
'site' => $site,
$this->patch(route('site-settings.update-vhost', [
'server' => $this->server->id,
'site' => $this->site,
]), [
'vhost' => 'test',
])
->callAction('vhost', [
'vhost' => 'test',
])
->assertNotified('VHost updated!');
->assertSessionDoesntHaveErrors();
}
public function test_see_logs(): void
{
$this->actingAs($this->user);
$this->get(View::getUrl([
$this->get(route('sites.logs', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee('Logs');
->assertInertia(fn (AssertableInertia $page) => $page->component('sites/logs'));
}
public function test_change_branch(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->patch(route('site-settings.update-branch', [
'server' => $this->server->id,
'site' => $this->site,
]), [
'branch' => 'master',
])
->assertSessionDoesntHaveErrors();
$this->site->refresh();
$this->assertEquals('master', $this->site->branch);
SSH::assertExecutedContains('git checkout -f master');
}
/**
* @return array<array<string, mixed>>
*/
public static function failure_create_data(): array
{
return [
@ -322,6 +333,9 @@ public static function failure_create_data(): array
];
}
/**
* @return array<array<array<string, mixed>>>
*/
public static function create_data(): array
{
return [
@ -435,6 +449,9 @@ public static function create_data(): array
];
}
/**
* @return array<array<int>>
*/
public static function create_failure_data(): array
{
return [

View File

@ -6,47 +6,45 @@
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 Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class SslTest extends TestCase
{
use RefreshDatabase;
public function test_see_ssls_list()
public function test_see_ssls_list(): void
{
$this->actingAs($this->user);
$ssl = Ssl::factory()->create([
Ssl::factory()->create([
'site_id' => $this->site->id,
]);
$this->get(Index::getUrl([
$this->get(route('ssls', [
'server' => $this->server,
'site' => $this->site,
]))
->assertSuccessful()
->assertSee($ssl->type);
->assertInertia(fn (AssertableInertia $page) => $page->component('ssls/index'));
}
public function test_letsencrypt_ssl()
public function test_letsencrypt_ssl(): void
{
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
$this->post(route('ssls.store', [
'server' => $this->server->id,
'site' => $this->site->id,
]), [
'type' => SslType::LETSENCRYPT,
'email' => 'ssl@example.com',
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
'email' => 'ssl@example.com',
])
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$ssl = Ssl::query()->where('site_id', $this->site->id)->first();
$this->assertNotEmpty($ssl);
@ -62,22 +60,21 @@ public function test_letsencrypt_ssl()
]);
}
public function test_letsencrypt_ssl_with_aliases()
public function test_letsencrypt_ssl_with_aliases(): void
{
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
$this->post(route('ssls.store', [
'server' => $this->server->id,
'site' => $this->site->id,
]), [
'type' => SslType::LETSENCRYPT,
'email' => 'ssl@example.com',
'aliases' => true,
])
->callAction('create', [
'type' => SslType::LETSENCRYPT,
'email' => 'ssl@example.com',
'aliases' => true,
])
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
@ -88,23 +85,22 @@ public function test_letsencrypt_ssl_with_aliases()
]);
}
public function test_custom_ssl()
public function test_custom_ssl(): void
{
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
Livewire::test(Index::class, [
'server' => $this->server,
'site' => $this->site,
$this->post(route('ssls.store', [
'server' => $this->server->id,
'site' => $this->site->id,
]), [
'type' => SslType::CUSTOM,
'certificate' => 'certificate',
'private' => 'private',
'expires_at' => now()->addYear()->format('Y-m-d'),
])
->callAction('create', [
'type' => SslType::CUSTOM,
'certificate' => 'certificate',
'private' => 'private',
'expires_at' => now()->addYear()->format('Y-m-d'),
])
->assertSuccessful();
->assertSessionDoesntHaveErrors();
$ssl = Ssl::query()->where('site_id', $this->site->id)->first();
$this->assertNotEmpty($ssl);
@ -119,7 +115,7 @@ public function test_custom_ssl()
]);
}
public function test_delete_ssl()
public function test_delete_ssl(): void
{
SSH::fake();
@ -129,11 +125,11 @@ public function test_delete_ssl()
'site_id' => $this->site->id,
]);
Livewire::test(SslsList::class, [
'site' => $this->site,
])
->callTableAction('delete', $ssl->id)
->assertSuccessful();
$this->delete(route('ssls.destroy', [
'server' => $this->server->id,
'site' => $this->site->id,
'ssl' => $ssl->id,
]))->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('ssls', [
'id' => $ssl->id,