This commit is contained in:
Saeed Vaziry
2024-03-24 09:56:34 +01:00
committed by GitHub
parent 884f18db63
commit 4d051330d6
1055 changed files with 14493 additions and 20278 deletions

View File

@ -2,17 +2,11 @@
namespace Tests\Feature;
use App\Http\Livewire\Application\AutoDeployment;
use App\Http\Livewire\Application\ChangeBranch;
use App\Http\Livewire\Application\Deploy;
use App\Http\Livewire\Application\DeploymentScript;
use App\Http\Livewire\Application\LaravelApp;
use App\Jobs\Site\UpdateBranch;
use App\Enums\DeploymentStatus;
use App\Facades\SSH;
use App\Models\GitHook;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class ApplicationTest extends TestCase
@ -30,44 +24,103 @@ public function test_visit_application()
])
)
->assertOk()
->assertSeeLivewire(LaravelApp::class);
->assertSee($this->site->domain);
}
public function test_update_deployment_script()
{
$this->actingAs($this->user);
Livewire::test(Deploy::class, ['site' => $this->site])
->assertDontSeeText('Deploy');
Livewire::test(DeploymentScript::class, ['site' => $this->site])
->set('script', 'some script')
->call('save')
->assertSuccessful();
$this->post(
route('servers.sites.application.deployment-script', [
'server' => $this->server,
'site' => $this->site,
]),
[
'script' => 'some script',
]
)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('deployment_scripts', [
'site_id' => $this->site->id,
'content' => 'some script',
]);
}
$this->site->refresh();
public function test_deploy(): void
{
SSH::fake();
Http::fake([
'github.com/*' => Http::response([
'sha' => '123',
'commit' => [
'message' => 'test commit message',
'name' => 'test commit name',
'email' => 'test@example.com',
'url' => 'https://github.com/commit-url',
],
], 200),
]);
Livewire::test(Deploy::class, ['site' => $this->site])
->assertSeeText('Deploy');
$this->site->deploymentScript->update([
'content' => 'git pull',
]);
$this->actingAs($this->user);
$response = $this->post(route('servers.sites.application.deploy', [
'server' => $this->server,
'site' => $this->site,
]))->assertSessionDoesntHaveErrors();
$response->assertSessionHas('toast.type', 'success');
$response->assertSessionHas('toast.message', 'Deployment started!');
$this->assertDatabaseHas('deployments', [
'site_id' => $this->site->id,
'status' => DeploymentStatus::FINISHED,
]);
SSH::assertExecutedContains('cd /home/vito/'.$this->site->domain);
SSH::assertExecutedContains('git pull');
$this->get(route('servers.sites.show', [
'server' => $this->server,
'site' => $this->site,
]))
->assertOk()
->assertSee('test commit message');
$deployment = $this->site->deployments()->first();
$this->get(route('servers.sites.application.deployment.log', [
'server' => $this->server,
'site' => $this->site,
'deployment' => $deployment,
]))
->assertRedirect()
->assertSessionHas('content', 'fake output');
}
public function test_change_branch()
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
Livewire::test(ChangeBranch::class, ['site' => $this->site])
->set('branch', 'master')
->call('change')
->assertSuccessful();
$this->post(route('servers.sites.application.branch', [
'server' => $this->server,
'site' => $this->site,
]), [
'branch' => 'master',
])
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'success');
Bus::assertDispatched(UpdateBranch::class);
$this->site->refresh();
$this->assertEquals('master', $this->site->branch);
SSH::assertExecutedContains('git checkout -f master');
}
public function test_enable_auto_deployment()
@ -75,39 +128,126 @@ public function test_enable_auto_deployment()
Http::fake([
'github.com/*' => Http::response([
'id' => '123',
], 201),
], 200),
]);
$this->actingAs($this->user);
Livewire::test(AutoDeployment::class, ['site' => $this->site])
->call('enable')
->assertSuccessful();
$this->post(route('servers.sites.application.auto-deployment', [
'server' => $this->server,
'site' => $this->site,
]))->assertSessionDoesntHaveErrors();
$this->site->refresh();
$this->assertTrue($this->site->auto_deployment);
$this->assertTrue($this->site->isAutoDeployment());
}
public function test_disable_auto_deployment()
{
Http::fake([
'github.com/*' => Http::response([], 204),
'api.github.com/repos/organization/repository' => Http::response([
'id' => '123',
], 200),
'api.github.com/repos/organization/repository/hooks/*' => Http::response([], 204),
]);
$this->actingAs($this->user);
GitHook::factory()->create([
$hook = GitHook::factory()->create([
'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id,
]);
Livewire::test(AutoDeployment::class, ['site' => $this->site])
->call('disable')
->assertSuccessful();
$this->delete(route('servers.sites.application.auto-deployment', [
'server' => $this->server,
'site' => $this->site,
]))->assertSessionDoesntHaveErrors();
$this->site->refresh();
$this->assertFalse($this->site->auto_deployment);
$this->assertFalse($this->site->isAutoDeployment());
}
public function test_update_env_file(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.sites.application.env', [
'server' => $this->server,
'site' => $this->site,
]), [
'env' => 'APP_ENV=production',
])->assertSessionDoesntHaveErrors();
SSH::assertExecutedContains('echo "APP_ENV=production" | tee /home/vito/'.$this->site->domain.'/.env');
}
public function test_git_hook_deployment(): void
{
SSH::fake();
Http::fake([
'github.com/*' => Http::response([
'sha' => '123',
'commit' => [
'message' => 'test commit message',
'name' => 'test commit name',
'email' => 'user@example.com',
'url' => 'https://github.com',
],
], 200),
]);
$hook = GitHook::factory()->create([
'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id,
'secret' => 'secret',
'events' => ['push'],
'actions' => ['deploy'],
]);
$this->site->deploymentScript->update([
'content' => 'git pull',
]);
$this->post(route('git-hooks'), [
'secret' => 'secret',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('deployments', [
'site_id' => $this->site->id,
'deployment_script_id' => $this->site->deploymentScript->id,
'status' => DeploymentStatus::FINISHED,
]);
}
public function test_git_hook_deployment_invalid_secret(): void
{
SSH::fake();
Http::fake();
$hook = GitHook::factory()->create([
'site_id' => $this->site->id,
'source_control_id' => $this->site->source_control_id,
'secret' => 'secret',
'events' => ['push'],
'actions' => ['deploy'],
]);
$this->site->deploymentScript->update([
'content' => 'git pull',
]);
$this->post(route('git-hooks'), [
'secret' => 'invalid-secret',
])->assertNotFound();
$this->assertDatabaseMissing('deployments', [
'site_id' => $this->site->id,
'deployment_script_id' => $this->site->deploymentScript->id,
'status' => DeploymentStatus::FINISHED,
]);
}
}

View File

@ -37,4 +37,11 @@ public function test_users_can_not_authenticate_with_invalid_password(): void
$this->assertGuest();
}
public function test_redirect_if_not_authenticated(): void
{
$response = $this->get('/servers');
$response->assertRedirect('/login');
}
}

View File

@ -3,7 +3,6 @@
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use JsonException;
use Tests\TestCase;
class PasswordConfirmationTest extends TestCase
@ -19,9 +18,6 @@ public function test_confirm_password_screen_can_be_rendered(): void
$response->assertStatus(200);
}
/**
* @throws JsonException
*/
public function test_password_can_be_confirmed(): void
{
$this->actingAs($this->user);
@ -31,7 +27,7 @@ public function test_password_can_be_confirmed(): void
]);
$response->assertRedirect();
$response->assertSessionHasNoErrors();
$response->assertSessionDoesntHaveErrors();
}
public function test_password_is_not_confirmed_with_invalid_password(): void

View File

@ -56,7 +56,7 @@ public function test_password_can_be_reset_with_valid_token(): void
'password_confirmation' => 'password',
]);
$response->assertSessionHasNoErrors();
$response->assertSessionDoesntHaveErrors();
return true;
});

View File

@ -1,40 +0,0 @@
<?php
namespace Tests\Feature\Auth;
use App\Http\Livewire\Profile\UpdatePassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
use Tests\TestCase;
class PasswordUpdateTest extends TestCase
{
use RefreshDatabase;
public function test_password_can_be_updated(): void
{
$this->actingAs($this->user);
Livewire::test(UpdatePassword::class)
->set('current_password', 'password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('update')
->assertSuccessful();
$this->assertTrue(Hash::check('new-password', $this->user->refresh()->password));
}
public function test_correct_password_must_be_provided_to_update_password(): void
{
$this->actingAs($this->user);
Livewire::test(UpdatePassword::class)
->set('current_password', 'wrong-password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('update')
->assertHasErrors();
}
}

View File

@ -3,11 +3,9 @@
namespace Tests\Feature;
use App\Enums\CronjobStatus;
use App\Http\Livewire\Cronjobs\CreateCronjob;
use App\Http\Livewire\Cronjobs\CronjobsList;
use App\Facades\SSH;
use App\Models\CronJob;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class CronjobTest extends TestCase
@ -23,42 +21,81 @@ public function test_see_cronjobs_list()
'server_id' => $this->server->id,
]);
Livewire::test(CronjobsList::class, ['server' => $this->server])
->assertSeeText($cronjob->frequency_label);
$this->get(route('servers.cronjobs', $this->server))
->assertSeeText($cronjob->frequencyLabel());
}
public function test_delete_cronjob()
{
SSH::fake();
$this->actingAs($this->user);
/** @var CronJob $cronjob */
$cronjob = CronJob::factory()->create([
'server_id' => $this->server->id,
'user' => 'vito',
]);
Livewire::test(CronjobsList::class, ['server' => $this->server])
->set('deleteId', $cronjob->id)
->call('delete')
->assertDispatched('confirmed');
$this->delete(route('servers.cronjobs.destroy', [
'server' => $this->server,
'cronJob' => $cronjob,
]))->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('cron_jobs', [
'id' => $cronjob->id,
]);
SSH::assertExecutedContains("echo '' | sudo -u vito crontab -");
SSH::assertExecutedContains('sudo -u vito crontab -l');
}
public function test_create_cronjob()
{
SSH::fake();
$this->actingAs($this->user);
Livewire::test(CreateCronjob::class, ['server' => $this->server])
->set('command', 'ls -la')
->set('user', 'vito')
->set('frequency', '* * * * *')
->call('create')
->assertDispatched('created');
$this->post(route('servers.cronjobs.store', $this->server), [
'command' => 'ls -la',
'user' => 'vito',
'frequency' => '* * * * *',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('cron_jobs', [
'server_id' => $this->server->id,
'command' => 'ls -la',
'user' => 'vito',
'frequency' => '* * * * *',
'status' => CronjobStatus::CREATING,
'status' => CronjobStatus::READY,
]);
SSH::assertExecutedContains("echo '* * * * * ls -la' | sudo -u vito crontab -");
SSH::assertExecutedContains('sudo -u vito crontab -l');
}
public function test_create_custom_cronjob()
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.cronjobs.store', $this->server), [
'command' => 'ls -la',
'user' => 'vito',
'frequency' => 'custom',
'custom' => '* * * 1 1',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('cron_jobs', [
'server_id' => $this->server->id,
'command' => 'ls -la',
'user' => 'vito',
'frequency' => '* * * 1 1',
'status' => CronjobStatus::READY,
]);
SSH::assertExecutedContains("echo '* * * 1 1 ls -la' | sudo -u vito crontab -");
SSH::assertExecutedContains('sudo -u vito crontab -l');
}
}

View File

@ -2,16 +2,16 @@
namespace Tests\Feature;
use App\Actions\Database\RunBackup;
use App\Enums\BackupFileStatus;
use App\Enums\BackupStatus;
use App\Facades\SSH;
use App\Http\Livewire\Databases\DatabaseBackups;
use App\Jobs\Backup\RunBackup;
use App\Models\Backup;
use App\Models\Database;
use App\Models\StorageProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class DatabaseBackupTest extends TestCase
@ -20,12 +20,11 @@ class DatabaseBackupTest extends TestCase
public function test_create_backup(): void
{
SSH::fake();
Http::fake();
$this->actingAs($this->user);
Bus::fake();
SSH::fake()->outputShouldBe('test');
$database = Database::factory()->create([
'server_id' => $this->server,
]);
@ -35,15 +34,44 @@ public function test_create_backup(): void
'provider' => \App\Enums\StorageProvider::DROPBOX,
]);
Livewire::test(DatabaseBackups::class, ['server' => $this->server])
->set('database', $database->id)
->set('storage', $storage->id)
->set('interval', '0 * * * *')
->set('keep', '10')
->call('create')
->assertSuccessful();
$this->post(route('servers.databases.backups.store', $this->server), [
'backup_database' => $database->id,
'backup_storage' => $storage->id,
'backup_interval' => '0 * * * *',
'backup_keep' => '10',
])->assertSessionDoesntHaveErrors();
Bus::assertDispatched(RunBackup::class);
$this->assertDatabaseHas('backups', [
'status' => BackupStatus::RUNNING,
]);
$this->assertDatabaseHas('backup_files', [
'status' => BackupFileStatus::CREATED,
]);
}
public function test_create_custom_interval_backup(): void
{
Bus::fake();
$this->actingAs($this->user);
$database = Database::factory()->create([
'server_id' => $this->server,
]);
$storage = StorageProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => \App\Enums\StorageProvider::DROPBOX,
]);
$this->post(route('servers.databases.backups.store', $this->server), [
'backup_database' => $database->id,
'backup_storage' => $storage->id,
'backup_interval' => 'custom',
'backup_custom' => '* * * * *',
'backup_keep' => '10',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('backups', [
'status' => BackupStatus::RUNNING,
@ -69,13 +97,11 @@ public function test_see_backups_list(): void
'storage_id' => $storage->id,
]);
Livewire::test(DatabaseBackups::class, ['server' => $this->server])
->assertSee([
$backup->database->name,
]);
$this->get(route('servers.databases.backups', [$this->server, $backup]))
->assertSee($backup->database->name);
}
public function test_delete_database(): void
public function test_delete_backup(): void
{
$this->actingAs($this->user);
@ -94,12 +120,50 @@ public function test_delete_database(): void
'storage_id' => $storage->id,
]);
Livewire::test(DatabaseBackups::class, ['server' => $this->server])
->set('deleteId', $backup->id)
->call('delete');
$this->delete(route('servers.databases.backups.destroy', [$this->server, $backup]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('backups', [
'id' => $backup->id,
]);
}
public function test_restore_backup(): void
{
Http::fake();
SSH::fake();
$this->actingAs($this->user);
$database = Database::factory()->create([
'server_id' => $this->server,
]);
$storage = StorageProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => \App\Enums\StorageProvider::DROPBOX,
]);
$backup = Backup::factory()->create([
'server_id' => $this->server->id,
'database_id' => $database->id,
'storage_id' => $storage->id,
]);
$backupFile = app(RunBackup::class)->run($backup);
$this->post(route('servers.databases.backups.files.restore', [
'server' => $this->server,
'backup' => $backup,
'backupFile' => $backupFile,
]), [
'database' => $database->id,
])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('backup_files', [
'id' => $backupFile->id,
'status' => BackupFileStatus::RESTORED,
]);
}
}

View File

@ -3,14 +3,10 @@
namespace Tests\Feature;
use App\Enums\DatabaseStatus;
use App\Enums\DatabaseUserStatus;
use App\Facades\SSH;
use App\Http\Livewire\Databases\DatabaseList;
use App\Jobs\Database\CreateOnServer;
use App\Jobs\Database\DeleteFromServer;
use App\Models\Database;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class DatabaseTest extends TestCase
@ -21,20 +17,43 @@ public function test_create_database(): void
{
$this->actingAs($this->user);
Bus::fake();
SSH::fake();
SSH::fake()->outputShouldBe('test');
Livewire::test(DatabaseList::class, ['server' => $this->server])
->set('name', 'database')
->call('create')
->assertSuccessful();
Bus::assertDispatched(CreateOnServer::class);
$this->post(route('servers.databases.store', $this->server), [
'name' => 'database',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('databases', [
'name' => 'database',
'status' => DatabaseStatus::CREATING,
'status' => DatabaseStatus::READY,
]);
}
public function test_create_database_with_user(): void
{
$this->actingAs($this->user);
SSH::fake();
$this->post(route('servers.databases.store', $this->server), [
'name' => 'database',
'user' => 'on',
'username' => 'user',
'password' => 'password',
'remote' => 'on',
'host' => '%',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('databases', [
'name' => 'database',
'status' => DatabaseStatus::READY,
]);
$this->assertDatabaseHas('database_users', [
'username' => 'user',
'databases' => json_encode(['database']),
'host' => '%',
'status' => DatabaseUserStatus::READY,
]);
}
@ -46,31 +65,25 @@ public function test_see_databases_list(): void
'server_id' => $this->server,
]);
Livewire::test(DatabaseList::class, ['server' => $this->server])
->assertSee([
$database->name,
]);
$this->get(route('servers.databases', $this->server))
->assertSee($database->name);
}
public function test_delete_database(): void
{
$this->actingAs($this->user);
Bus::fake();
SSH::fake();
$database = Database::factory()->create([
'server_id' => $this->server,
]);
Livewire::test(DatabaseList::class, ['server' => $this->server])
->set('deleteId', $database->id)
->call('delete');
$this->delete(route('servers.databases.destroy', [$this->server, $database]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('databases', [
$this->assertDatabaseMissing('databases', [
'id' => $database->id,
'status' => DatabaseStatus::DELETING,
]);
Bus::assertDispatched(DeleteFromServer::class);
}
}

View File

@ -3,13 +3,9 @@
namespace Tests\Feature;
use App\Enums\DatabaseUserStatus;
use App\Http\Livewire\Databases\DatabaseUserList;
use App\Jobs\DatabaseUser\CreateOnServer;
use App\Jobs\DatabaseUser\DeleteFromServer;
use App\Facades\SSH;
use App\Models\DatabaseUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class DatabaseUserTest extends TestCase
@ -20,19 +16,36 @@ public function test_create_database_user(): void
{
$this->actingAs($this->user);
Bus::fake();
SSH::fake();
Livewire::test(DatabaseUserList::class, ['server' => $this->server])
->set('username', 'user')
->set('password', 'password')
->call('create')
->assertSuccessful();
Bus::assertDispatched(CreateOnServer::class);
$this->post(route('servers.databases.users.store', $this->server), [
'username' => 'user',
'password' => 'password',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('database_users', [
'username' => 'user',
'status' => DatabaseUserStatus::CREATING,
'status' => DatabaseUserStatus::READY,
]);
}
public function test_create_database_user_with_remote(): void
{
$this->actingAs($this->user);
SSH::fake();
$this->post(route('servers.databases.users.store', $this->server), [
'username' => 'user',
'password' => 'password',
'remote' => 'on',
'host' => '%',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('database_users', [
'username' => 'user',
'host' => '%',
'status' => DatabaseUserStatus::READY,
]);
}
@ -44,31 +57,47 @@ public function test_see_database_users_list(): void
'server_id' => $this->server,
]);
Livewire::test(DatabaseUserList::class, ['server' => $this->server])
->assertSee([
$databaseUser->username,
]);
$this->get(route('servers.databases', $this->server))
->assertSee($databaseUser->username);
}
public function test_delete_database_user(): void
{
$this->actingAs($this->user);
Bus::fake();
SSH::fake();
$databaseUser = DatabaseUser::factory()->create([
'server_id' => $this->server,
]);
Livewire::test(DatabaseUserList::class, ['server' => $this->server])
->set('deleteId', $databaseUser->id)
->call('delete');
$this->delete(route('servers.databases.users.destroy', [$this->server, $databaseUser]))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('database_users', [
$this->assertDatabaseMissing('database_users', [
'id' => $databaseUser->id,
'status' => DatabaseUserStatus::DELETING,
]);
}
public function test_unlink_database(): void
{
$this->actingAs($this->user);
SSH::fake();
$databaseUser = DatabaseUser::factory()->create([
'server_id' => $this->server,
]);
Bus::assertDispatched(DeleteFromServer::class);
$this->post(route('servers.databases.users.link', [
'server' => $this->server,
'databaseUser' => $databaseUser,
]), [])
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('database_users', [
'username' => $databaseUser->username,
'databases' => json_encode([]),
]);
}
}

View File

@ -3,14 +3,9 @@
namespace Tests\Feature;
use App\Enums\FirewallRuleStatus;
use App\Http\Livewire\Firewall\CreateFirewallRule;
use App\Http\Livewire\Firewall\FirewallRulesList;
use App\Jobs\Firewall\AddToServer;
use App\Jobs\Firewall\RemoveFromServer;
use App\Facades\SSH;
use App\Models\FirewallRule;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class FirewallTest extends TestCase
@ -19,23 +14,21 @@ class FirewallTest extends TestCase
public function test_create_firewall_rule(): void
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
Livewire::test(CreateFirewallRule::class, ['server' => $this->server])
->set('type', 'allow')
->set('protocol', 'tcp')
->set('port', '1234')
->set('source', '0.0.0.0')
->set('mask', '0')
->call('create')
->assertSuccessful();
Bus::assertDispatched(AddToServer::class);
$this->post(route('servers.firewall.store', $this->server), [
'type' => 'allow',
'protocol' => 'tcp',
'port' => '1234',
'source' => '0.0.0.0',
'mask' => '0',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('firewall_rules', [
'port' => '1234',
'status' => FirewallRuleStatus::READY,
]);
}
@ -47,16 +40,14 @@ public function test_see_firewall_rules(): void
'server_id' => $this->server->id,
]);
Livewire::test(FirewallRulesList::class, ['server' => $this->server])
->assertSee([
$rule->source,
$rule->port,
]);
$this->get(route('servers.firewall', $this->server))
->assertSee($rule->source)
->assertSee($rule->port);
}
public function test_delete_firewall_rule(): void
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -64,16 +55,13 @@ public function test_delete_firewall_rule(): void
'server_id' => $this->server->id,
]);
Livewire::test(FirewallRulesList::class, ['server' => $this->server])
->set('deleteId', $rule->id)
->call('delete')
->assertSuccessful();
$this->delete(route('servers.firewall.destroy', [
'server' => $this->server,
'firewallRule' => $rule,
]))->assertSessionDoesntHaveErrors();
Bus::assertDispatched(RemoveFromServer::class);
$this->assertDatabaseHas('firewall_rules', [
$this->assertDatabaseMissing('firewall_rules', [
'id' => $rule->id,
'status' => FirewallRuleStatus::DELETING,
]);
}
}

View File

@ -2,10 +2,8 @@
namespace Tests\Feature;
use App\Http\Livewire\ServerLogs\LogsList;
use App\Models\ServerLog;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class LogsTest extends TestCase
@ -22,9 +20,6 @@ public function test_see_logs()
]);
$this->get(route('servers.logs', $this->server))
->assertOk();
Livewire::test(LogsList::class, ['server' => $this->server])
->assertSeeText($log->type);
}
}

View File

@ -3,12 +3,9 @@
namespace Tests\Feature;
use App\Enums\NotificationChannel;
use App\Http\Livewire\NotificationChannels\AddChannel;
use App\Http\Livewire\NotificationChannels\ChannelsList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class NotificationChannelsTest extends TestCase
@ -20,20 +17,42 @@ public function test_add_email_channel(): void
{
$this->actingAs($this->user);
Livewire::test(AddChannel::class)
->set('provider', NotificationChannel::EMAIL)
->set('email', 'email@example.com')
->set('label', 'Email')
->call('add')
->assertSuccessful();
$this->assertDatabaseHas('notification_channels', [
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::EMAIL,
'data' => cast_to_json([
'email' => 'email@example.com',
]),
'connected' => 1,
]);
'email' => 'email@example.com',
'label' => 'Email',
])->assertSessionDoesntHaveErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::EMAIL)
->where('label', 'Email')
->first();
$this->assertEquals('email@example.com', $channel->data['email']);
$this->assertTrue($channel->connected);
}
public function test_cannot_add_email_channel(): void
{
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp.host', '127.0.0.1'); // invalid host
$this->actingAs($this->user);
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::EMAIL,
'email' => 'email@example.com',
'label' => 'Email',
])->assertSessionHasErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::EMAIL)
->where('label', 'Email')
->first();
$this->assertNull($channel);
}
public function test_add_slack_channel(): void
@ -42,20 +61,41 @@ public function test_add_slack_channel(): void
Http::fake();
Livewire::test(AddChannel::class)
->set('provider', NotificationChannel::SLACK)
->set('label', 'Slack')
->set('webhook_url', 'https://hooks.slack.com/services/123/token')
->call('add')
->assertSuccessful();
$this->assertDatabaseHas('notification_channels', [
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::SLACK,
'data' => cast_to_json([
'webhook_url' => 'https://hooks.slack.com/services/123/token',
]),
'connected' => 1,
'webhook_url' => 'https://hooks.slack.com/services/123/token',
'label' => 'Slack',
])->assertSessionDoesntHaveErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::SLACK)
->first();
$this->assertEquals('https://hooks.slack.com/services/123/token', $channel->data['webhook_url']);
$this->assertTrue($channel->connected);
}
public function test_cannot_add_slack_channel(): void
{
$this->actingAs($this->user);
Http::fake([
'slack.com/*' => Http::response(['ok' => false], 401),
]);
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::SLACK,
'webhook_url' => 'https://hooks.slack.com/services/123/token',
'label' => 'Slack',
])->assertSessionHasErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::SLACK)
->first();
$this->assertNull($channel);
}
public function test_add_discord_channel(): void
@ -64,20 +104,41 @@ public function test_add_discord_channel(): void
Http::fake();
Livewire::test(AddChannel::class)
->set('provider', NotificationChannel::DISCORD)
->set('label', 'Discord')
->set('webhook_url', 'https://discord.com/api/webhooks/123/token')
->call('add')
->assertSuccessful();
$this->assertDatabaseHas('notification_channels', [
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::DISCORD,
'data' => cast_to_json([
'webhook_url' => 'https://discord.com/api/webhooks/123/token',
]),
'connected' => 1,
'webhook_url' => 'https://discord.com/api/webhooks/123/token',
'label' => 'Discord',
])->assertSessionDoesntHaveErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::DISCORD)
->first();
$this->assertEquals('https://discord.com/api/webhooks/123/token', $channel->data['webhook_url']);
$this->assertTrue($channel->connected);
}
public function test_cannot_add_discord_channel(): void
{
$this->actingAs($this->user);
Http::fake([
'discord.com/*' => Http::response(['ok' => false], 401),
]);
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::DISCORD,
'webhook_url' => 'https://discord.com/api/webhooks/123/token',
'label' => 'Discord',
])->assertSessionHasErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::DISCORD)
->first();
$this->assertNull($channel);
}
public function test_add_telegram_channel(): void
@ -86,22 +147,44 @@ public function test_add_telegram_channel(): void
Http::fake();
Livewire::test(AddChannel::class)
->set('provider', NotificationChannel::TELEGRAM)
->set('label', 'Telegram')
->set('bot_token', 'token')
->set('chat_id', '123')
->call('add')
->assertSuccessful();
$this->assertDatabaseHas('notification_channels', [
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::TELEGRAM,
'data' => cast_to_json([
'chat_id' => '123',
'bot_token' => 'token',
]),
'connected' => 1,
'bot_token' => 'token',
'chat_id' => '123',
'label' => 'Telegram',
])->assertSessionDoesntHaveErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::TELEGRAM)
->first();
$this->assertEquals('123', $channel->data['chat_id']);
$this->assertEquals('token', $channel->data['bot_token']);
$this->assertTrue($channel->connected);
}
public function test_cannot_add_telegram_channel(): void
{
$this->actingAs($this->user);
Http::fake([
'api.telegram.org/*' => Http::response(['ok' => false], 401),
]);
$this->post(route('notification-channels.add'), [
'provider' => NotificationChannel::TELEGRAM,
'bot_token' => 'token',
'chat_id' => '123',
'label' => 'Telegram',
])->assertSessionHasErrors();
/** @var \App\Models\NotificationChannel $channel */
$channel = \App\Models\NotificationChannel::query()
->where('provider', NotificationChannel::TELEGRAM)
->first();
$this->assertNull($channel);
}
public function test_see_channels_list(): void
@ -110,10 +193,8 @@ public function test_see_channels_list(): void
$channel = \App\Models\NotificationChannel::factory()->create();
Livewire::test(ChannelsList::class)
->assertSee([
$channel->provider,
]);
$this->get(route('notification-channels'))
->assertSee($channel->provider);
}
public function test_delete_channel(): void
@ -122,10 +203,8 @@ public function test_delete_channel(): void
$channel = \App\Models\NotificationChannel::factory()->create();
Livewire::test(ChannelsList::class)
->set('deleteId', $channel->id)
->call('delete')
->assertSuccessful();
$this->delete(route('notification-channels.delete', $channel->id))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('notification_channels', [
'id' => $channel->id,

View File

@ -1,110 +0,0 @@
<?php
namespace Tests\Feature;
use App\Enums\ServiceStatus;
use App\Http\Livewire\Php\DefaultCli;
use App\Http\Livewire\Php\InstalledVersions;
use App\Jobs\Installation\InstallPHP;
use App\Jobs\Installation\UninstallPHP;
use App\Jobs\PHP\InstallPHPExtension;
use App\Jobs\PHP\SetDefaultCli;
use App\Models\Service;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class PHP extends TestCase
{
use RefreshDatabase;
public function test_install_new_php(): void
{
Bus::fake();
$this->actingAs($this->user);
Livewire::test(InstalledVersions::class, ['server' => $this->server])
->call('install', '8.1')
->assertSuccessful();
Bus::assertDispatched(InstallPHP::class);
}
public function test_uninstall_new_php(): void
{
Bus::fake();
$this->actingAs($this->user);
Livewire::test(InstalledVersions::class, ['server' => $this->server])
->set('uninstallId', $this->server->php('8.2')?->id)
->call('uninstall')
->assertSuccessful();
Bus::assertDispatched(UninstallPHP::class);
}
public function test_change_default_php_cli(): void
{
Bus::fake();
$this->actingAs($this->user);
Service::factory()->create([
'server_id' => $this->server->id,
'type' => 'php',
'type_data' => [
'extensions' => [],
],
'name' => 'php',
'version' => '8.1',
'status' => ServiceStatus::READY,
]);
Livewire::test(DefaultCli::class, ['server' => $this->server])
->call('change', '8.1')
->assertSuccessful();
Bus::assertDispatched(SetDefaultCli::class);
}
public function test_install_extension(): void
{
Bus::fake();
$this->actingAs($this->user);
Livewire::test(InstalledVersions::class, ['server' => $this->server])
->set('extensionId', $this->server->php('8.2')?->id)
->set('extension', 'gmp')
->call('installExtension')
->assertSuccessful();
Bus::assertDispatched(InstallPHPExtension::class);
}
public function test_extension_already_installed(): void
{
Bus::fake();
$this->actingAs($this->user);
$this->server->php('8.2')->update([
'type_data' => [
'extensions' => [
'gmp',
],
],
]);
Livewire::test(InstalledVersions::class, ['server' => $this->server])
->set('extensionId', $this->server->php('8.2')?->id)
->set('extension', 'gmp')
->call('installExtension')
->assertSuccessful();
Bus::assertNotDispatched(InstallPHPExtension::class);
}
}

166
tests/Feature/PHPTest.php Normal file
View File

@ -0,0 +1,166 @@
<?php
namespace Tests\Feature;
use App\Enums\ServiceStatus;
use App\Facades\SSH;
use App\Models\Service;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PHPTest extends TestCase
{
use RefreshDatabase;
public function test_install_new_php(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.php.install', [
'server' => $this->server,
'version' => '8.1',
]))->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('services', [
'server_id' => $this->server->id,
'type' => 'php',
'version' => '8.1',
'status' => ServiceStatus::READY,
]);
}
public function test_uninstall_php(): void
{
SSH::fake();
$this->actingAs($this->user);
$php = new Service([
'server_id' => $this->server->id,
'type' => 'php',
'type_data' => [
'extensions' => [],
'settings' => config('core.php_settings'),
],
'name' => 'php',
'version' => '8.1',
'status' => ServiceStatus::READY,
'is_default' => true,
]);
$php->save();
$this->delete(route('servers.php.uninstall', [
'server' => $this->server,
'version' => '8.1',
]))->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('services', [
'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
{
SSH::fake();
$this->actingAs($this->user);
$php = Service::factory()->create([
'server_id' => $this->server->id,
'type' => 'php',
'type_data' => [
'extensions' => [],
],
'name' => 'php',
'version' => '8.1',
'status' => ServiceStatus::READY,
]);
$this->post(route('servers.php.default-cli', [
'server' => $this->server,
'version' => '8.1',
]))->assertSessionDoesntHaveErrors();
$php->refresh();
$this->assertTrue($php->is_default);
}
public function test_install_extension(): void
{
SSH::fake('output... [PHP Modules] gmp');
$this->actingAs($this->user);
$this->post(route('servers.php.install-extension', [
'server' => $this->server,
'version' => '8.2',
'extension' => 'gmp',
]))->assertSessionDoesntHaveErrors();
$php = $this->server->php('8.2');
$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();
}
public function test_get_php_ini(): void
{
SSH::fake('[PHP ini]');
$this->actingAs($this->user);
$this->get(route('servers.php.get-ini', [
'server' => $this->server,
'version' => '8.2',
]))->assertSessionHas('ini');
}
public function test_update_php_ini(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.php.update-ini', [
'server' => $this->server,
'version' => '8.2',
'ini' => 'new ini',
]))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'success')
->assertSessionHas('toast.message', 'PHP ini updated!');
}
}

View File

@ -2,10 +2,8 @@
namespace Tests\Feature;
use App\Http\Livewire\Profile\UpdatePassword;
use App\Http\Livewire\Profile\UpdateProfileInformation;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class ProfileTest extends TestCase
@ -18,20 +16,20 @@ public function test_profile_page_is_displayed(): void
$this
->get(route('profile'))
->assertSeeLivewire(UpdateProfileInformation::class)
->assertSeeLivewire(UpdatePassword::class);
->assertSee('Profile Information')
->assertSee('Update Password')
->assertSee('Two Factor Authentication');
}
public function test_profile_information_can_be_updated(): void
{
$this->actingAs($this->user);
Livewire::test(UpdateProfileInformation::class)
->set('name', 'Test')
->set('email', 'test@example.com')
->set('timezone', 'Europe/Berlin')
->call('submit')
->assertSuccessful();
$this->post(route('profile.info'), [
'name' => 'Test',
'email' => 'test@example.com',
'timezone' => 'Europe/Berlin',
]);
$this->user->refresh();
@ -39,4 +37,28 @@ public function test_profile_information_can_be_updated(): void
$this->assertSame('test@example.com', $this->user->email);
$this->assertSame('Europe/Berlin', $this->user->timezone);
}
public function test_password_can_be_updated(): void
{
$this->actingAs($this->user);
$this->post(route('profile.password'), [
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$this->assertTrue(Hash::check('new-password', $this->user->refresh()->password));
}
public function test_correct_password_must_be_provided_to_update_password(): void
{
$this->actingAs($this->user);
$this->post(route('profile.password'), [
'current_password' => 'wrong-password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])->assertSessionHasErrors('current_password');
}
}

View File

@ -2,12 +2,8 @@
namespace Tests\Feature;
use App\Http\Livewire\Projects\CreateProject;
use App\Http\Livewire\Projects\EditProject;
use App\Http\Livewire\Projects\ProjectsList;
use App\Models\Project;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class ProjectsTest extends TestCase
@ -18,10 +14,9 @@ public function test_create_project(): void
{
$this->actingAs($this->user);
Livewire::test(CreateProject::class)
->set('inputs.name', 'test')
->call('create')
->assertSuccessful();
$this->post(route('projects.create'), [
'name' => 'test',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('projects', [
'name' => 'test',
@ -36,10 +31,8 @@ public function test_see_projects_list(): void
'user_id' => $this->user->id,
]);
Livewire::test(ProjectsList::class)
->assertSee([
$project->name,
]);
$this->get(route('projects'))
->assertSee($project->name);
}
public function test_delete_project(): void
@ -50,10 +43,8 @@ public function test_delete_project(): void
'user_id' => $this->user->id,
]);
Livewire::test(ProjectsList::class)
->set('deleteId', $project->id)
->call('delete')
->assertSuccessful();
$this->delete(route('projects.delete', $project))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('projects', [
'id' => $project->id,
@ -68,16 +59,25 @@ public function test_edit_project(): void
'user_id' => $this->user->id,
]);
Livewire::test(EditProject::class, [
'project' => $project,
])
->set('inputs.name', 'test')
->call('save')
->assertSuccessful();
$this->post(route('projects.update', $project), [
'name' => 'new-name',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('projects', [
'id' => $project->id,
'name' => 'test',
'name' => 'new-name',
]);
}
public function test_cannot_delete_last_project(): void
{
$this->actingAs($this->user);
$this->delete(route('projects.delete', [
'project' => $this->user->currentProject,
]))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'Cannot delete the last project.');
}
}

View File

@ -3,11 +3,9 @@
namespace Tests\Feature;
use App\Enums\QueueStatus;
use App\Http\Livewire\Queues\CreateQueue;
use App\Http\Livewire\Queues\QueuesList;
use App\Facades\SSH;
use App\Models\Queue;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class QueuesTest extends TestCase
@ -23,12 +21,20 @@ public function test_see_queues()
'site_id' => $this->site->id,
]);
Livewire::test(QueuesList::class, ['site' => $this->site])
->assertSeeText($queue->command);
$this->get(
route('servers.sites.queues', [
'server' => $this->server,
'site' => $this->site,
])
)
->assertOk()
->assertSee($queue->command);
}
public function test_delete_queue()
{
SSH::fake();
$this->actingAs($this->user);
$queue = Queue::factory()->create([
@ -36,24 +42,38 @@ public function test_delete_queue()
'site_id' => $this->site->id,
]);
Livewire::test(QueuesList::class, ['site' => $this->site])
->set('deleteId', $queue->id)
->call('delete')
->assertDispatched('confirmed');
$this->delete(
route('servers.sites.queues.destroy', [
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertRedirect();
$this->assertDatabaseMissing('queues', [
'id' => $queue->id,
]);
}
public function test_create_queue()
{
SSH::fake();
$this->actingAs($this->user);
Livewire::test(CreateQueue::class, ['site' => $this->site])
->set('command', 'php artisan queue:work')
->set('user', 'vito')
->set('auto_start', 1)
->set('auto_restart', 1)
->set('numprocs', 1)
->call('create')
->assertDispatched('created');
$this->post(
route('servers.sites.queues.store', [
'server' => $this->server,
'site' => $this->site,
]),
[
'command' => 'php artisan queue:work',
'user' => 'vito',
'auto_start' => 1,
'auto_restart' => 1,
'numprocs' => 1,
]
)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('queues', [
'server_id' => $this->server->id,
@ -63,7 +83,111 @@ public function test_create_queue()
'auto_start' => 1,
'auto_restart' => 1,
'numprocs' => 1,
'status' => QueueStatus::CREATING,
'status' => QueueStatus::RUNNING,
]);
}
public function test_start_queue(): void
{
SSH::fake();
$this->actingAs($this->user);
$queue = Queue::factory()->create([
'server_id' => $this->server->id,
'site_id' => $this->site->id,
'status' => QueueStatus::STOPPED,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'start',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
'status' => QueueStatus::RUNNING,
]);
}
public function test_stop_queue(): void
{
SSH::fake();
$this->actingAs($this->user);
$queue = Queue::factory()->create([
'server_id' => $this->server->id,
'site_id' => $this->site->id,
'status' => QueueStatus::RUNNING,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'stop',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
'status' => QueueStatus::STOPPED,
]);
}
public function test_restart_queue(): void
{
SSH::fake();
$this->actingAs($this->user);
$queue = Queue::factory()->create([
'server_id' => $this->server->id,
'site_id' => $this->site->id,
'status' => QueueStatus::RUNNING,
]);
$this->post(
route('servers.sites.queues.action', [
'action' => 'restart',
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('queues', [
'id' => $queue->id,
'status' => QueueStatus::RUNNING,
]);
}
public function test_show_logs(): void
{
SSH::fake('logs');
$this->actingAs($this->user);
$queue = Queue::factory()->create([
'server_id' => $this->server->id,
'site_id' => $this->site->id,
'status' => QueueStatus::RUNNING,
]);
$this->get(
route('servers.sites.queues.logs', [
'server' => $this->server,
'site' => $this->site,
'queue' => $queue,
])
)
->assertSessionDoesntHaveErrors()
->assertSessionHas('content', 'logs');
}
}

View File

@ -0,0 +1,58 @@
<?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

@ -3,15 +3,9 @@
namespace Tests\Feature;
use App\Enums\SshKeyStatus;
use App\Http\Livewire\ServerSshKeys\AddExistingKey;
use App\Http\Livewire\ServerSshKeys\AddNewKey;
use App\Http\Livewire\ServerSshKeys\ServerKeysList;
use App\Jobs\SshKey\DeleteSshKeyFromServer;
use App\Jobs\SshKey\DeploySshKeyToServer;
use App\Facades\SSH;
use App\Models\SshKey;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class ServerKeysTest extends TestCase
@ -28,15 +22,17 @@ public function test_see_server_keys()
'public_key' => 'public-key-content',
]);
$this->server->sshKeys()->attach($sshKey);
$this->server->sshKeys()->attach($sshKey, [
'status' => SshKeyStatus::ADDED,
]);
Livewire::test(ServerKeysList::class, ['server' => $this->server])
$this->get(route('servers.ssh-keys', $this->server))
->assertSeeText('My first key');
}
public function test_delete_ssh_key()
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -46,46 +42,38 @@ public function test_delete_ssh_key()
'public_key' => 'public-key-content',
]);
$this->server->sshKeys()->attach($sshKey);
Livewire::test(ServerKeysList::class, ['server' => $this->server])
->set('deleteId', $sshKey->id)
->call('delete')
->assertDispatched('confirmed');
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
'ssh_key_id' => $sshKey->id,
'status' => SshKeyStatus::DELETING,
$this->server->sshKeys()->attach($sshKey, [
'status' => SshKeyStatus::ADDED,
]);
Bus::assertDispatched(DeleteSshKeyFromServer::class);
$this->delete(route('servers.ssh-keys.destroy', [$this->server, $sshKey]));
$this->assertDatabaseMissing('server_ssh_keys', [
'server_id' => $this->server->id,
'ssh_key_id' => $sshKey->id,
]);
}
public function test_add_new_ssh_key()
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
Livewire::test(AddNewKey::class, ['server' => $this->server])
->set('name', 'My first key')
->set('public_key', 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3CCnyBbpCgOJ0AWUSfBZ+mYAsYzcQDegPkBx1kyE0bXT1yX4+6uYx1Jh6NxWgLyaU0BaP4nsClrK1u5FojQHd8J7ycc0N3H8B+v2NPzj1Q6bFnl40saastONVm+d4edbCg9BowGAafLcf9ALsognqqOWQbK/QOpAhg25IAe47eiY3IjDGMHlsvaZkMtkDhT4t1mK8ZLjxw5vjyVYgINJefR981bIxMFrXy+0xBCsYOZxMIoAJsgCkrAGlI4kQHKv0SQVccSyTE1eziIZa5b3QUlXj8ogxMfK/EOD7Aoqinw652k4S5CwFs/LLmjWcFqCKDM6CSggWpB78DZ729O6zFvQS9V99/9SsSV7Qc5ML7B0DKzJ/tbHkaAE8xdZnQnZFVUegUMtUmjvngMaGlYsxkAZrUKsFRoh7xfXVkDyRBaBSslRNe8LFsXw9f7Q+3jdZ5vhGhmp+TBXTlgxApwR023411+ABE9y0doCx8illya3m2olEiiMZkRclgqsWFSk=')
->call('add')
->assertSuccessful()
->assertDispatched('added');
$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=',
]);
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
'status' => SshKeyStatus::ADDING,
'status' => SshKeyStatus::ADDED,
]);
Bus::assertDispatched(DeploySshKeyToServer::class);
}
public function test_add_existing_key()
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -95,17 +83,13 @@ public function test_add_existing_key()
'public_key' => 'public-key-content',
]);
Livewire::test(AddExistingKey::class, ['server' => $this->server])
->set('key_id', $sshKey->id)
->call('add')
->assertSuccessful()
->assertDispatched('added');
$this->post(route('servers.ssh-keys.deploy', $this->server), [
'key_id' => $sshKey->id,
]);
$this->assertDatabaseHas('server_ssh_keys', [
'server_id' => $this->server->id,
'status' => SshKeyStatus::ADDING,
'status' => SshKeyStatus::ADDED,
]);
Bus::assertDispatched(DeploySshKeyToServer::class);
}
}

View File

@ -3,32 +3,60 @@
namespace Tests\Feature;
use App\Enums\ServerProvider;
use App\Http\Livewire\ServerProviders\ConnectProvider;
use App\Http\Livewire\ServerProviders\ProvidersList;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class ServerProvidersTest extends TestCase
{
use RefreshDatabase;
public function test_connect_hetzner(): void
/**
* @dataProvider data
*/
public function test_connect_provider(string $provider, array $input): void
{
$this->actingAs($this->user);
Http::fake();
Livewire::test(ConnectProvider::class)
->set('provider', ServerProvider::HETZNER)
->set('name', 'profile')
->set('token', 'token')
->call('connect')
->assertSuccessful();
$data = array_merge(
[
'provider' => $provider,
'name' => 'profile',
],
$input
);
$this->post(route('server-providers.connect'), $data)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('server_providers', [
'provider' => ServerProvider::HETZNER,
'provider' => $provider,
'profile' => 'profile',
]);
}
/**
* @dataProvider data
*/
public function test_cannot_connect_to_provider(string $provider, array $input): void
{
$this->actingAs($this->user);
Http::fake([
'*' => Http::response([], 401),
]);
$data = array_merge(
[
'provider' => $provider,
'name' => 'profile',
],
$input
);
$this->post(route('server-providers.connect'), $data)->assertSessionHasErrors();
$this->assertDatabaseMissing('server_providers', [
'provider' => $provider,
'profile' => 'profile',
]);
}
@ -41,27 +69,90 @@ public function test_see_providers_list(): void
'user_id' => $this->user->id,
]);
Livewire::test(ProvidersList::class)
->assertSee([
$provider->profile,
]);
$this->get(route('server-providers'))
->assertSee($provider->profile);
}
public function test_delete_provider(): void
/**
* @dataProvider data
*/
public function test_delete_provider(string $provider): void
{
$this->actingAs($this->user);
$provider = \App\Models\ServerProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => $provider,
]);
Livewire::test(ProvidersList::class)
->set('deleteId', $provider->id)
->call('delete')
->assertSuccessful();
$this->delete(route('server-providers.delete', $provider))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('server_providers', [
'id' => $provider->id,
]);
}
/**
* @dataProvider data
*/
public function test_cannot_delete_provider(string $provider): void
{
$this->actingAs($this->user);
$provider = \App\Models\ServerProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => $provider,
]);
$this->server->update([
'provider_id' => $provider->id,
]);
$this->delete(route('server-providers.delete', $provider))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This server provider is being used by a server.');
$this->assertDatabaseHas('server_providers', [
'id' => $provider->id,
]);
}
public static function data(): array
{
return [
// [
// ServerProvider::AWS,
// [
// 'key' => 'key',
// 'secret' => 'secret',
// ],
// ],
[
ServerProvider::LINODE,
[
'token' => 'token',
],
],
[
ServerProvider::DIGITALOCEAN,
[
'token' => 'token',
],
],
[
ServerProvider::VULTR,
[
'token' => 'token',
],
],
[
ServerProvider::HETZNER,
[
'token' => 'token',
],
],
];
}
}

View File

@ -6,42 +6,278 @@
use App\Enums\OperatingSystem;
use App\Enums\ServerProvider;
use App\Enums\ServerStatus;
use App\Enums\ServerType;
use App\Enums\ServiceStatus;
use App\Enums\Webserver;
use App\Http\Livewire\Servers\CreateServer;
use App\Jobs\Installation\Initialize;
use App\Facades\SSH;
use App\NotificationChannels\Email\NotificationMail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class ServerTest extends TestCase
{
use RefreshDatabase;
public function test_create_custom_server(): void
public function test_create_regular_server(): void
{
$this->actingAs($this->user);
Bus::fake();
SSH::fake('Active: active'); // fake output for service installations
Livewire::test(CreateServer::class)
->set('provider', ServerProvider::CUSTOM)
->set('name', 'test')
->set('ip', '1.1.1.1')
->set('port', '22')
->set('os', OperatingSystem::UBUNTU22)
->set('webserver', Webserver::NGINX)
->set('database', Database::MYSQL80)
->set('php', '8.2')
->call('submit')
->assertSuccessful();
$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();
$this->assertDatabaseHas('servers', [
'name' => 'test',
'ip' => '1.1.1.1',
'status' => ServerStatus::INSTALLING,
'status' => ServerStatus::READY,
]);
Bus::assertDispatched(Initialize::class);
$this->assertDatabaseHas('services', [
'server_id' => 1,
'type' => 'php',
'version' => '8.2',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseHas('services', [
'server_id' => 1,
'type' => 'webserver',
'name' => 'nginx',
'version' => 'latest',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseHas('services', [
'server_id' => 1,
'type' => 'database',
'name' => 'mysql',
'version' => '8.0',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseHas('services', [
'server_id' => 1,
'type' => 'firewall',
'name' => 'ufw',
'version' => 'latest',
'status' => ServiceStatus::READY,
]);
}
public function test_create_database_server(): void
{
$this->actingAs($this->user);
SSH::fake('Active: active'); // fake output for service installations
$this->post(route('servers.create'), [
'type' => ServerType::DATABASE,
'provider' => ServerProvider::CUSTOM,
'name' => 'test',
'ip' => '2.2.2.2',
'port' => '22',
'os' => OperatingSystem::UBUNTU22,
'database' => Database::MYSQL80,
])->assertSessionDoesntHaveErrors();
$server = \App\Models\Server::query()->where('ip', '2.2.2.2')->first();
$this->assertDatabaseHas('servers', [
'name' => 'test',
'ip' => '2.2.2.2',
'status' => ServerStatus::READY,
]);
$this->assertDatabaseMissing('services', [
'server_id' => $server->id,
'type' => 'php',
'version' => '8.2',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseMissing('services', [
'server_id' => $server->id,
'type' => 'webserver',
'name' => 'nginx',
'version' => 'latest',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseHas('services', [
'server_id' => $server->id,
'type' => 'database',
'name' => 'mysql',
'version' => '8.0',
'status' => ServiceStatus::READY,
]);
$this->assertDatabaseHas('services', [
'server_id' => $server->id,
'type' => 'firewall',
'name' => 'ufw',
'version' => 'latest',
'status' => ServiceStatus::READY,
]);
}
public function test_delete_server(): void
{
$this->actingAs($this->user);
SSH::fake();
$this->delete(route('servers.delete', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
]);
}
public function test_cannot_delete_on_provider(): void
{
Mail::fake();
Http::fake([
'*' => Http::response([], 401),
]);
$this->actingAs($this->user);
$provider = \App\Models\ServerProvider::factory()->create([
'user_id' => $this->user->id,
'provider' => ServerProvider::HETZNER,
'credentials' => [
'token' => 'token',
],
]);
$this->server->update([
'provider' => ServerProvider::HETZNER,
'provider_id' => $provider->id,
'provider_data' => [
'hetzner_id' => 1,
'ssh_key_id' => 1,
],
]);
$this->delete(route('servers.delete', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('servers', [
'id' => $this->server->id,
]);
Mail::assertSent(NotificationMail::class);
}
public function test_check_connection_is_ready(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->server->update(['status' => ServerStatus::DISCONNECTED]);
$this->post(route('servers.settings.check-connection', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'status' => ServerStatus::READY,
]);
}
public function test_connection_failed(): void
{
SSH::fake()->connectionWillFail();
$this->actingAs($this->user);
$this->server->update(['status' => ServerStatus::READY]);
$this->post(route('servers.settings.check-connection', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'status' => ServerStatus::DISCONNECTED,
]);
}
public function test_reboot_server(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.settings.reboot', $this->server))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'status' => ServerStatus::DISCONNECTED,
]);
}
public function test_edit_server(): void
{
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'name' => 'new-name',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'name' => 'new-name',
]);
}
public function test_edit_server_ip_address(): void
{
SSH::fake();
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'ip' => '2.2.2.2',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'ip' => '2.2.2.2',
'status' => ServerStatus::READY,
]);
}
public function test_edit_server_ip_address_and_disconnect(): void
{
SSH::fake()->connectionWillFail();
$this->actingAs($this->user);
$this->post(route('servers.settings.edit', $this->server), [
'ip' => '2.2.2.2',
'port' => 2222,
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('servers', [
'id' => $this->server->id,
'ip' => '2.2.2.2',
'port' => 2222,
'status' => ServerStatus::DISCONNECTED,
]);
}
}

View File

@ -3,15 +3,8 @@
namespace Tests\Feature;
use App\Enums\ServiceStatus;
use App\Http\Livewire\Services\InstallPHPMyAdmin;
use App\Http\Livewire\Services\ServicesList;
use App\Jobs\Installation\InstallPHPMyAdmin as InstallationInstallPHPMyAdmin;
use App\Jobs\Installation\UninstallPHPMyAdmin;
use App\Jobs\Service\Manage;
use App\Models\Service;
use App\Facades\SSH;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class ServicesTest extends TestCase
@ -22,15 +15,12 @@ public function test_see_services_list(): void
{
$this->actingAs($this->user);
Livewire::test(ServicesList::class, ['server' => $this->server])
->assertSee([
'nginx',
'php',
'supervisor',
'redis',
'ufw',
'php',
]);
$this->get(route('servers.services', $this->server))
->assertSee('nginx')
->assertSee('php')
->assertSee('supervisor')
->assertSee('redis')
->assertSee('ufw');
}
/**
@ -38,15 +28,41 @@ public function test_see_services_list(): void
*/
public function test_restart_service(string $name): void
{
$service = $this->server->services()->where('name', $name)->first();
$this->actingAs($this->user);
Bus::fake();
$service = $this->server->services()->where('name', $name)->firstOrFail();
Livewire::test(ServicesList::class, ['server' => $this->server])
->call('restart', $service->id)
->assertSuccessful();
SSH::fake('Active: active');
Bus::assertDispatched(Manage::class);
$this->get(route('servers.services.restart', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::READY, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_restart_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: inactive');
$this->get(route('servers.services.restart', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
/**
@ -54,15 +70,41 @@ public function test_restart_service(string $name): void
*/
public function test_stop_service(string $name): void
{
$service = $this->server->services()->where('name', $name)->first();
$this->actingAs($this->user);
Bus::fake();
$service = $this->server->services()->where('name', $name)->firstOrFail();
Livewire::test(ServicesList::class, ['server' => $this->server])
->call('stop', $service->id)
->assertSuccessful();
SSH::fake('Active: inactive');
Bus::assertDispatched(Manage::class);
$this->get(route('servers.services.stop', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::STOPPED, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_stop_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: active');
$this->get(route('servers.services.stop', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
/**
@ -70,57 +112,125 @@ public function test_stop_service(string $name): void
*/
public function test_start_service(string $name): void
{
$service = $this->server->services()->where('name', $name)->first();
$this->actingAs($this->user);
$service->status = ServiceStatus::STOPPED;
$service->save();
$service = $this->server->services()->where('name', $name)->firstOrFail();
Bus::fake();
SSH::fake('Active: active');
Livewire::test(ServicesList::class, ['server' => $this->server])
->call('start', $service->id)
->assertSuccessful();
$this->get(route('servers.services.start', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
Bus::assertDispatched(Manage::class);
$service->refresh();
$this->assertEquals(ServiceStatus::READY, $service->status);
}
public function test_install_phpmyadmin(): void
/**
* @dataProvider data
*/
public function test_failed_to_start_service(string $name): void
{
Bus::fake();
$this->actingAs($this->user);
Livewire::test(InstallPHPMyAdmin::class, ['server' => $this->server])
->set('allowed_ip', '0.0.0.0')
->set('port', 5433)
->call('install')
->assertSuccessful();
$service = $this->server->services()->where('name', $name)->firstOrFail();
Bus::assertDispatched(InstallationInstallPHPMyAdmin::class);
SSH::fake('Active: inactive');
$this->get(route('servers.services.start', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
public function test_uninstall_phpmyadmin(): void
/**
* @dataProvider data
*/
public function test_enable_service(string $name): void
{
$service = Service::factory()->create([
'server_id' => $this->server->id,
'type' => 'phpmyadmin',
'type_data' => [
'allowed_ip' => '0.0.0.0',
'port' => '5433',
'php' => '8.1',
],
'name' => 'phpmyadmin',
'version' => '5.1.2',
'status' => ServiceStatus::READY,
'is_default' => 1,
$this->actingAs($this->user);
]);
$service = $this->server->services()->where('name', $name)->firstOrFail();
Bus::fake();
SSH::fake('Active: active');
Livewire::test(ServicesList::class, ['server' => $this->server])
->call('uninstall', $service->id)
->assertSuccessful();
$this->get(route('servers.services.enable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
Bus::assertDispatched(UninstallPHPMyAdmin::class);
$service->refresh();
$this->assertEquals(ServiceStatus::READY, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_enable_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: inactive');
$this->get(route('servers.services.enable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
/**
* @dataProvider data
*/
public function test_disable_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: inactive');
$this->get(route('servers.services.disable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::DISABLED, $service->status);
}
/**
* @dataProvider data
*/
public function test_failed_to_disable_service(string $name): void
{
$this->actingAs($this->user);
$service = $this->server->services()->where('name', $name)->firstOrFail();
SSH::fake('Active: active');
$this->get(route('servers.services.disable', [
'server' => $this->server,
'service' => $service,
]))->assertSessionDoesntHaveErrors();
$service->refresh();
$this->assertEquals(ServiceStatus::FAILED, $service->status);
}
public static function data(): array

View File

@ -6,18 +6,9 @@
use App\Enums\SiteType;
use App\Enums\SourceControl;
use App\Facades\SSH;
use App\Http\Livewire\Sites\ChangePhpVersion;
use App\Http\Livewire\Sites\CreateSite;
use App\Http\Livewire\Sites\DeleteSite;
use App\Http\Livewire\Sites\SitesList;
use App\Http\Livewire\Sites\UpdateSourceControlProvider;
use App\Http\Livewire\Sites\UpdateVHost;
use App\Jobs\Site\CreateVHost;
use App\Models\Site;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class SitesTest extends TestCase
@ -29,9 +20,12 @@ class SitesTest extends TestCase
*/
public function test_create_site(array $inputs): void
{
Bus::fake();
SSH::fake();
Http::fake();
Http::fake([
'https://api.github.com/repos/*' => Http::response([
], 201),
]);
$this->actingAs($this->user);
@ -40,18 +34,15 @@ public function test_create_site(array $inputs): void
'provider' => SourceControl::GITHUB,
]);
Livewire::test(CreateSite::class, ['server' => $this->server])
->fill($inputs)
->set('inputs.source_control', $sourceControl->id)
->call('create')
->assertSuccessful()
->assertHasNoErrors();
$inputs['source_control'] = $sourceControl->id;
Bus::assertDispatched(CreateVHost::class);
$this->post(route('servers.sites.create', [
'server' => $this->server,
]), $inputs)->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('sites', [
'domain' => 'example.com',
'status' => SiteStatus::INSTALLING,
'status' => SiteStatus::READY,
]);
}
@ -63,15 +54,16 @@ public function test_see_sites_list(): void
'server_id' => $this->server->id,
]);
Livewire::test(SitesList::class, ['server' => $this->server])
->assertSee([
$site->domain,
]);
$this->get(route('servers.sites', [
'server' => $this->server,
]))
->assertOk()
->assertSee($site->domain);
}
public function test_delete_site(): void
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -79,21 +71,19 @@ public function test_delete_site(): void
'server_id' => $this->server->id,
]);
Livewire::test(DeleteSite::class, ['server' => $this->server])
->set('site', $site)
->call('delete')
->assertSuccessful();
$this->delete(route('servers.sites.destroy', [
'server' => $this->server,
'site' => $site,
]))->assertRedirect();
Bus::assertDispatched(\App\Jobs\Site\DeleteSite::class);
$site->refresh();
$this->assertEquals(SiteStatus::DELETING, $site->status);
$this->assertDatabaseMissing('sites', [
'id' => $site->id,
]);
}
public function test_change_php_version(): void
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -101,12 +91,16 @@ public function test_change_php_version(): void
'server_id' => $this->server->id,
]);
Livewire::test(ChangePhpVersion::class, ['site' => $site])
->set('version', '8.1')
->call('change')
->assertSuccessful();
$this->post(route('servers.sites.settings.php', [
'server' => $this->server,
'site' => $site,
]), [
'version' => '8.2',
])->assertSessionDoesntHaveErrors();
Bus::assertDispatched(\App\Jobs\Site\ChangePHPVersion::class);
$site->refresh();
$this->assertEquals('8.2', $site->php_version);
}
public function test_update_v_host(): void
@ -119,27 +113,10 @@ public function test_update_v_host(): void
'server_id' => $this->server->id,
]);
Livewire::test(UpdateVHost::class, ['site' => $site])
->set('vHost', 'test-vhost')
->call('update')
->assertSuccessful();
}
public function test_update_source_control(): void
{
$this->actingAs($this->user);
/** @var \App\Models\SourceControl $gitlab */
$gitlab = \App\Models\SourceControl::factory()->gitlab()->create();
Livewire::test(UpdateSourceControlProvider::class, ['site' => $this->site])
->set('source_control', $gitlab->id)
->call('update')
->assertSuccessful();
$this->site->refresh();
$this->assertEquals($gitlab->id, $this->site->source_control_id);
$this->get(route('servers.sites.settings.vhost', [
'server' => $this->server,
'site' => $site,
]))->assertSessionDoesntHaveErrors();
}
public static function create_data(): array
@ -147,40 +124,52 @@ public static function create_data(): array
return [
[
[
'inputs.type' => SiteType::LARAVEL,
'inputs.domain' => 'example.com',
'inputs.alias' => 'www.example.com',
'inputs.php_version' => '8.2',
'inputs.web_directory' => 'public',
'inputs.repository' => 'test/test',
'inputs.branch' => 'main',
'inputs.composer' => true,
'type' => SiteType::LARAVEL,
'domain' => 'example.com',
'alias' => 'www.example.com',
'php_version' => '8.2',
'web_directory' => 'public',
'repository' => 'test/test',
'branch' => 'main',
'composer' => true,
],
],
[
[
'inputs.type' => SiteType::WORDPRESS,
'inputs.domain' => 'example.com',
'inputs.alias' => 'www.example.com',
'inputs.php_version' => '8.2',
'inputs.title' => 'Example',
'inputs.username' => 'example',
'inputs.email' => 'email@example.com',
'inputs.password' => 'password',
'inputs.database' => 'example',
'inputs.database_user' => 'example',
'inputs.database_password' => 'password',
'type' => SiteType::WORDPRESS,
'domain' => 'example.com',
'alias' => 'www.example.com',
'php_version' => '8.2',
'title' => 'Example',
'username' => 'example',
'email' => 'email@example.com',
'password' => 'password',
'database' => 'example',
'database_user' => 'example',
'database_password' => 'password',
],
],
[
[
'inputs.type' => SiteType::PHP_BLANK,
'inputs.domain' => 'example.com',
'inputs.alias' => 'www.example.com',
'inputs.php_version' => '8.2',
'inputs.web_directory' => 'public',
'type' => SiteType::PHP_BLANK,
'domain' => 'example.com',
'alias' => 'www.example.com',
'php_version' => '8.2',
'web_directory' => 'public',
],
],
];
}
public function test_see_logs(): void
{
$this->actingAs($this->user);
$this->get(route('servers.sites.logs', [
'server' => $this->server,
'site' => $this->site,
]))
->assertOk()
->assertSee('Logs');
}
}

View File

@ -2,12 +2,9 @@
namespace Tests\Feature;
use App\Http\Livewire\SourceControls\Connect;
use App\Http\Livewire\SourceControls\SourceControlsList;
use App\Models\SourceControl;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class SourceControlsTest extends TestCase
@ -17,24 +14,22 @@ class SourceControlsTest extends TestCase
/**
* @dataProvider data
*/
public function test_connect_provider(string $provider, ?string $customUrl): void
public function test_connect_provider(string $provider, ?string $customUrl, array $input): void
{
$this->actingAs($this->user);
Http::fake();
$livewire = Livewire::test(Connect::class)
->set('token', 'token')
->set('name', 'profile')
->set('provider', $provider);
$input = array_merge([
'name' => 'test',
'provider' => $provider,
], $input);
if ($customUrl !== null) {
$livewire->set('url', $customUrl);
$input['url'] = $customUrl;
}
$livewire
->call('connect')
->assertSuccessful();
$this->post(route('source-controls.connect'), $input)
->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('source_controls', [
'provider' => $provider,
@ -55,23 +50,48 @@ public function test_delete_provider(string $provider): void
'profile' => 'test',
]);
Livewire::test(SourceControlsList::class)
->set('deleteId', $sourceControl->id)
->call('delete')
->assertSuccessful();
$this->delete(route('source-controls.delete', $sourceControl->id))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('source_controls', [
'id' => $sourceControl->id,
]);
}
/**
* @dataProvider data
*/
public function test_cannot_delete_provider(string $provider): void
{
$this->actingAs($this->user);
/** @var SourceControl $sourceControl */
$sourceControl = SourceControl::factory()->create([
'provider' => $provider,
'profile' => 'test',
]);
$this->site->update([
'source_control_id' => $sourceControl->id,
]);
$this->delete(route('source-controls.delete', $sourceControl->id))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This source control is being used by a site.');
$this->assertDatabaseHas('source_controls', [
'id' => $sourceControl->id,
]);
}
public static function data(): array
{
return [
['github', null],
['gitlab', null],
['gitlab', 'https://git.example.com/'],
['bitbucket', null],
['github', null, ['token' => 'test']],
['gitlab', null, ['token' => 'test']],
['gitlab', 'https://git.example.com/', ['token' => 'test']],
['bitbucket', null, ['username' => 'test', 'password' => 'test']],
];
}
}

View File

@ -2,11 +2,8 @@
namespace Tests\Feature;
use App\Http\Livewire\SshKeys\AddKey;
use App\Http\Livewire\SshKeys\KeysList;
use App\Models\SshKey;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class SshKeysTest extends TestCase
@ -17,14 +14,10 @@ public function test_create_ssh_key(): void
{
$this->actingAs($this->user);
Livewire::test(AddKey::class)
->set('name', 'test')
->set(
'public_key',
'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== test@test.local'
)
->call('add')
->assertSuccessful();
$this->post(route('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();
}
public function test_get_public_keys_list(): void
@ -35,10 +28,8 @@ public function test_get_public_keys_list(): void
'user_id' => $this->user->id,
]);
Livewire::test(KeysList::class)
->assertSee([
$key->name,
]);
$this->get(route('ssh-keys'))
->assertSee($key->name);
}
public function test_delete_key(): void
@ -49,10 +40,8 @@ public function test_delete_key(): void
'user_id' => $this->user->id,
]);
Livewire::test(KeysList::class)
->set('deleteId', $key->id)
->call('delete')
->assertSuccessful();
$this->delete(route('ssh-keys.delete', $key->id))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('ssh_keys', [
'id' => $key->id,

View File

@ -4,14 +4,9 @@
use App\Enums\SslStatus;
use App\Enums\SslType;
use App\Http\Livewire\Ssl\CreateSsl;
use App\Http\Livewire\Ssl\SslsList;
use App\Jobs\Ssl\Deploy;
use App\Jobs\Ssl\Remove;
use App\Facades\SSH;
use App\Models\Ssl;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;
class SslTest extends TestCase
@ -26,41 +21,72 @@ public function test_see_ssls_list()
'site_id' => $this->site->id,
]);
Livewire::test(SslsList::class, ['site' => $this->site])
->assertSeeText($ssl->type);
$this->get(route('servers.sites.ssl', [
'server' => $this->server,
'site' => $this->site,
]))
->assertOk()
->assertSee($ssl->type);
}
public function test_see_ssls_list_with_no_ssls()
{
$this->actingAs($this->user);
Livewire::test(SslsList::class, ['site' => $this->site])
$this->get(route('servers.sites.ssl', [
'server' => $this->server,
'site' => $this->site,
]))
->assertOk()
->assertSeeText(__("You don't have any SSL certificates yet!"));
}
public function test_create_ssl()
public function test_letsencrypt_ssl()
{
Bus::fake();
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
Livewire::test(CreateSsl::class, ['site' => $this->site])
->set('type', SslType::LETSENCRYPT)
->call('create')
->assertDispatched('created');
$this->post(route('servers.sites.ssl.store', [
'server' => $this->server,
'site' => $this->site,
]), [
'type' => SslType::LETSENCRYPT,
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
'type' => SslType::LETSENCRYPT,
'status' => SslStatus::CREATING,
'status' => SslStatus::CREATED,
]);
}
Bus::assertDispatched(Deploy::class);
public function test_custom_ssl()
{
SSH::fake('Successfully received certificate');
$this->actingAs($this->user);
$this->post(route('servers.sites.ssl.store', [
'server' => $this->server,
'site' => $this->site,
]), [
'type' => SslType::CUSTOM,
'certificate' => 'certificate',
'private' => 'private',
'expires_at' => now()->addYear()->format('Y-m-d'),
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('ssls', [
'site_id' => $this->site->id,
'type' => SslType::CUSTOM,
'status' => SslStatus::CREATED,
]);
}
public function test_delete_ssl()
{
Bus::fake();
SSH::fake();
$this->actingAs($this->user);
@ -68,16 +94,14 @@ public function test_delete_ssl()
'site_id' => $this->site->id,
]);
Livewire::test(SslsList::class, ['site' => $this->site])
->set('deleteId', $ssl->id)
->call('delete')
->assertDispatched('confirmed');
$this->delete(route('servers.sites.ssl.destroy', [
'server' => $this->server,
'site' => $this->site,
'ssl' => $ssl,
]))->assertRedirect();
$this->assertDatabaseHas('ssls', [
$this->assertDatabaseMissing('ssls', [
'id' => $ssl->id,
'status' => SslStatus::DELETING,
]);
Bus::assertDispatched(Remove::class);
}
}

View File

@ -3,11 +3,10 @@
namespace Tests\Feature;
use App\Enums\StorageProvider;
use App\Http\Livewire\StorageProviders\ConnectProvider;
use App\Http\Livewire\StorageProviders\ProvidersList;
use App\Models\Backup;
use App\Models\Database;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;
use Tests\TestCase;
class StorageProvidersTest extends TestCase
@ -20,12 +19,11 @@ public function test_connect_dropbox(): void
Http::fake();
Livewire::test(ConnectProvider::class)
->set('provider', StorageProvider::DROPBOX)
->set('name', 'profile')
->set('token', 'token')
->call('connect')
->assertSuccessful();
$this->post(route('storage-providers.connect'), [
'provider' => StorageProvider::DROPBOX,
'name' => 'profile',
'token' => 'token',
])->assertSessionDoesntHaveErrors();
$this->assertDatabaseHas('storage_providers', [
'provider' => StorageProvider::DROPBOX,
@ -42,10 +40,8 @@ public function test_see_providers_list(): void
'provider' => StorageProvider::DROPBOX,
]);
Livewire::test(ProvidersList::class)
->assertSee([
$provider->profile,
]);
$this->get(route('storage-providers'))
->assertSee($provider->profile);
}
public function test_delete_provider(): void
@ -56,13 +52,39 @@ public function test_delete_provider(): void
'user_id' => $this->user->id,
]);
Livewire::test(ProvidersList::class)
->set('deleteId', $provider->id)
->call('delete')
->assertSuccessful();
$this->delete(route('storage-providers.delete', $provider->id))
->assertSessionDoesntHaveErrors();
$this->assertDatabaseMissing('storage_providers', [
'id' => $provider->id,
]);
}
public function test_cannot_delete_provider(): void
{
$this->actingAs($this->user);
$database = Database::factory()->create([
'server_id' => $this->server,
]);
$provider = \App\Models\StorageProvider::factory()->create([
'user_id' => $this->user->id,
]);
Backup::factory()->create([
'server_id' => $this->server->id,
'database_id' => $database->id,
'storage_id' => $provider->id,
]);
$this->delete(route('storage-providers.delete', $provider->id))
->assertSessionDoesntHaveErrors()
->assertSessionHas('toast.type', 'error')
->assertSessionHas('toast.message', 'This storage provider is being used by a backup.');
$this->assertDatabaseHas('storage_providers', [
'id' => $provider->id,
]);
}
}