Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Jun 8, 2024
1 parent ac05a00 commit 849d814
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/Models/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
Expand All @@ -20,6 +21,8 @@
*/
class Script extends AbstractModel
{
use HasFactory;

protected $fillable = [
'user_id',
'name',
Expand Down
10 changes: 5 additions & 5 deletions database/factories/ScriptExecutionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Enums\ScriptExecutionStatus;
use App\Models\ScriptExecution;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
Expand All @@ -13,12 +14,11 @@ class ScriptExecutionFactory extends Factory
public function definition(): array
{
return [
'created_at' => Carbon::now(), //
'user' => 'root',
'variables' => [],
'status' => ScriptExecutionStatus::EXECUTING,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'script_id' => $this->faker->randomNumber(),
'variables' => $this->faker->words(),
'status' => $this->faker->word(),
'server_log_id' => $this->faker->randomNumber(),
];
}
}
22 changes: 22 additions & 0 deletions database/factories/ScriptFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Factories;

use App\Models\Script;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class ScriptFactory extends Factory
{
protected $model = Script::class;

public function definition(): array
{
return [
'name' => $this->faker->name(),
'content' => 'ls -la',
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
}
}
1 change: 1 addition & 0 deletions public/build/assets/app-7f487305.css

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/build/assets/app-f8a673af.css

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"resources/css/app.css": {
"file": "assets/app-f8a673af.css",
"file": "assets/app-7f487305.css",
"isEntry": true,
"src": "resources/css/app.css"
},
Expand Down
156 changes: 156 additions & 0 deletions tests/Feature/ScriptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Tests\Feature;

use App\Enums\ScriptExecutionStatus;
use App\Facades\SSH;
use App\Models\Script;
use App\Models\ScriptExecution;
use Illuminate\Foundation\Testing\RefreshDatabase;
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(
route('scripts.index')
)
->assertSuccessful()
->assertSee($script->name);
}

public function test_create_script(): void
{
$this->actingAs($this->user);

$this->post(
route('scripts.store'),
[
'name' => 'Test Script',
'content' => 'echo "Hello, World!"',
]
)
->assertSessionDoesntHaveErrors()
->assertHeader('HX-Redirect');

$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,
]);

$this->post(route('scripts.edit', ['script' => $script]), [
'name' => 'New Name',
'content' => 'echo "Hello, new World!"',
])
->assertSuccessful()
->assertHeader('HX-Redirect');

$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,
]);

$this->delete(
route('scripts.delete', [
'script' => $script,
])
)->assertRedirect();

$this->assertDatabaseMissing('scripts', [
'id' => $script->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,
]);

$this->post(
route('scripts.execute', [
'script' => $script,
]),
[
'server' => $this->server->id,
'user' => 'root',
]
)
->assertSessionDoesntHaveErrors()
->assertHeader('HX-Redirect');

$this->assertDatabaseHas('script_executions', [
'script_id' => $script->id,
'status' => ScriptExecutionStatus::COMPLETED,
]);

$this->assertDatabaseHas('server_logs', [
'server_id' => $this->server->id,
]);

$execution = $script->lastExecution;

$this->get(
route('scripts.log', [
'script' => $script,
'execution' => $execution,
])
)
->assertRedirect()
->assertSessionHas('content', 'script output');
}

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(
route('scripts.show', [
'script' => $script,
])
)
->assertSuccessful()
->assertSee($scriptExecution->status);
}
}

0 comments on commit 849d814

Please sign in to comment.