mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-29 10:55:43 -05:00
Return correct error message when deleting self, add test coverage (#5456)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user