mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-20 02:11:36 +00:00
* WIP to isolate users * Resolved issue with SSH AsUser Updated Isolated User Script to use Server User for Team Access Updated Path creation script to simplify for running as the isolated user * Included the server user * PHPMyAdmin script updated Wordpress Script Updated Updated Execute Script to support executing as isolated users * Issue Resolution & Resolved Failing Unit Tests * Fix for isolated_username vs user * Run the deploy as the isolated user * queue updates for isolated user * Support isolated users in cronjobs * script tests for isolated users * Queue tests for isolated users * Cronjob tests for isolated user * Removed default queue command for laravel apps * add default user to factory * laravel pint fixes * ensure echos are consistent * removed unneeded parameter * update * fix queues for isolated users * revert addslashes --------- Co-authored-by: Saeed Vaziry <mr.saeedvaziry@gmail.com>
239 lines
6.3 KiB
PHP
239 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\ScriptExecutionStatus;
|
|
use App\Facades\SSH;
|
|
use App\Models\Script;
|
|
use App\Models\ScriptExecution;
|
|
use App\Models\Server;
|
|
use App\Models\Site;
|
|
use App\Web\Pages\Scripts\Executions;
|
|
use App\Web\Pages\Scripts\Index;
|
|
use App\Web\Pages\Scripts\Widgets\ScriptExecutionsList;
|
|
use App\Web\Pages\Scripts\Widgets\ScriptsList;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
class ScriptTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_see_scripts(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
$this->get(Index::getUrl())
|
|
->assertSuccessful()
|
|
->assertSee($script->name);
|
|
}
|
|
|
|
public function test_create_script(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
Livewire::test(Index::class)
|
|
->callAction('create', [
|
|
'name' => 'Test Script',
|
|
'content' => 'echo "Hello, World!"',
|
|
])
|
|
->assertSuccessful();
|
|
|
|
$this->assertDatabaseHas('scripts', [
|
|
'name' => 'Test Script',
|
|
'content' => 'echo "Hello, World!"',
|
|
]);
|
|
}
|
|
|
|
public function test_edit_script(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
Livewire::test(ScriptsList::class)
|
|
->callTableAction('edit', $script->id, [
|
|
'name' => 'New Name',
|
|
'content' => 'echo "Hello, new World!"',
|
|
])
|
|
->assertSuccessful();
|
|
|
|
$this->assertDatabaseHas('scripts', [
|
|
'id' => $script->id,
|
|
'name' => 'New Name',
|
|
'content' => 'echo "Hello, new World!"',
|
|
]);
|
|
}
|
|
|
|
public function test_delete_script(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
$scriptExecution = ScriptExecution::factory()->create([
|
|
'script_id' => $script->id,
|
|
'status' => ScriptExecutionStatus::EXECUTING,
|
|
]);
|
|
|
|
Livewire::test(ScriptsList::class)
|
|
->callTableAction('delete', $script->id)
|
|
->assertSuccessful();
|
|
|
|
$this->assertDatabaseMissing('scripts', [
|
|
'id' => $script->id,
|
|
]);
|
|
|
|
$this->assertDatabaseMissing('script_executions', [
|
|
'id' => $scriptExecution->id,
|
|
]);
|
|
}
|
|
|
|
public function test_execute_script_and_view_log(): void
|
|
{
|
|
SSH::fake('script output');
|
|
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
Livewire::test(Executions::class, [
|
|
'script' => $script,
|
|
])
|
|
->callAction('execute', [
|
|
'server' => $this->server->id,
|
|
'user' => 'root',
|
|
])
|
|
->assertSuccessful();
|
|
|
|
$this->assertDatabaseHas('script_executions', [
|
|
'script_id' => $script->id,
|
|
'status' => ScriptExecutionStatus::COMPLETED,
|
|
'user' => 'root',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('server_logs', [
|
|
'server_id' => $this->server->id,
|
|
]);
|
|
|
|
$execution = $script->lastExecution;
|
|
|
|
Livewire::test(ScriptExecutionsList::class, [
|
|
'script' => $script,
|
|
])
|
|
->callTableAction('logs', $execution->id)
|
|
->assertSuccessful();
|
|
}
|
|
|
|
public function test_execute_script_as_isolated_user(): void
|
|
{
|
|
SSH::fake('script output');
|
|
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
Site::factory()->create([
|
|
'server_id' => $this->server->id,
|
|
'user' => 'example',
|
|
]);
|
|
|
|
Livewire::test(Executions::class, [
|
|
'script' => $script,
|
|
])
|
|
->callAction('execute', [
|
|
'server' => $this->server->id,
|
|
'user' => 'example',
|
|
])
|
|
->assertSuccessful();
|
|
|
|
$this->assertDatabaseHas('script_executions', [
|
|
'script_id' => $script->id,
|
|
'status' => ScriptExecutionStatus::COMPLETED,
|
|
'user' => 'example',
|
|
]);
|
|
}
|
|
|
|
public function test_cannot_execute_script_as_non_existing_user(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
Livewire::test(Executions::class, [
|
|
'script' => $script,
|
|
])
|
|
->callAction('execute', [
|
|
'server' => $this->server->id,
|
|
'user' => 'example',
|
|
])
|
|
->assertHasActionErrors();
|
|
|
|
$this->assertDatabaseMissing('script_executions', [
|
|
'script_id' => $script->id,
|
|
'user' => 'example',
|
|
]);
|
|
}
|
|
|
|
public function test_cannot_execute_script_as_user_not_on_server(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
Site::factory()->create([
|
|
'server_id' => Server::factory()->create(['user_id' => 1])->id,
|
|
'user' => 'example',
|
|
]);
|
|
|
|
Livewire::test(Executions::class, [
|
|
'script' => $script,
|
|
])
|
|
->callAction('execute', [
|
|
'server' => $this->server->id,
|
|
'user' => 'example',
|
|
])
|
|
->assertHasActionErrors();
|
|
|
|
$this->assertDatabaseMissing('script_executions', [
|
|
'script_id' => $script->id,
|
|
'user' => 'example',
|
|
]);
|
|
}
|
|
|
|
public function test_see_executions(): void
|
|
{
|
|
$this->actingAs($this->user);
|
|
|
|
$script = Script::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
$scriptExecution = ScriptExecution::factory()->create([
|
|
'script_id' => $script->id,
|
|
'status' => ScriptExecutionStatus::EXECUTING,
|
|
]);
|
|
|
|
$this->get(Executions::getUrl(['script' => $script]))
|
|
->assertSuccessful()
|
|
->assertSee($scriptExecution->status);
|
|
}
|
|
}
|