2017-09-16 06:45:56 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-16 06:45:56 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-16 06:45:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Commands\User;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\User;
|
2017-09-22 05:30:09 +00:00
|
|
|
use Tests\Unit\Commands\CommandTestCase;
|
2017-09-16 06:45:56 +00:00
|
|
|
use Tests\Assertions\CommandAssertionsTrait;
|
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
|
|
|
use Pterodactyl\Console\Commands\User\DeleteUserCommand;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
class DeleteUserCommandTest extends CommandTestCase
|
2017-09-16 06:45:56 +00:00
|
|
|
{
|
|
|
|
use CommandAssertionsTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Console\Commands\User\DeleteUserCommand
|
|
|
|
*/
|
|
|
|
protected $command;
|
|
|
|
|
|
|
|
/**
|
2017-09-22 05:30:09 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserDeletionService|\Mockery\Mock
|
2017-09-16 06:45:56 +00:00
|
|
|
*/
|
|
|
|
protected $deletionService;
|
|
|
|
|
|
|
|
/**
|
2017-09-22 05:30:09 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-09-16 06:45:56 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->deletionService = m::mock(UserDeletionService::class);
|
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
$this->command = new DeleteUserCommand($this->deletionService, $this->repository);
|
|
|
|
$this->command->setLaravel($this->app);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user can be deleted using a normal pathway.
|
|
|
|
*/
|
|
|
|
public function testCommandWithNoOptions()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
$user2 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
|
|
|
|
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'yes']);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertTableContains($user1->id, $display);
|
|
|
|
$this->assertTableContains($user1->email, $display);
|
|
|
|
$this->assertTableContains($user1->name, $display);
|
|
|
|
$this->assertContains(trans('command/messages.user.deleted'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a bad first user search followed by a good second search.
|
|
|
|
*/
|
|
|
|
public function testCommandWithInvalidInitialSearch()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with('noResults')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn([]);
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
|
|
|
|
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command, [], ['noResults', $user1->username, $user1->id, 'yes']);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.user.no_users_found'), $display);
|
|
|
|
$this->assertTableContains($user1->id, $display);
|
|
|
|
$this->assertTableContains($user1->email, $display);
|
|
|
|
$this->assertTableContains($user1->name, $display);
|
|
|
|
$this->assertContains(trans('command/messages.user.deleted'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the ability to re-do a search for a user account.
|
|
|
|
*/
|
|
|
|
public function testReSearchAbility()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->twice()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->twice()->andReturn($users);
|
|
|
|
$this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull();
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command, [], [$user1->username, 0, $user1->username, $user1->id, 'yes']);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.user.select_search_user'), $display);
|
|
|
|
$this->assertTableContains($user1->id, $display);
|
|
|
|
$this->assertTableContains($user1->email, $display);
|
|
|
|
$this->assertTableContains($user1->name, $display);
|
|
|
|
$this->assertContains(trans('command/messages.user.deleted'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that answering no works as expected when confirming deletion of account.
|
|
|
|
*/
|
|
|
|
public function testAnsweringNoToDeletionConfirmationWillNotDeleteUser()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
|
|
|
|
$this->deletionService->shouldNotReceive('handle');
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'no']);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertNotContains(trans('command/messages.user.deleted'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a single result is deleted if there is no interaction setup.
|
|
|
|
*/
|
|
|
|
public function testNoInteractionWithSingleResult()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
|
|
|
|
$this->deletionService->shouldReceive('handle')->with($user1)->once()->andReturnNull();
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.user.deleted'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error is returned if there is no interaction but multiple results.
|
|
|
|
*/
|
|
|
|
public function testNoInteractionWithMultipleResults()
|
|
|
|
{
|
|
|
|
$users = collect([
|
|
|
|
$user1 = factory(User::class)->make(),
|
|
|
|
$user2 = factory(User::class)->make(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('search')->with($user1->username)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn($users);
|
|
|
|
$this->deletionService->shouldNotReceive('handle');
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.user.multiple_found'), $display);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error is returned if there is no interaction and no results returned.
|
|
|
|
*/
|
|
|
|
public function testNoInteractionWithNoResults()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('search')->with(123456)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn([]);
|
|
|
|
|
2017-09-22 05:30:09 +00:00
|
|
|
$display = $this->withoutInteraction()->runCommand($this->command, ['--user' => 123456]);
|
2017-09-16 06:45:56 +00:00
|
|
|
|
|
|
|
$this->assertNotEmpty($display);
|
|
|
|
$this->assertContains(trans('command/messages.user.no_users_found'), $display);
|
|
|
|
}
|
|
|
|
}
|