Files
panel-pelican-dev/tests/Integration/Api/Client/Server/PowerControllerTest.php

99 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2020-06-27 12:23:40 -07:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Api\Client\Server;
2020-06-27 12:23:40 -07:00
use App\Enums\SubuserPermission;
2025-09-08 08:56:59 +02:00
use App\Repositories\Daemon\DaemonServerRepository;
2024-03-12 22:39:16 -04:00
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
2025-09-24 13:34:19 +02:00
use Illuminate\Http\Response;
use PHPUnit\Framework\Attributes\DataProvider;
2020-06-27 12:23:40 -07:00
class PowerControllerTest extends ClientApiIntegrationTestCase
{
/**
* Test that a subuser without permission to send a command to the server receives
* an error in response. This checks against the specific permission needed to send
* the command to the server.
*
* @param array<string|SubuserPermission> $permissions
2020-06-27 12:23:40 -07:00
*/
#[DataProvider('invalidPermissionDataProvider')]
public function test_subuser_without_permissions_receives_error(string $action, array $permissions): void
2020-06-27 12:23:40 -07:00
{
[$user, $server] = $this->generateTestAccount($permissions);
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])
2020-06-27 12:23:40 -07:00
->assertStatus(Response::HTTP_FORBIDDEN);
}
/**
* Test that sending an invalid power signal returns an error.
*/
public function test_invalid_power_signal_results_in_error(): void
2020-06-27 12:23:40 -07:00
{
[$user, $server] = $this->generateTestAccount();
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/power", [
2020-06-27 12:23:40 -07:00
'signal' => 'invalid',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.meta.rule', 'in');
2020-06-27 12:23:40 -07:00
$response->assertJsonPath('errors.0.detail', 'The selected signal is invalid.');
}
/**
* Test that sending a valid power actions works.
*/
#[DataProvider('validPowerActionDataProvider')]
public function test_action_can_be_sent_to_server(string $action, string|SubuserPermission $permission): void
2020-06-27 12:23:40 -07:00
{
2025-09-08 08:56:59 +02:00
$service = \Mockery::mock(DaemonServerRepository::class);
$this->app->instance(DaemonServerRepository::class, $service);
2020-06-27 12:23:40 -07:00
[$user, $server] = $this->generateTestAccount([$permission]);
$service->expects('setServer')
2023-02-23 12:30:16 -07:00
->with(\Mockery::on(function ($value) use ($server) {
2020-06-27 12:23:40 -07:00
return $server->uuid === $value->uuid;
}))
->andReturnSelf()
->getMock()
2025-09-08 08:56:59 +02:00
->expects('power')
2020-06-27 12:23:40 -07:00
->with(trim($action));
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])
2020-06-27 12:23:40 -07:00
->assertStatus(Response::HTTP_NO_CONTENT);
}
/**
* Returns invalid permission combinations for a given power action.
*/
2023-02-23 12:30:16 -07:00
public static function invalidPermissionDataProvider(): array
2020-06-27 12:23:40 -07:00
{
return [
['start', [SubuserPermission::ControlStop, SubuserPermission::ControlRestart]],
['stop', [SubuserPermission::ControlStart]],
['kill', [SubuserPermission::ControlStart, SubuserPermission::ControlRestart]],
['restart', [SubuserPermission::ControlStop, SubuserPermission::ControlStart]],
['random', [SubuserPermission::ControlStart]],
2020-06-27 12:23:40 -07:00
];
}
2023-02-23 12:30:16 -07:00
public static function validPowerActionDataProvider(): array
2020-06-27 12:23:40 -07:00
{
return [
['start', SubuserPermission::ControlStart],
['stop', SubuserPermission::ControlStop],
['restart', SubuserPermission::ControlRestart],
['kill', SubuserPermission::ControlStop],
2020-06-27 12:23:40 -07:00
// Yes, these spaces are intentional. You should be able to send values with or without
// a space on the start/end since we should be trimming the values.
[' restart', SubuserPermission::ControlRestart],
['kill ', SubuserPermission::ControlStop],
2020-06-27 12:23:40 -07:00
];
}
}