Return correct error message when deleting self, add test coverage (#5456)

This commit is contained in:
Dane Everitt
2025-12-24 16:13:31 -08:00
committed by GitHub
parent a7c1882edf
commit 0917e60a3b
6 changed files with 82 additions and 3 deletions
@@ -89,8 +89,8 @@ class UserController extends Controller
*/
public function delete(Request $request, User $user): RedirectResponse
{
if ($request->user()->id === $user->id) {
throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers'));
if ($request->user()->is($user)) {
throw new DisplayException(__('admin/user.exceptions.delete_self'));
}
$this->deletionService->handle($user);
+1
View File
@@ -2,6 +2,7 @@
return [
'exceptions' => [
'delete_self' => 'You cannot delete your own account.',
'user_has_servers' => 'Cannot delete a user with active servers attached to their account. Please delete their servers before continuing.',
],
'notices' => [
+1 -1
View File
@@ -93,7 +93,7 @@ Route::group(['prefix' => 'users'], function () {
Route::post('/new', [Admin\UserController::class, 'store']);
Route::patch('/view/{user:id}', [Admin\UserController::class, 'update']);
Route::delete('/view/{user:id}', [Admin\UserController::class, 'delete']);
Route::delete('/view/{user:id}', [Admin\UserController::class, 'delete'])->name('admin.users.delete');
});
/*
@@ -0,0 +1,37 @@
<?php
namespace Pterodactyl\Tests\Integration\Http\Controllers\Admin\UserController;
use Pterodactyl\Models\User;
use Pterodactyl\Tests\Integration\Http\HttpTestCase;
class DeleteUserTest extends HttpTestCase
{
public function testNonAdminCannotAccessEndpoint(): void
{
$this->actingAs(User::factory()->create())
->delete(route('admin.users.delete', ['user' => User::factory()->create()]))
->assertForbidden();
}
public function testCannotDeleteSelf(): void
{
$this->actingAs($user = User::factory()->admin()->create())
->delete(route('admin.users.delete', ['user' => $user]))
->assertBadRequest()
->assertJsonPath('errors.0.detail', __('admin/user.exceptions.delete_self'));
$this->assertModelExists($user);
}
public function testUserIsDeleted(): void
{
$user = User::factory()->create();
$this->actingAs(User::factory()->admin()->create())
->delete(route('admin.users.delete', ['user' => $user]))
->assertRedirectToRoute('admin.users');
$this->assertModelMissing($user);
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace Pterodactyl\Tests\Integration\Http;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
abstract class HttpTestCase extends IntegrationTestCase
{
}
@@ -0,0 +1,32 @@
<?php
namespace Pterodactyl\Tests\Integration\Services\Users;
use Pterodactyl\Models\User;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Users\UserDeletionService;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
class UserDeletionServiceTest extends IntegrationTestCase
{
public function testExceptionReturnedIfUserAssignedToServers(): void
{
$server = $this->createServerModel();
$this->expectException(DisplayException::class);
$this->expectExceptionMessage(__('admin/user.exceptions.user_has_servers'));
$this->app->make(UserDeletionService::class)->handle($server->user);
$this->assertModelExists($server->user);
}
public function testUserIsDeleted(): void
{
$user = User::factory()->create();
$this->app->make(UserDeletionService::class)->handle($user);
$this->assertModelMissing($user);
}
}