From 50904d0e6a15847925dbf812ea7fcf82f1ceb8ed Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Sun, 11 Oct 2026 14:01:27 -0400 Subject: [PATCH] power activity --- app/Actions/Servers/SendServerCommand.php | 3 ++ app/Actions/Servers/SendServerPower.php | 3 ++ .../Api/Client/Servers/CommandController.php | 3 -- .../Api/Client/Servers/PowerController.php | 3 -- tests/Assertions/AssertsActivityLogged.php | 25 ++++++++++++++- .../Actions/Servers/SendServerPowerTest.php | 32 +++++++++++++++++++ .../Client/Server/CommandControllerTest.php | 1 + .../Api/Client/Server/PowerControllerTest.php | 1 + 8 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/Integration/Actions/Servers/SendServerPowerTest.php diff --git a/app/Actions/Servers/SendServerCommand.php b/app/Actions/Servers/SendServerCommand.php index 8998b2c49..6b185a19a 100644 --- a/app/Actions/Servers/SendServerCommand.php +++ b/app/Actions/Servers/SendServerCommand.php @@ -7,6 +7,7 @@ namespace Pterodactyl\Actions\Servers; use Illuminate\Http\Response; use Pterodactyl\Contracts\Servers\SendsServerCommands; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; +use Pterodactyl\Facades\Activity; use Pterodactyl\Facades\Daemon; use Pterodactyl\Models\Server; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -22,5 +23,7 @@ final readonly class SendServerCommand implements SendsServerCommands throw $daemonConnectionException; } + + Activity::event('server:console.command')->subject($server)->property('command', $command)->log(); } } diff --git a/app/Actions/Servers/SendServerPower.php b/app/Actions/Servers/SendServerPower.php index 6e036c3ad..befdde985 100644 --- a/app/Actions/Servers/SendServerPower.php +++ b/app/Actions/Servers/SendServerPower.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Pterodactyl\Actions\Servers; use Pterodactyl\Contracts\Servers\SendsServerPower; +use Pterodactyl\Facades\Activity; use Pterodactyl\Facades\Daemon; use Pterodactyl\Models\Server; @@ -13,5 +14,7 @@ final readonly class SendServerPower implements SendsServerPower public function send(Server $server, string $signal): void { Daemon::server($server)->power($signal); + + Activity::event(mb_strtolower("server:power.{$signal}"))->subject($server)->log(); } } diff --git a/app/Http/Controllers/Api/Client/Servers/CommandController.php b/app/Http/Controllers/Api/Client/Servers/CommandController.php index 4e0b9285b..76fb83b43 100644 --- a/app/Http/Controllers/Api/Client/Servers/CommandController.php +++ b/app/Http/Controllers/Api/Client/Servers/CommandController.php @@ -11,7 +11,6 @@ use Knuckles\Scribe\Attributes\Response as ScribeResponse; use Knuckles\Scribe\Attributes\Subgroup; use Pterodactyl\Contracts\Servers\SendsServerCommands; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; -use Pterodactyl\Facades\Activity; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest; use Pterodactyl\Models\Server; @@ -44,8 +43,6 @@ class CommandController extends ClientApiController $command = JsonValueGuard::string($request->validated('command')); $commands->send($server, $command); - Activity::event('server:console.command')->property('command', $command)->log(); - return $this->returnNoContent(); } } diff --git a/app/Http/Controllers/Api/Client/Servers/PowerController.php b/app/Http/Controllers/Api/Client/Servers/PowerController.php index 49f6c8b38..d93d87bba 100644 --- a/app/Http/Controllers/Api/Client/Servers/PowerController.php +++ b/app/Http/Controllers/Api/Client/Servers/PowerController.php @@ -10,7 +10,6 @@ use Knuckles\Scribe\Attributes\Group; use Knuckles\Scribe\Attributes\Response as ScribeResponse; use Knuckles\Scribe\Attributes\Subgroup; use Pterodactyl\Contracts\Servers\SendsServerPower; -use Pterodactyl\Facades\Activity; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest; use Pterodactyl\Models\Server; @@ -30,8 +29,6 @@ class PowerController extends ClientApiController $signal = JsonValueGuard::string($request->validated('signal')); $power->send($server, $signal); - Activity::event(mb_strtolower("server:power.{$signal}"))->log(); - return $this->returnNoContent(); } } diff --git a/tests/Assertions/AssertsActivityLogged.php b/tests/Assertions/AssertsActivityLogged.php index 30dd42ac0..99b716383 100644 --- a/tests/Assertions/AssertsActivityLogged.php +++ b/tests/Assertions/AssertsActivityLogged.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace Pterodactyl\Tests\Assertions; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Event; use PHPUnit\Framework\Assert; use Pterodactyl\Events\ActivityLogged; +use Pterodactyl\Models\ActivityLog; use Pterodactyl\Models\ActivityLogSubject; trait AssertsActivityLogged @@ -22,6 +24,27 @@ trait AssertsActivityLogged $this->assertActivitySubjects($event, ...$subjects); } + /** + * Asserts that exactly one entry was stored for the event against all of the given + * subjects, which is what puts it on that server's or user's activity feed. + */ + public function assertActivityOn(string $event, Model ...$subjects): ActivityLog + { + $query = ActivityLog::query()->where('event', $event); + foreach ($subjects as $subject) { + $query->whereHas('subjects', fn (Builder $builder) => $builder->where('subject_type', $subject->getMorphClass())->where('subject_id', $subject->getKey())); + } + + $found = $query->get(); + Assert::assertCount(1, $found, sprintf( + 'Failed asserting that one "%s" event was logged against %s.', + $event, + implode(', ', array_map(fn (Model $subject): string => sprintf('%s[%d]', $subject::class, $subject->getKey()), $subjects)), + )); + + return $found->sole(); + } + /** * Asserts that the given activity log event was stored in the database. */ @@ -49,7 +72,7 @@ trait AssertsActivityLogged foreach ($subjects as $subject) { $match = $e->model->subjects->first(function (ActivityLogSubject $model) use ($subject) { return $model->subject_type === $subject->getMorphClass() - && $model->subject_id = $subject->getKey(); + && $model->subject_id === $subject->getKey(); }); Assert::assertNotNull( diff --git a/tests/Integration/Actions/Servers/SendServerPowerTest.php b/tests/Integration/Actions/Servers/SendServerPowerTest.php new file mode 100644 index 000000000..71041c78b --- /dev/null +++ b/tests/Integration/Actions/Servers/SendServerPowerTest.php @@ -0,0 +1,32 @@ +createServerModel(); + + $this->app->make(SendsServerPower::class)->send($server, 'restart'); + + $this->assertActivityOn('server:power.restart', $server); +}); + +test('a command sent without the http api is logged on the server', function (): void { + new FakeDaemonCommand; + $server = $this->createServerModel(); + + $this->app->make(SendsServerCommands::class)->send($server, 'say hi'); + + expect($this->assertActivityOn('server:console.command', $server)->properties->get('command'))->toBe('say hi'); +}); diff --git a/tests/Integration/Api/Client/Server/CommandControllerTest.php b/tests/Integration/Api/Client/Server/CommandControllerTest.php index 22b50fd4b..d1ac057c2 100644 --- a/tests/Integration/Api/Client/Server/CommandControllerTest.php +++ b/tests/Integration/Api/Client/Server/CommandControllerTest.php @@ -30,6 +30,7 @@ test('command can send to server', function () { $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", ['command' => 'say Test']); $response->assertStatus(Response::HTTP_NO_CONTENT); $fake->assertSent('say Test'); + $this->assertActivityFor('server:console.command', $user, $server); Http::assertSent(fn (HttpRequest $request): bool => str_contains($request->url(), "/api/servers/{$server->uuid}/")); }); test('error is returned when server is offline', function () { diff --git a/tests/Integration/Api/Client/Server/PowerControllerTest.php b/tests/Integration/Api/Client/Server/PowerControllerTest.php index 5917e3e55..fed4e3aa0 100644 --- a/tests/Integration/Api/Client/Server/PowerControllerTest.php +++ b/tests/Integration/Api/Client/Server/PowerControllerTest.php @@ -45,4 +45,5 @@ test('action can be sent to server', function (string $action, string $permissio [$user, $server] = $this->generateTestAccount([$permission]); $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/power", ['signal' => $action])->assertStatus(Response::HTTP_NO_CONTENT); $fake->assertSentTo(mb_trim($action), $server->uuid); + $this->assertActivityFor('server:power.'.mb_trim($action), $user, $server); })->with('validPowerActionDataProvider');