From 54246206b6841b2e4e344fd7e87d1db86c4cce02 Mon Sep 17 00:00:00 2001 From: Arunas Skirius Date: Sun, 16 Mar 2025 12:17:43 +0200 Subject: [PATCH 1/2] Make it possible to exclude "full_text" from logs API response --- src/Http/Resources/LogResource.php | 10 ++++++++-- tests/Feature/LogsControllerTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Http/Resources/LogResource.php b/src/Http/Resources/LogResource.php index ecef144d..ab2a03ac 100644 --- a/src/Http/Resources/LogResource.php +++ b/src/Http/Resources/LogResource.php @@ -14,8 +14,9 @@ class LogResource extends JsonResource public function toArray($request): array { $level = $this->getLevel(); + $excludeFullText = $request->boolean('exclude_full_text', false); - return [ + $data = [ 'index' => $this->index, 'file_identifier' => $this->fileIdentifier, 'file_position' => $this->filePosition, @@ -30,8 +31,13 @@ public function toArray($request): array 'context' => $this->context, 'extra' => $this->extra, - 'full_text' => $this->getOriginalText(), 'url' => $this->url(), ]; + + if (! $excludeFullText) { + $data['full_text'] = $this->getOriginalText(); + } + + return $data; } } diff --git a/tests/Feature/LogsControllerTest.php b/tests/Feature/LogsControllerTest.php index 7a4dddf7..cd7f23a3 100644 --- a/tests/Feature/LogsControllerTest.php +++ b/tests/Feature/LogsControllerTest.php @@ -61,3 +61,30 @@ ])); expect($response->json('logs'))->toHaveCount(4); }); + +test('logs include full_text property by default', function () { + $logEntries = [ + makeLaravelLogEntry(message: 'Test message'), + ]; + $file = generateLogFile('log_with_full_text.log', implode(PHP_EOL, $logEntries)); + + $response = getJson(route('log-viewer.logs', ['file' => $file->identifier])); + + expect($response->json('logs'))->toHaveCount(1); + expect($response->json('logs.0'))->toHaveKey('full_text'); +}); + +test('logs can exclude full_text property when requested', function () { + $logEntries = [ + makeLaravelLogEntry(message: 'Test message'), + ]; + $file = generateLogFile('log_without_full_text.log', implode(PHP_EOL, $logEntries)); + + $response = getJson(route('log-viewer.logs', [ + 'file' => $file->identifier, + 'exclude_full_text' => true, + ])); + + expect($response->json('logs'))->toHaveCount(1); + expect($response->json('logs.0'))->not->toHaveKey('full_text'); +}); From bbf4614def24769b518a471e7f17faa34ef092e1 Mon Sep 17 00:00:00 2001 From: arukompas Date: Sun, 16 Mar 2025 10:18:11 +0000 Subject: [PATCH 2/2] Fix styling --- tests/Feature/Authorization/CanDownloadFoldersTest.php | 10 +++++----- tests/Feature/Authorization/CanDownloadLogFileTest.php | 10 +++++----- tests/Feature/Authorization/CanViewLogViewerTest.php | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Feature/Authorization/CanDownloadFoldersTest.php b/tests/Feature/Authorization/CanDownloadFoldersTest.php index af10dd59..3d36089f 100644 --- a/tests/Feature/Authorization/CanDownloadFoldersTest.php +++ b/tests/Feature/Authorization/CanDownloadFoldersTest.php @@ -21,8 +21,8 @@ function assertCanDownloadFolder(string $folderName, string $expectedFileName): function assertCannotDownloadFolder(string $folderName): void { -get(route('log-viewer.folders.request-download', $folderName)) -->assertForbidden(); + get(route('log-viewer.folders.request-download', $folderName)) + ->assertForbidden(); } test('can download every folder by default', function () { @@ -33,9 +33,9 @@ function assertCannotDownloadFolder(string $folderName): void }); test('cannot download a folder that\'s not found', function () { -get(route('log-viewer.folders.request-download', 'notfound')) -->assertNotFound(); - }); + get(route('log-viewer.folders.request-download', 'notfound')) + ->assertNotFound(); +}); test('"downloadLogFolder" gate can prevent folder download', function () { generateLogFiles([$fileName = 'laravel.log']); diff --git a/tests/Feature/Authorization/CanDownloadLogFileTest.php b/tests/Feature/Authorization/CanDownloadLogFileTest.php index 643b244f..ed73a043 100644 --- a/tests/Feature/Authorization/CanDownloadLogFileTest.php +++ b/tests/Feature/Authorization/CanDownloadLogFileTest.php @@ -20,8 +20,8 @@ function assertCanDownloadFile(string $fileName): void function assertCannotDownloadFile(string $fileName): void { -get(route('log-viewer.files.request-download', $fileName)) -->assertForbidden(); + get(route('log-viewer.files.request-download', $fileName)) + ->assertForbidden(); } test('can download every file by default', function () { @@ -31,9 +31,9 @@ function assertCannotDownloadFile(string $fileName): void }); test('cannot download a file that\'s not found', function () { -get(route('log-viewer.files.request-download', 'notfound.log')) -->assertNotFound(); - }); + get(route('log-viewer.files.request-download', 'notfound.log')) + ->assertNotFound(); +}); test('"downloadLogFile" gate can prevent file download', function () { generateLogFiles([$fileName = 'laravel.log']); diff --git a/tests/Feature/Authorization/CanViewLogViewerTest.php b/tests/Feature/Authorization/CanViewLogViewerTest.php index e061d114..174adb55 100644 --- a/tests/Feature/Authorization/CanViewLogViewerTest.php +++ b/tests/Feature/Authorization/CanViewLogViewerTest.php @@ -6,7 +6,7 @@ use function Pest\Laravel\get; test('can define an "auth" callback for authorization', function () { -get(route('log-viewer.index'))->assertOk(); + get(route('log-viewer.index'))->assertOk(); // with the gate defined and a false value, it should not be possible to access the log viewer LogViewer::auth(fn ($request) => false); @@ -15,7 +15,7 @@ // now let's give them access LogViewer::auth(fn ($request) => true); get(route('log-viewer.index'))->assertOk(); - }); +}); test('the "auth" callback is given with a Request object to check against', function () { LogViewer::auth(function ($request) { @@ -28,14 +28,14 @@ }); test('can define a "viewLogViewer" gate as an alternative', function () { -get(route('log-viewer.index'))->assertOk(); + get(route('log-viewer.index'))->assertOk(); Gate::define('viewLogViewer', fn ($user = null) => false); get(route('log-viewer.index'))->assertForbidden(); Gate::define('viewLogViewer', fn ($user = null) => true); get(route('log-viewer.index'))->assertOk(); - }); +}); test('local environment can use Log Viewer by default', function () { app()->detectEnvironment(fn () => 'local');