2017-09-16 06:45:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-09-16 06:45:56 +00:00
|
|
|
{
|
|
|
|
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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with('noResults')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
|
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->twice()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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(),
|
|
|
|
]);
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with($user1->username)->once()->andReturnSelf()
|
2017-09-16 06:45:56 +00:00
|
|
|
->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()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('setSearchTerm')->with(123456)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('all')->withNoArgs()->once()->andReturn(collect());
|
2017-09-16 06:45:56 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|