Update user service to be more separated
This commit is contained in:
parent
8daec38622
commit
275c01bc37
12 changed files with 473 additions and 1361 deletions
178
tests/Unit/Services/Users/CreationServiceTest.php
Normal file
178
tests/Unit/Services/Users/CreationServiceTest.php
Normal file
|
@ -0,0 +1,178 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Services\Users\CreationService;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Contracts\Hashing\Hasher;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Notifications\ChannelManager;
|
||||
use Pterodactyl\Notifications\AccountCreated;
|
||||
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
|
||||
class CreationServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Foundation\Application
|
||||
*/
|
||||
protected $appMock;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Hashing\Hasher
|
||||
*/
|
||||
protected $hasher;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Notifications\ChannelManager
|
||||
*/
|
||||
protected $notification;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
|
||||
*/
|
||||
protected $passwordService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\CreationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->appMock = m::mock(Application::class);
|
||||
$this->database = m::mock(ConnectionInterface::class);
|
||||
$this->hasher = m::mock(Hasher::class);
|
||||
$this->notification = m::mock(ChannelManager::class);
|
||||
$this->passwordService = m::mock(TemporaryPasswordService::class);
|
||||
$this->repository = m::mock(UserRepositoryInterface::class);
|
||||
|
||||
$this->service = new CreationService(
|
||||
$this->appMock,
|
||||
$this->notification,
|
||||
$this->database,
|
||||
$this->hasher,
|
||||
$this->passwordService,
|
||||
$this->repository
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a user is created when a password is passed.
|
||||
*/
|
||||
public function testUserIsCreatedWhenPasswordIsProvided()
|
||||
{
|
||||
$user = (object) [
|
||||
'name_first' => 'FirstName',
|
||||
'username' => 'user_name',
|
||||
];
|
||||
|
||||
$this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password');
|
||||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->hasher->shouldNotReceive('make');
|
||||
$this->passwordService->shouldNotReceive('generateReset');
|
||||
$this->repository->shouldReceive('create')->with(['password' => 'enc-password'])->once()->andReturn($user);
|
||||
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
|
||||
'user' => [
|
||||
'name' => 'FirstName',
|
||||
'username' => 'user_name',
|
||||
'token' => null,
|
||||
],
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle([
|
||||
'password' => 'raw-password',
|
||||
]);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals($user->username, $response->username);
|
||||
$this->assertEquals($user->name_first, 'FirstName');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a user is created with a random password when no password is provided.
|
||||
*/
|
||||
public function testUserIsCreatedWhenNoPasswordIsProvided()
|
||||
{
|
||||
$user = (object) [
|
||||
'name_first' => 'FirstName',
|
||||
'username' => 'user_name',
|
||||
'email' => 'user@example.com',
|
||||
];
|
||||
|
||||
$this->hasher->shouldNotReceive('make');
|
||||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->hasher->shouldReceive('make')->once()->andReturn('created-enc-password');
|
||||
$this->passwordService->shouldReceive('generateReset')
|
||||
->with('user@example.com')
|
||||
->once()
|
||||
->andReturn('random-token');
|
||||
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'password' => 'created-enc-password',
|
||||
'email' => 'user@example.com',
|
||||
])->once()->andReturn($user);
|
||||
|
||||
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
$this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [
|
||||
'user' => [
|
||||
'name' => 'FirstName',
|
||||
'username' => 'user_name',
|
||||
'token' => 'random-token',
|
||||
],
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$this->notification->shouldReceive('send')->with($user, null)->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle([
|
||||
'email' => 'user@example.com',
|
||||
]);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals($user->username, $response->username);
|
||||
$this->assertEquals($user->name_first, 'FirstName');
|
||||
$this->assertEquals($user->email, $response->email);
|
||||
}
|
||||
}
|
120
tests/Unit/Services/Users/DeletionServiceTest.php
Normal file
120
tests/Unit/Services/Users/DeletionServiceTest.php
Normal file
|
@ -0,0 +1,120 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use Illuminate\Contracts\Translation\Translator;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Services\Users\DeletionService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeletionServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Translation\Translator
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $serverRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\DeletionService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
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);
|
||||
|
||||
$this->service = new DeletionService(
|
||||
$this->serverRepository, $this->translator, $this->repository
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a user is deleted if they have no servers.
|
||||
*/
|
||||
public function testUserIsDeletedIfNoServersAreAttachedToAccount()
|
||||
{
|
||||
$this->serverRepository->shouldReceive('findWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn([]);
|
||||
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle($this->user),
|
||||
'Assert that service responds true.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if trying to delete a user with servers.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function testExceptionIsThrownIfServersAreAttachedToAccount()
|
||||
{
|
||||
$this->serverRepository->shouldReceive('findWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn(['item']);
|
||||
$this->translator->shouldReceive('trans')->with('admin/user.exceptions.user_has_servers')->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($this->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the function supports passing in a model or an ID.
|
||||
*/
|
||||
public function testIntegerCanBePassedInPlaceOfUserModel()
|
||||
{
|
||||
$this->repository->shouldReceive('find')->with($this->user->id)->once()->andReturn($this->user);
|
||||
$this->serverRepository->shouldReceive('findWhere')->with([['owner_id', '=', $this->user->id]])->once()->andReturn([]);
|
||||
$this->repository->shouldReceive('delete')->with($this->user->id)->once()->andReturn(true);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->service->handle($this->user->id),
|
||||
'Assert that service responds true.'
|
||||
);
|
||||
}
|
||||
}
|
83
tests/Unit/Services/Users/UpdateServiceTest.php
Normal file
83
tests/Unit/Services/Users/UpdateServiceTest.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\Users;
|
||||
|
||||
use Illuminate\Contracts\Hashing\Hasher;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Services\Users\UpdateService;
|
||||
use Tests\TestCase;
|
||||
use Mockery as m;
|
||||
|
||||
class UpdateServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Hashing\Hasher
|
||||
*/
|
||||
protected $hasher;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\UpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->hasher = m::mock(Hasher::class);
|
||||
$this->repository = m::mock(UserRepositoryInterface::class);
|
||||
|
||||
$this->service = new UpdateService($this->hasher, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the handle function does not attempt to hash a password if no password is passed.
|
||||
*/
|
||||
public function testUpdateUserWithoutTouchingHasherIfNoPasswordPassed()
|
||||
{
|
||||
$this->repository->shouldReceive('update')->with(1, ['test-data' => 'value'])->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->service->handle(1, ['test-data' => 'value']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the handle function hashes a password if passed in the data array.
|
||||
*/
|
||||
public function testUpdateUserAndHashPasswordIfProvided()
|
||||
{
|
||||
$this->hasher->shouldReceive('make')->with('raw_pass')->once()->andReturn('enc_pass');
|
||||
$this->repository->shouldReceive('update')->with(1, ['password' => 'enc_pass'])->once()->andReturnNull();
|
||||
|
||||
$this->assertNull($this->service->handle(1, ['password' => 'raw_pass']));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue