81 lines
3 KiB
PHP
81 lines
3 KiB
PHP
<?php
|
|
|
|
use App\Http\Middleware\EnsureUserIsAdmin;
|
|
use App\Http\Middleware\LogSlowAdminRequests;
|
|
use App\Models\User;
|
|
use Database\Seeders\RolesAndPermissionsSeeder;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Livewire\Livewire;
|
|
use Mockery as MockeryFacade;
|
|
use Psr\Log\LoggerInterface;
|
|
use Tests\TestCase;
|
|
|
|
test('slow admin requests are logged with request and query metrics', function () {
|
|
/** @var TestCase $this */
|
|
$this->seed(RolesAndPermissionsSeeder::class);
|
|
|
|
config([
|
|
'admin_performance.slow_requests.enabled' => true,
|
|
'admin_performance.slow_requests.duration_threshold_ms' => 0,
|
|
'admin_performance.slow_requests.database_threshold_ms' => 0,
|
|
'admin_performance.slow_requests.query_count_threshold' => 0,
|
|
'admin_performance.slow_requests.slow_query_threshold_ms' => 0,
|
|
'admin_performance.slow_requests.channel' => 'admin-slow-test',
|
|
]);
|
|
|
|
$logger = MockeryFacade::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('warning')
|
|
->once()
|
|
->with('Slow admin request detected.', MockeryFacade::on(function (array $context): bool {
|
|
return $context['method'] === 'GET'
|
|
&& $context['path'] === '/admin/roles'
|
|
&& $context['route_name'] === 'admin.roles.index'
|
|
&& $context['status_code'] === 200
|
|
&& is_int($context['user_id'])
|
|
&& is_int($context['duration_ms'])
|
|
&& is_float($context['database_time_ms'])
|
|
&& is_int($context['query_count'])
|
|
&& is_array($context['slow_queries']);
|
|
}));
|
|
|
|
Log::shouldReceive('channel')
|
|
->once()
|
|
->with('admin-slow-test')
|
|
->andReturn($logger);
|
|
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole('admin');
|
|
|
|
$this->actingAs($admin)
|
|
->get(route('admin.roles.index'))
|
|
->assertSuccessful();
|
|
});
|
|
|
|
test('slow admin requests use the dedicated admin log channel by default', function () {
|
|
expect(config('admin_performance.slow_requests.channel'))->toBe('admin_slow')
|
|
->and(config('logging.channels.admin_slow.driver'))->toBe('daily')
|
|
->and(config('logging.channels.admin_slow.path'))->toBe(storage_path('logs/admin-slow.log'))
|
|
->and(config('logging.channels.admin_slow.level'))->toBe('warning');
|
|
});
|
|
|
|
test('slow admin request logging can be disabled', function () {
|
|
/** @var TestCase $this */
|
|
$this->seed(RolesAndPermissionsSeeder::class);
|
|
|
|
config(['admin_performance.slow_requests.enabled' => false]);
|
|
|
|
Log::shouldReceive('channel')->never();
|
|
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole('admin');
|
|
|
|
$this->actingAs($admin)
|
|
->get(route('admin.roles.index'))
|
|
->assertSuccessful();
|
|
});
|
|
|
|
test('admin middleware is persisted for livewire follow-up requests', function () {
|
|
expect(Livewire::getPersistentMiddleware())
|
|
->toContain(EnsureUserIsAdmin::class)
|
|
->toContain(LogSlowAdminRequests::class);
|
|
});
|