Service refactor to improve organization
This commit is contained in:
parent
761d34f178
commit
2588c25b0b
12 changed files with 79 additions and 42 deletions
193
tests/Unit/Services/Administrative/DatabaseHostServiceTest.php
Normal file
193
tests/Unit/Services/Administrative/DatabaseHostServiceTest.php
Normal file
|
@ -0,0 +1,193 @@
|
|||
<?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\Administrative;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostInterface;
|
||||
use Pterodactyl\Services\Administrative\DatabaseHostService;
|
||||
|
||||
class DatabaseHostServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\DatabaseManager
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
|
||||
*/
|
||||
protected $dynamic;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Encryption\Encrypter
|
||||
*/
|
||||
protected $encrypter;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseHostInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Administrative\DatabaseHostService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->database = m::mock(DatabaseManager::class);
|
||||
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
|
||||
$this->encrypter = m::mock(Encrypter::class);
|
||||
$this->repository = m::mock(DatabaseHostInterface::class);
|
||||
|
||||
$this->service = new DatabaseHostService(
|
||||
$this->repository,
|
||||
$this->database,
|
||||
$this->dynamic,
|
||||
$this->encrypter
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that creating a host returns the correct data.
|
||||
*/
|
||||
public function test_create_host_function()
|
||||
{
|
||||
$data = [
|
||||
'password' => 'raw-password',
|
||||
'name' => 'HostName',
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 3306,
|
||||
'username' => 'someusername',
|
||||
'node_id' => null,
|
||||
];
|
||||
|
||||
$finalData = (object) array_replace($data, ['password' => 'enc-password']);
|
||||
|
||||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->encrypter->shouldReceive('encrypt')->with('raw-password')->once()->andReturn('enc-password');
|
||||
|
||||
$this->repository->shouldReceive('create')->with([
|
||||
'password' => 'enc-password',
|
||||
'name' => 'HostName',
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 3306,
|
||||
'username' => 'someusername',
|
||||
'max_databases' => null,
|
||||
'node_id' => null,
|
||||
])->once()->andReturn($finalData);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
|
||||
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
|
||||
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
|
||||
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->create($data);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertTrue(is_object($response), 'Assert that response is an object.');
|
||||
|
||||
$this->assertEquals('enc-password', $response->password);
|
||||
$this->assertEquals('HostName', $response->name);
|
||||
$this->assertEquals('127.0.0.1', $response->host);
|
||||
$this->assertEquals(3306, $response->port);
|
||||
$this->assertEquals('someusername', $response->username);
|
||||
$this->assertNull($response->node_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing a password will store an encrypted version in the DB.
|
||||
*/
|
||||
public function test_update_with_password()
|
||||
{
|
||||
$finalData = (object) ['password' => 'enc-pass', 'host' => '123.456.78.9'];
|
||||
|
||||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->encrypter->shouldReceive('encrypt')->with('raw-pass')->once()->andReturn('enc-pass');
|
||||
|
||||
$this->repository->shouldReceive('update')->with(1, [
|
||||
'password' => 'enc-pass',
|
||||
'host' => '123.456.78.9',
|
||||
])->once()->andReturn($finalData);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
|
||||
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
|
||||
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
|
||||
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->update(1, ['password' => 'raw-pass', 'host' => '123.456.78.9']);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('enc-pass', $response->password);
|
||||
$this->assertEquals('123.456.78.9', $response->host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing no or empty password will skip storing it
|
||||
*/
|
||||
public function test_update_without_password()
|
||||
{
|
||||
$finalData = (object) ['host' => '123.456.78.9'];
|
||||
|
||||
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->encrypter->shouldNotReceive('encrypt');
|
||||
|
||||
$this->repository->shouldReceive('update')->with(1, ['host' => '123.456.78.9'])->once()->andReturn($finalData);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
|
||||
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
|
||||
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
|
||||
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->update(1, ['password' => '', 'host' => '123.456.78.9']);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertEquals('123.456.78.9', $response->host);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a database host can be deleted.
|
||||
*/
|
||||
public function test_delete_function()
|
||||
{
|
||||
$this->repository->shouldReceive('deleteIfNoDatabases')->with(1)->once()->andReturn(true);
|
||||
|
||||
$response = $this->service->delete(1);
|
||||
|
||||
$this->assertTrue($response, 'Assert that response is true.');
|
||||
}
|
||||
}
|
101
tests/Unit/Services/Administrative/LocationServiceTest.php
Normal file
101
tests/Unit/Services/Administrative/LocationServiceTest.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?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 Tests\TestCase;
|
||||
use Pterodactyl\Services\Administrative\LocationService;
|
||||
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
||||
|
||||
class LocationServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Administrative\LocationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->repository = m::mock(LocationRepositoryInterface::class);
|
||||
|
||||
$this->service = new LocationService($this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that creating a location returns the correct information.
|
||||
*/
|
||||
public function test_create_location()
|
||||
{
|
||||
$data = ['short' => 'shortCode', 'long' => 'longCode'];
|
||||
|
||||
$this->repository->shouldReceive('create')->with($data)->once()->andReturn((object) $data);
|
||||
|
||||
$response = $this->service->create($data);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertObjectHasAttribute('short', $response);
|
||||
$this->assertObjectHasAttribute('long', $response);
|
||||
$this->assertEquals('shortCode', $response->short);
|
||||
$this->assertEquals('longCode', $response->long);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that updating a location updates it correctly.
|
||||
*/
|
||||
public function test_update_location()
|
||||
{
|
||||
$data = ['short' => 'newShort'];
|
||||
|
||||
$this->repository->shouldReceive('update')->with(1, $data)->once()->andReturn((object) $data);
|
||||
|
||||
$response = $this->service->update(1, $data);
|
||||
|
||||
$this->assertNotNull($response);
|
||||
$this->assertObjectHasAttribute('short', $response);
|
||||
$this->assertEquals('newShort', $response->short);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a location deletion returns valid data.
|
||||
*/
|
||||
public function test_delete_location()
|
||||
{
|
||||
$this->repository->shouldReceive('deleteIfNoNodes')->with(1)->once()->andReturn(true);
|
||||
|
||||
$response = $this->service->delete(1);
|
||||
|
||||
$this->assertTrue($response);
|
||||
}
|
||||
}
|
202
tests/Unit/Services/Administrative/UserServiceTest.php
Normal file
202
tests/Unit/Services/Administrative/UserServiceTest.php
Normal file
|
@ -0,0 +1,202 @@
|
|||
<?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 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\Administrative\UserService;
|
||||
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
|
||||
class UserServiceTest 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\Administrative\UserService
|
||||
*/
|
||||
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 UserService(
|
||||
$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 test_user_creation_with_password()
|
||||
{
|
||||
$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->create([
|
||||
'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 test_user_creation_without_password()
|
||||
{
|
||||
$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->create([
|
||||
'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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing no password will not attempt any hashing.
|
||||
*/
|
||||
public function test_user_update_without_password()
|
||||
{
|
||||
$this->hasher->shouldNotReceive('make');
|
||||
$this->repository->shouldReceive('update')->with(1, ['email' => 'new@example.com'])->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->update(1, ['email' => 'new@example.com']);
|
||||
|
||||
$this->assertNull($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that passing a password will hash it before storage.
|
||||
*/
|
||||
public function test_user_update_with_password()
|
||||
{
|
||||
$this->hasher->shouldReceive('make')->with('password')->once()->andReturn('enc-password');
|
||||
$this->repository->shouldReceive('update')->with(1, ['password' => 'enc-password'])->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->update(1, ['password' => 'password']);
|
||||
|
||||
$this->assertNull($response);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue