clusev/tests/Feature/ShellBadgesTest.php

47 lines
1.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\AlertIncident;
use App\Models\AlertRule;
use App\Models\Server;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/** The polled sidebar badge endpoint (live alert/threat/update chips). */
class ShellBadgesTest extends TestCase
{
use RefreshDatabase;
public function test_guests_cannot_read_the_badge_counts(): void
{
// The panel's auth layer redirects guests to the login page (no data leaves).
$this->get('/shell/badges.json')->assertRedirect();
}
public function test_admin_gets_the_firing_alert_count(): void
{
$this->actingAs(User::factory()->create(['must_change_password' => false]));
$server = Server::create(['name' => 'box', 'ip' => '10.0.0.1', 'ssh_port' => 22, 'status' => 'online']);
$rule = AlertRule::create(['name' => 'CPU', 'metric' => 'cpu', 'comparator' => 'gt', 'threshold' => 80, 'scope_type' => 'all', 'enabled' => true]);
AlertIncident::create(['alert_rule_id' => $rule->id, 'server_id' => $server->id, 'state' => 'firing', 'value' => 95, 'firing_key' => $rule->id.':'.$server->id, 'started_at' => now()]);
$this->getJson('/shell/badges.json')
->assertOk()
->assertJson(['alerts' => 1, 'threats' => 0]);
}
public function test_non_panel_roles_get_zeroed_alert_and_threat_counts(): void
{
// Operators/viewers don't see the Alerts/Threats nav items, so the endpoint must not leak
// those counts to them either.
$this->actingAs(User::factory()->operator()->create(['must_change_password' => false]));
$this->getJson('/shell/badges.json')
->assertOk()
->assertJson(['alerts' => 0, 'threats' => 0]);
}
}