2017-08-05 00:11:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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-08-05 00:11:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Users;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Tests\TestCase;
|
2017-08-05 00:11:41 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Contracts\Translation\Translator;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Services\Users\UserDeletionService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserDeletionServiceTest extends TestCase
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Translation\Translator
|
|
|
|
*/
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $serverRepository;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\UserDeletionService
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = factory(User::class)->make();
|
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
|
|
|
$this->translator = m::mock(Translator::class);
|
|
|
|
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new UserDeletionService(
|
2017-08-22 03:10:48 +00:00
|
|
|
$this->serverRepository,
|
|
|
|
$this->translator,
|
|
|
|
$this->repository
|
2017-08-05 00:11:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user is deleted if they have no servers.
|
|
|
|
*/
|
|
|
|
public function testUserIsDeletedIfNoServersAreAttachedToAccount()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:23:02 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertEquals(1, $this->service->handle($this->user->id));
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if trying to delete a user with servers.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServersAreAttachedToAccount()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:23:02 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(1);
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->translator->shouldReceive('trans')->with('admin/user.exceptions.user_has_servers')->once()->andReturnNull();
|
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->service->handle($this->user->id);
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the function supports passing in a model or an ID.
|
|
|
|
*/
|
2017-08-05 22:20:07 +00:00
|
|
|
public function testModelCanBePassedInPlaceOfUserId()
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->serverRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
|
2017-08-05 22:23:02 +00:00
|
|
|
->shouldReceive('findCountWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(0);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(1);
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertEquals(1, $this->service->handle($this->user));
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
}
|