clusev/tests/Feature/AuditLogDisplayTest.php

62 lines
2.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Audit\Index;
use App\Models\AuditEvent;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class AuditLogDisplayTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->actingAs(User::factory()->create(['must_change_password' => false]));
}
public function test_action_label_maps_a_known_code(): void
{
// Default locale is German; the raw code maps to a readable label.
$this->assertSame('WireGuard-Endpoint geändert', (new AuditEvent(['action' => 'wg.set-endpoint']))->action_label);
}
public function test_action_label_falls_back_to_a_tidied_form_for_unknown_codes(): void
{
// Dynamic harden.* codes are not individually mapped → tidied, never a bare machine code.
$e = new AuditEvent(['action' => 'harden.ssh_password.off']);
$this->assertSame('Harden · ssh password · off', $e->action_label);
}
public function test_is_error_flags_failure_and_security_events(): void
{
$this->assertTrue((new AuditEvent(['action' => 'auth.login_failed']))->is_error);
$this->assertTrue((new AuditEvent(['action' => 'wg.action-failed']))->is_error);
$this->assertFalse((new AuditEvent(['action' => 'wg.set-endpoint']))->is_error);
}
public function test_page_renders_the_readable_label_not_the_raw_code(): void
{
AuditEvent::create(['actor' => 'Administrator', 'action' => 'wg.set-endpoint', 'target' => '1.2.3.4:51820', 'ip' => '127.0.0.1']);
Livewire::test(Index::class)
->assertSee('WireGuard-Endpoint geändert')
->assertDontSee('wg.set-endpoint');
}
public function test_search_matches_the_readable_label(): void
{
AuditEvent::create(['actor' => 'Administrator', 'action' => 'wg.set-endpoint', 'target' => 'x', 'ip' => '127.0.0.1']);
AuditEvent::create(['actor' => 'Administrator', 'action' => 'user.create', 'target' => 'y', 'ip' => '127.0.0.1']);
Livewire::test(Index::class)
->set('q', 'Endpoint geändert')
->assertSee('WireGuard-Endpoint geändert')
->assertDontSee('Benutzer angelegt');
}
}