migrating tests (Metrics, NotificationChannels, PHP, Profile and Projects)

This commit is contained in:
Saeed Vaziry 2024-10-10 23:24:07 +02:00
parent 93cee92568
commit 7086e84c3c
16 changed files with 292 additions and 212 deletions

View File

@ -3,15 +3,11 @@
namespace App\Actions\Monitoring; namespace App\Actions\Monitoring;
use App\Models\Server; use App\Models\Server;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class UpdateMetricSettings class UpdateMetricSettings
{ {
public function update(Server $server, array $input): void public function update(Server $server, array $input): void
{ {
$this->validate($input);
$service = $server->monitoring(); $service = $server->monitoring();
$data = $service->handler()->data(); $data = $service->handler()->data();
@ -20,13 +16,14 @@ public function update(Server $server, array $input): void
$service->save(); $service->save();
} }
private function validate(array $input): void public static function rules(): array
{ {
Validator::make($input, [ return [
'data_retention' => [ 'data_retention' => [
'required', 'required',
Rule::in(config('core.metrics_data_retention')), 'numeric',
'min:1',
], ],
])->validate(); ];
} }
} }

View File

@ -4,6 +4,7 @@
use App\Models\NotificationChannel; use App\Models\NotificationChannel;
use App\Models\User; use App\Models\User;
use Exception;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@ -11,6 +12,7 @@ class AddChannel
{ {
/** /**
* @throws ValidationException * @throws ValidationException
* @throws Exception
*/ */
public function add(User $user, array $input): void public function add(User $user, array $input): void
{ {
@ -23,18 +25,24 @@ public function add(User $user, array $input): void
$channel->data = $channel->provider()->createData($input); $channel->data = $channel->provider()->createData($input);
$channel->save(); $channel->save();
if (! $channel->provider()->connect()) { try {
$channel->delete(); if (! $channel->provider()->connect()) {
$channel->delete();
if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) {
throw ValidationException::withMessages([
'email' => __('Could not connect! Make sure you configured `.env` file correctly.'),
]);
}
if ($channel->provider === \App\Enums\NotificationChannel::EMAIL) {
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'email' => __('Could not connect! Make sure you configured `.env` file correctly.'), 'provider' => __('Could not connect'),
]); ]);
} }
} catch (Exception $e) {
$channel->delete();
throw ValidationException::withMessages([ throw $e;
'provider' => __('Could not connect'),
]);
} }
$channel->connected = true; $channel->connected = true;

View File

@ -14,31 +14,36 @@ class MetricPolicy
public function viewAny(User $user, Server $server): bool public function viewAny(User $user, Server $server): bool
{ {
return ($user->isAdmin() || $server->project->users->contains($user)) && return ($user->isAdmin() || $server->project->users->contains($user)) &&
$server->service('monitoring'); $server->service('monitoring') &&
$server->isReady();
} }
public function view(User $user, Metric $metric): bool public function view(User $user, Metric $metric): bool
{ {
return ($user->isAdmin() || $metric->server->project->users->contains($user)) && return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
$metric->server->service('monitoring'); $metric->server->service('monitoring') &&
$metric->server->isReady();
} }
public function create(User $user, Server $server): bool public function create(User $user, Server $server): bool
{ {
return ($user->isAdmin() || $server->project->users->contains($user)) && return ($user->isAdmin() || $server->project->users->contains($user)) &&
$server->service('monitoring'); $server->service('monitoring') &&
$server->isReady();
} }
public function update(User $user, Metric $metric): bool public function update(User $user, Metric $metric): bool
{ {
return ($user->isAdmin() || $metric->server->project->users->contains($user)) && return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
$metric->server->service('monitoring'); $metric->server->service('monitoring') &&
$metric->server->isReady();
} }
public function delete(User $user, Metric $metric): bool public function delete(User $user, Metric $metric): bool
{ {
return ($user->isAdmin() || $metric->server->project->users->contains($user)) && return ($user->isAdmin() || $metric->server->project->users->contains($user)) &&
$metric->server->service('monitoring'); $metric->server->service('monitoring') &&
$metric->server->isReady();
} }
} }

View File

@ -2,8 +2,13 @@
namespace App\Web\Pages\Servers\Metrics; namespace App\Web\Pages\Servers\Metrics;
use App\Actions\Monitoring\UpdateMetricSettings;
use App\Models\Metric; use App\Models\Metric;
use App\Web\Pages\Servers\Page; use App\Web\Pages\Servers\Page;
use Filament\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
class Index extends Page class Index extends Page
{ {
@ -26,6 +31,37 @@ public function getWidgets(): array
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return []; return [
Action::make('data-retention')
->button()
->color('gray')
->icon('heroicon-o-trash')
->label('Data Retention')
->modalWidth(MaxWidth::Large)
->form([
Select::make('data_retention')
->options([
7 => '7 days',
14 => '14 days',
30 => '30 days',
60 => '60 days',
90 => '90 days',
180 => '180 days',
365 => '365 days',
])
->rules(UpdateMetricSettings::rules()['data_retention'])
->label('Data Retention')
->default($this->server->monitoring()?->type_data['data_retention'] ?? 30),
])
->modalSubmitActionLabel('Save')
->action(function (array $data) {
app(UpdateMetricSettings::class)->update($this->server, $data);
Notification::make()
->success()
->title('Data retention updated')
->send();
}),
];
} }
} }

View File

@ -7,8 +7,9 @@
use App\Web\Pages\Servers\Page; use App\Web\Pages\Servers\Page;
use App\Web\Pages\Servers\PHP\Widgets\PHPList; use App\Web\Pages\Servers\PHP\Widgets\PHPList;
use Filament\Actions\Action; use Filament\Actions\Action;
use Filament\Actions\ActionGroup; use Filament\Forms\Components\Select;
use Filament\Support\Enums\IconPosition; use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
class Index extends Page class Index extends Page
{ {
@ -30,31 +31,35 @@ public function getWidgets(): array
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
$phps = []; $installedPHPs = $this->server->installedPHPVersions();
foreach (config('core.php_versions') as $version) {
if (! $this->server->service('php', $version) && $version !== 'none') {
$phps[] = Action::make($version)
->label($version)
->requiresConfirmation()
->modalHeading('Install PHP '.$version)
->modalSubmitActionLabel('Install')
->action(function () use ($version) {
app(InstallNewPHP::class)->install($this->server, ['version' => $version]);
$this->dispatch('$refresh');
});
}
}
return [ return [
ActionGroup::make($phps) Action::make('install')
->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server])) ->authorize(fn () => auth()->user()?->can('create', [Service::class, $this->server]))
->label('Install PHP') ->label('Install PHP')
->icon('heroicon-o-chevron-up-down') ->icon('heroicon-o-archive-box-arrow-down')
->iconPosition(IconPosition::After) ->modalWidth(MaxWidth::Large)
->dropdownPlacement('bottom-end') ->form([
->color('primary') Select::make('version')
->button(), ->options(
collect(config('core.php_versions'))
->filter(fn ($version) => ! in_array($version, $installedPHPs))
->mapWithKeys(fn ($version) => [$version => $version])
->toArray()
)
->rules(InstallNewPHP::rules($this->server)['version']),
])
->modalSubmitActionLabel('Install')
->action(function (array $data) {
app(InstallNewPHP::class)->install($this->server, $data);
Notification::make()
->success()
->title('Installing PHP...')
->send();
$this->dispatch('$refresh');
}),
]; ];
} }
} }

View File

@ -43,9 +43,13 @@ protected function getHeaderActions(): array
->authorize('create', NotificationChannel::class) ->authorize('create', NotificationChannel::class)
->modalWidth(MaxWidth::Large) ->modalWidth(MaxWidth::Large)
->action(function (array $data) { ->action(function (array $data) {
Actions\Create::action($data); try {
Actions\Create::action($data);
$this->dispatch('$refresh'); $this->dispatch('$refresh');
} catch (\Exception) {
$this->halt();
}
}), }),
]; ];
} }

View File

@ -70,7 +70,7 @@ public function getTable(): Table
->modalHeading('Delete Notification Channel') ->modalHeading('Delete Notification Channel')
->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record)) ->authorize(fn (NotificationChannel $record) => auth()->user()->can('delete', $record))
->using(function (array $data, NotificationChannel $record) { ->using(function (array $data, NotificationChannel $record) {
// $record->delete();
}), }),
]); ]);
} }

View File

@ -5,7 +5,7 @@
use App\Actions\Projects\CreateProject; use App\Actions\Projects\CreateProject;
use App\Models\Project; use App\Models\Project;
use App\Web\Components\Page; use App\Web\Components\Page;
use Filament\Actions\CreateAction; use Filament\Actions\Action;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Support\Enums\MaxWidth; use Filament\Support\Enums\MaxWidth;
@ -42,13 +42,11 @@ public function getWidgets(): array
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [
CreateAction::make() Action::make('create')
->label('Create Project') ->label('Create Project')
->icon('heroicon-o-plus') ->icon('heroicon-o-plus')
->authorize('create', Project::class) ->authorize('create', Project::class)
->using(function (array $data) { ->modalWidth(MaxWidth::Large)
return app(CreateProject::class)->create(auth()->user(), $data);
})
->form(function (Form $form) { ->form(function (Form $form) {
return $form->schema([ return $form->schema([
TextInput::make('name') TextInput::make('name')
@ -56,8 +54,11 @@ protected function getHeaderActions(): array
->rules(CreateProject::rules()['name']), ->rules(CreateProject::rules()['name']),
])->columns(1); ])->columns(1);
}) })
->createAnother(false) ->action(function (array $data) {
->modalWidth(MaxWidth::Large), app(CreateProject::class)->create(auth()->user(), $data);
$this->dispatch('$refresh');
}),
]; ];
} }
} }

View File

@ -9,7 +9,7 @@
use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList; use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList;
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject; use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
use Exception; use Exception;
use Filament\Actions\DeleteAction; use Filament\Actions\Action;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Illuminate\Contracts\Support\Htmlable; use Illuminate\Contracts\Support\Htmlable;
@ -54,17 +54,24 @@ public function getTitle(): string|Htmlable
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [
DeleteAction::make() Action::make('delete')
->record($this->project) ->record($this->project)
->color('danger')
->label('Delete Project') ->label('Delete Project')
->icon('heroicon-o-trash') ->icon('heroicon-o-trash')
->modalHeading('Delete Project') ->modalHeading('Delete Project')
->modalDescription('Are you sure you want to delete this project? This action will delete all associated data and cannot be undone.') ->modalDescription('Are you sure you want to delete this project? This action will delete all associated data and cannot be undone.')
->using(function (Project $record) { ->requiresConfirmation()
->action(function (Project $record) {
try { try {
app(DeleteProject::class)->delete(auth()->user(), $record); app(DeleteProject::class)->delete(auth()->user(), $record);
$this->redirectRoute(Index::getUrl()); Notification::make()
->success()
->title('Project deleted successfully.')
->send();
$this->redirect(Index::getUrl());
} catch (Exception $e) { } catch (Exception $e) {
Notification::make() Notification::make()
->title($e->getMessage()) ->title($e->getMessage())

View File

@ -12,6 +12,8 @@
class ProjectsList extends Widget class ProjectsList extends Widget
{ {
protected $listeners = ['$refresh'];
protected function getTableQuery(): Builder protected function getTableQuery(): Builder
{ {
return Project::query(); return Project::query();

View File

@ -185,7 +185,6 @@
/* /*
* Package Service Providers... * Package Service Providers...
*/ */
Illuminate\Concurrency\ConcurrencyServiceProvider::class,
/* /*
* Application Service Providers... * Application Service Providers...

View File

@ -4,7 +4,9 @@
use App\Enums\ServiceStatus; use App\Enums\ServiceStatus;
use App\Models\Service; use App\Models\Service;
use App\Web\Pages\Servers\Metrics\Index;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class MetricsTest extends TestCase class MetricsTest extends TestCase
@ -23,7 +25,7 @@ public function test_visit_metrics(): void
'status' => ServiceStatus::READY, 'status' => ServiceStatus::READY,
]); ]);
$this->get(route('servers.metrics', ['server' => $this->server])) $this->get(Index::getUrl(['server' => $this->server]))
->assertSuccessful() ->assertSuccessful()
->assertSee('CPU Load') ->assertSee('CPU Load')
->assertSee('Memory Usage') ->assertSee('Memory Usage')
@ -34,8 +36,8 @@ public function test_cannot_visit_metrics(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->get(route('servers.metrics', ['server' => $this->server])) $this->get(Index::getUrl(['server' => $this->server]))
->assertNotFound(); ->assertForbidden();
} }
public function test_update_data_retention(): void public function test_update_data_retention(): void
@ -50,14 +52,18 @@ public function test_update_data_retention(): void
'status' => ServiceStatus::READY, 'status' => ServiceStatus::READY,
]); ]);
$this->post(route('servers.metrics.settings', ['server' => $this->server]), [ Livewire::test(Index::class, [
'data_retention' => 30, 'server' => $this->server,
])->assertSessionHas('toast.type', 'success'); ])
->callAction('data-retention', [
'data_retention' => 365,
])
->assertSuccessful();
$this->assertDatabaseHas('services', [ $this->assertDatabaseHas('services', [
'server_id' => $this->server->id, 'server_id' => $this->server->id,
'type' => 'monitoring', 'type' => 'monitoring',
'type_data->data_retention' => 30, 'type_data->data_retention' => 365,
]); ]);
} }
} }

View File

@ -3,9 +3,12 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Enums\NotificationChannel; use App\Enums\NotificationChannel;
use App\Web\Pages\Settings\NotificationChannels\Index;
use App\Web\Pages\Settings\NotificationChannels\Widgets\NotificationChannelsList;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class NotificationChannelsTest extends TestCase class NotificationChannelsTest extends TestCase
@ -17,12 +20,14 @@ public function test_add_email_channel(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::EMAIL, ->callAction('add', [
'email' => 'email@example.com', 'provider' => NotificationChannel::EMAIL,
'label' => 'Email', 'email' => 'email@example.com',
'global' => 1, 'label' => 'Email',
])->assertSessionDoesntHaveErrors(); 'global' => true,
])
->assertSuccessful();
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -42,11 +47,14 @@ public function test_cannot_add_email_channel(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::EMAIL, ->callAction('add', [
'email' => 'email@example.com', 'provider' => NotificationChannel::EMAIL,
'label' => 'Email', 'email' => 'email@example.com',
])->assertSessionHasErrors(); 'label' => 'Email',
'global' => true,
])
->assertNotified('Could not connect! Make sure you configured `.env` file correctly.');
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -63,11 +71,13 @@ public function test_add_slack_channel(): void
Http::fake(); Http::fake();
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::SLACK, ->callAction('add', [
'webhook_url' => 'https://hooks.slack.com/services/123/token', 'provider' => NotificationChannel::SLACK,
'label' => 'Slack', 'webhook_url' => 'https://hooks.slack.com/services/123/token',
])->assertSessionDoesntHaveErrors(); 'label' => 'Slack',
])
->assertSuccessful();
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -86,11 +96,13 @@ public function test_cannot_add_slack_channel(): void
'slack.com/*' => Http::response(['ok' => false], 401), 'slack.com/*' => Http::response(['ok' => false], 401),
]); ]);
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::SLACK, ->callAction('add', [
'webhook_url' => 'https://hooks.slack.com/services/123/token', 'provider' => NotificationChannel::SLACK,
'label' => 'Slack', 'webhook_url' => 'https://hooks.slack.com/services/123/token',
])->assertSessionHasErrors(); 'label' => 'Slack',
])
->assertNotified('Could not connect');
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -106,11 +118,13 @@ public function test_add_discord_channel(): void
Http::fake(); Http::fake();
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::DISCORD, ->callAction('add', [
'webhook_url' => 'https://discord.com/api/webhooks/123/token', 'provider' => NotificationChannel::DISCORD,
'label' => 'Discord', 'webhook_url' => 'https://discord.com/api/webhooks/123/token',
])->assertSessionDoesntHaveErrors(); 'label' => 'Discord',
])
->assertSuccessful();
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -129,11 +143,13 @@ public function test_cannot_add_discord_channel(): void
'discord.com/*' => Http::response(['ok' => false], 401), 'discord.com/*' => Http::response(['ok' => false], 401),
]); ]);
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::DISCORD, ->callAction('add', [
'webhook_url' => 'https://discord.com/api/webhooks/123/token', 'provider' => NotificationChannel::DISCORD,
'label' => 'Discord', 'webhook_url' => 'https://discord.com/api/webhooks/123/token',
])->assertSessionHasErrors(); 'label' => 'Discord',
])
->assertNotified('Could not connect');
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -149,12 +165,14 @@ public function test_add_telegram_channel(): void
Http::fake(); Http::fake();
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::TELEGRAM, ->callAction('add', [
'bot_token' => 'token', 'provider' => NotificationChannel::TELEGRAM,
'chat_id' => '123', 'bot_token' => 'token',
'label' => 'Telegram', 'chat_id' => '123',
])->assertSessionDoesntHaveErrors(); 'label' => 'Telegram',
])
->assertSuccessful();
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -174,12 +192,14 @@ public function test_cannot_add_telegram_channel(): void
'api.telegram.org/*' => Http::response(['ok' => false], 401), 'api.telegram.org/*' => Http::response(['ok' => false], 401),
]); ]);
$this->post(route('settings.notification-channels.add'), [ Livewire::test(Index::class)
'provider' => NotificationChannel::TELEGRAM, ->callAction('add', [
'bot_token' => 'token', 'provider' => NotificationChannel::TELEGRAM,
'chat_id' => '123', 'bot_token' => 'token',
'label' => 'Telegram', 'chat_id' => '123',
])->assertSessionHasErrors(); 'label' => 'Telegram',
])
->assertNotified('Could not connect');
/** @var \App\Models\NotificationChannel $channel */ /** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query() $channel = \App\Models\NotificationChannel::query()
@ -193,11 +213,12 @@ public function test_see_channels_list(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::factory()->create(); $channel = \App\Models\NotificationChannel::factory()->create();
$this->get(route('settings.notification-channels')) $this->get(Index::getUrl())
->assertSuccessful() ->assertSuccessful()
->assertSee($channel->provider); ->assertSee($channel->label);
} }
public function test_delete_channel(): void public function test_delete_channel(): void
@ -206,8 +227,9 @@ public function test_delete_channel(): void
$channel = \App\Models\NotificationChannel::factory()->create(); $channel = \App\Models\NotificationChannel::factory()->create();
$this->delete(route('settings.notification-channels.delete', $channel->id)) Livewire::test(NotificationChannelsList::class)
->assertSessionDoesntHaveErrors(); ->callTableAction('delete', $channel->id)
->assertSuccessful();
$this->assertDatabaseMissing('notification_channels', [ $this->assertDatabaseMissing('notification_channels', [
'id' => $channel->id, 'id' => $channel->id,

View File

@ -6,7 +6,10 @@
use App\Enums\ServiceStatus; use App\Enums\ServiceStatus;
use App\Facades\SSH; use App\Facades\SSH;
use App\Models\Service; use App\Models\Service;
use App\Web\Pages\Servers\PHP\Index;
use App\Web\Pages\Servers\PHP\Widgets\PHPList;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class PHPTest extends TestCase class PHPTest extends TestCase
@ -19,10 +22,11 @@ public function test_install_new_php(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.php.install', [ Livewire::test(Index::class, ['server' => $this->server])
'server' => $this->server, ->callAction('install', [
'version' => '8.1', 'version' => '8.1',
]))->assertSessionDoesntHaveErrors(); ])
->assertSuccessful();
$this->assertDatabaseHas('services', [ $this->assertDatabaseHas('services', [
'server_id' => $this->server->id, 'server_id' => $this->server->id,
@ -52,26 +56,17 @@ public function test_uninstall_php(): void
]); ]);
$php->save(); $php->save();
$this->delete(route('servers.php.uninstall', [ Livewire::test(PHPList::class, [
'server' => $this->server, 'server' => $this->server,
'version' => '8.1', ])
]))->assertSessionDoesntHaveErrors(); ->callTableAction('uninstall', $php->id)
->assertSuccessful();
$this->assertDatabaseMissing('services', [ $this->assertDatabaseMissing('services', [
'id' => $php->id, 'id' => $php->id,
]); ]);
} }
public function test_cannot_uninstall_php(): void
{
$this->actingAs($this->user);
$this->delete(route('servers.php.uninstall', [
'server' => $this->server,
'version' => '8.2',
]))->assertSessionHasErrors();
}
public function test_change_default_php_cli(): void public function test_change_default_php_cli(): void
{ {
SSH::fake(); SSH::fake();
@ -87,12 +82,14 @@ public function test_change_default_php_cli(): void
'name' => 'php', 'name' => 'php',
'version' => '8.1', 'version' => '8.1',
'status' => ServiceStatus::READY, 'status' => ServiceStatus::READY,
'is_default' => false,
]); ]);
$this->post(route('servers.php.default-cli', [ Livewire::test(PHPList::class, [
'server' => $this->server, 'server' => $this->server,
'version' => '8.1', ])
]))->assertSessionDoesntHaveErrors(); ->callTableAction('default-php-cli', $php->id)
->assertSuccessful();
$php->refresh(); $php->refresh();
@ -105,38 +102,19 @@ public function test_install_extension(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('servers.php.install-extension', [ Livewire::test(PHPList::class, [
'server' => $this->server, 'server' => $this->server,
'version' => '8.2', ])
'extension' => 'gmp', ->callTableAction('install-extension', $this->server->php()->id, [
]))->assertSessionDoesntHaveErrors(); 'extension' => 'gmp',
])
->assertSuccessful();
$php = $this->server->php('8.2'); $php = $this->server->php('8.2');
$this->assertContains('gmp', $php->type_data['extensions']); $this->assertContains('gmp', $php->type_data['extensions']);
} }
public function test_extension_already_installed(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->server->php('8.2')->update([
'type_data' => [
'extensions' => [
'gmp',
],
],
]);
$this->post(route('servers.php.install-extension', [
'server' => $this->server,
'version' => '8.2',
'extension' => 'gmp',
]))->assertSessionHasErrors();
}
/** /**
* @dataProvider php_ini_data * @dataProvider php_ini_data
*/ */
@ -146,31 +124,13 @@ public function test_get_php_ini(string $version, string $type): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this->get(route('servers.php.get-ini', [ Livewire::test(PHPList::class, [
'server' => $this->server, 'server' => $this->server,
'version' => $version, ])
'type' => $type, ->callTableAction('php-ini-'.$type, $this->server->php()->id, [
]))->assertSessionHas('ini'); 'ini' => 'new-ini',
} ])
->assertSuccessful();
/**
* @dataProvider php_ini_data
*/
public function test_update_php_ini(string $version, string $type): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.php.update-ini', [
'server' => $this->server,
'version' => $version,
'type' => $type,
'ini' => 'new ini',
]))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'success')
->assertSessionHas('toast.message', __('PHP ini (:type) updated!', ['type' => $type]));
} }
public static function php_ini_data(): array public static function php_ini_data(): array

View File

@ -2,8 +2,12 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Web\Pages\Settings\Profile\Index;
use App\Web\Pages\Settings\Profile\Widgets\ProfileInformation;
use App\Web\Pages\Settings\Profile\Widgets\UpdatePassword;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class ProfileTest extends TestCase class ProfileTest extends TestCase
@ -15,7 +19,7 @@ public function test_profile_page_is_displayed(): void
$this->actingAs($this->user); $this->actingAs($this->user);
$this $this
->get(route('profile')) ->get(Index::getUrl())
->assertSuccessful() ->assertSuccessful()
->assertSee('Profile Information') ->assertSee('Profile Information')
->assertSee('Update Password') ->assertSee('Update Password')
@ -26,11 +30,13 @@ public function test_profile_information_can_be_updated(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('profile.info'), [ Livewire::test(ProfileInformation::class)
'name' => 'Test', ->fill([
'email' => 'test@example.com', 'name' => 'Test',
'timezone' => 'Europe/Berlin', 'email' => 'test@example.com',
]); 'timezone' => 'Europe/Berlin',
])
->call('submit');
$this->user->refresh(); $this->user->refresh();
@ -43,11 +49,13 @@ public function test_password_can_be_updated(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('profile.password'), [ Livewire::test(UpdatePassword::class)
'current_password' => 'password', ->fill([
'password' => 'new-password', 'current_password' => 'password',
'password_confirmation' => 'new-password', 'password' => 'new-password',
]); 'password_confirmation' => 'new-password',
])
->call('submit');
$this->assertTrue(Hash::check('new-password', $this->user->refresh()->password)); $this->assertTrue(Hash::check('new-password', $this->user->refresh()->password));
} }
@ -56,10 +64,13 @@ public function test_correct_password_must_be_provided_to_update_password(): voi
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('profile.password'), [ Livewire::test(UpdatePassword::class)
'current_password' => 'wrong-password', ->fill([
'password' => 'new-password', 'current_password' => 'wrong-password',
'password_confirmation' => 'new-password', 'password' => 'new-password',
])->assertSessionHasErrors('current_password'); 'password_confirmation' => 'new-password',
])
->call('submit')
->assertHasErrors('current_password');
} }
} }

View File

@ -3,7 +3,11 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Models\Project; use App\Models\Project;
use App\Web\Pages\Settings\Projects\Index;
use App\Web\Pages\Settings\Projects\Settings;
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase; use Tests\TestCase;
class ProjectsTest extends TestCase class ProjectsTest extends TestCase
@ -14,9 +18,11 @@ public function test_create_project(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->post(route('settings.projects.create'), [ Livewire::test(Index::class)
'name' => 'test', ->callAction('create', [
])->assertSessionDoesntHaveErrors(); 'name' => 'test',
])
->assertSuccessful();
$this->assertDatabaseHas('projects', [ $this->assertDatabaseHas('projects', [
'name' => 'test', 'name' => 'test',
@ -31,7 +37,7 @@ public function test_see_projects_list(): void
$this->user->projects()->attach($project); $this->user->projects()->attach($project);
$this->get(route('settings.projects')) $this->get(Index::getUrl())
->assertSuccessful() ->assertSuccessful()
->assertSee($project->name); ->assertSee($project->name);
} }
@ -44,8 +50,11 @@ public function test_delete_project(): void
$this->user->projects()->attach($project); $this->user->projects()->attach($project);
$this->delete(route('settings.projects.delete', $project)) Livewire::test(Settings::class, [
->assertSessionDoesntHaveErrors(); 'project' => $project,
])
->callAction('delete')
->assertSuccessful();
$this->assertDatabaseMissing('projects', [ $this->assertDatabaseMissing('projects', [
'id' => $project->id, 'id' => $project->id,
@ -60,9 +69,14 @@ public function test_edit_project(): void
$this->user->projects()->attach($project); $this->user->projects()->attach($project);
$this->post(route('settings.projects.update', $project), [ Livewire::test(UpdateProject::class, [
'name' => 'new-name', 'project' => $project,
])->assertSessionDoesntHaveErrors(); ])
->fill([
'name' => 'new-name',
])
->call('submit')
->assertSuccessful();
$this->assertDatabaseHas('projects', [ $this->assertDatabaseHas('projects', [
'id' => $project->id, 'id' => $project->id,
@ -74,11 +88,14 @@ public function test_cannot_delete_last_project(): void
{ {
$this->actingAs($this->user); $this->actingAs($this->user);
$this->delete(route('settings.projects.delete', [ Livewire::test(Settings::class, [
'project' => $this->user->currentProject, 'project' => $this->user->currentProject,
])) ])
->assertSessionDoesntHaveErrors() ->callAction('delete')
->assertSessionHas('toast.type', 'error') ->assertNotified('Cannot delete the last project.');
->assertSessionHas('toast.message', 'Cannot delete the last project.');
$this->assertDatabaseHas('projects', [
'id' => $this->user->currentProject->id,
]);
} }
} }