Fix database management things to actually work correctly.

This commit is contained in:
Dane Everitt 2017-07-22 13:55:30 -05:00
parent 580e5ac569
commit 63e39fbe58
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
14 changed files with 124 additions and 119 deletions

View File

@ -44,4 +44,12 @@ interface UserRepositoryInterface extends RepositoryInterface, SearchableInterfa
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
*/ */
public function deleteIfNoServers($id); public function deleteIfNoServers($id);
/**
* Return all matching models for a user in a format that can be used for dropdowns.
*
* @param string $query
* @return \Illuminate\Database\Eloquent\Collection
*/
public function filterUsersByQuery($query);
} }

View File

@ -63,6 +63,11 @@ class ServersController extends Controller
*/ */
protected $databaseRepository; protected $databaseRepository;
/**
* @var \Pterodactyl\Services\Database\CreationService
*/
protected $databaseCreationService;
/** /**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface * @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/ */
@ -97,6 +102,7 @@ class ServersController extends Controller
AllocationRepositoryInterface $allocationRepository, AllocationRepositoryInterface $allocationRepository,
ConfigRepository $config, ConfigRepository $config,
CreationService $service, CreationService $service,
\Pterodactyl\Services\Database\CreationService $databaseCreationService,
DatabaseRepositoryInterface $databaseRepository, DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepository $databaseHostRepository, DatabaseHostRepository $databaseHostRepository,
LocationRepositoryInterface $locationRepository, LocationRepositoryInterface $locationRepository,
@ -106,6 +112,7 @@ class ServersController extends Controller
) { ) {
$this->allocationRepository = $allocationRepository; $this->allocationRepository = $allocationRepository;
$this->config = $config; $this->config = $config;
$this->databaseCreationService = $databaseCreationService;
$this->databaseRepository = $databaseRepository; $this->databaseRepository = $databaseRepository;
$this->databaseHostRepository = $databaseHostRepository; $this->databaseHostRepository = $databaseHostRepository;
$this->locationRepository = $locationRepository; $this->locationRepository = $locationRepository;
@ -583,20 +590,25 @@ class ServersController extends Controller
*/ */
public function newDatabase(Request $request, $id) public function newDatabase(Request $request, $id)
{ {
$repo = new DatabaseRepository; $this->databaseCreationService->create($id, [
'database' => $request->input('database'),
try { 'remote' => $request->input('remote'),
$repo->create($id, $request->only(['host', 'database', 'connection'])); 'database_host_id' => $request->input('database_host_id'),
]);
Alert::success('A new database was assigned to this server successfully.')->flash(); // $repo = new DatabaseRepository;
} catch (DisplayValidationException $ex) { //
return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput(); // try {
} catch (DisplayException $ex) { // $repo->create($id, $request->only(['host', 'database', 'connection']));
Alert::danger($ex->getMessage())->flash(); //
} catch (\Exception $ex) { // Alert::success('A new database was assigned to this server successfully.')->flash();
Log::error($ex); // } catch (DisplayValidationException $ex) {
Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash(); // return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
} // } catch (DisplayException $ex) {
// Alert::danger($ex->getMessage())->flash();
// } catch (\Exception $ex) {
// Log::error($ex);
// Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
// }
return redirect()->route('admin.servers.view.database', $id)->withInput(); return redirect()->route('admin.servers.view.database', $id)->withInput();
} }

View File

@ -28,7 +28,7 @@ use Illuminate\Http\Request;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Services\Administrative\UserService; use Pterodactyl\Services\UserService;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\UserFormRequest; use Pterodactyl\Http\Requests\Admin\UserFormRequest;
@ -41,7 +41,7 @@ class UserController extends Controller
protected $alert; protected $alert;
/** /**
* @var \Pterodactyl\Services\Administrative\UserService * @var \Pterodactyl\Services\UserService
*/ */
protected $service; protected $service;
@ -59,7 +59,7 @@ class UserController extends Controller
* UserController constructor. * UserController constructor.
* *
* @param \Prologue\Alerts\AlertsMessageBag $alert * @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Services\Administrative\UserService $service * @param \Pterodactyl\Services\UserService $service
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
* @param \Pterodactyl\Models\User $model * @param \Pterodactyl\Models\User $model
*/ */
@ -178,17 +178,11 @@ class UserController extends Controller
/** /**
* Get a JSON response of users on the system. * Get a JSON response of users on the system.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Pterodactyl\Models\User * @return \Illuminate\Database\Eloquent\Collection
*/ */
public function json(Request $request) public function json(Request $request)
{ {
return $this->model->search($request->input('q'))->all([ return $this->repository->filterUsersByQuery($request->input('q'));
'id', 'email', 'username', 'name_first', 'name_last',
])->transform(function ($item) {
$item->md5 = md5(strtolower($item->email));
return $item;
});
} }
} }

View File

@ -33,17 +33,10 @@ class DatabaseHostFormRequest extends AdminFormRequest
*/ */
public function rules() public function rules()
{ {
$this->merge([ if ($this->method() !== 'POST') {
'node_id' => ($this->input('node_id') < 1) ? null : $this->input('node_id'), return DatabaseHost::getUpdateRulesForId($this->route()->parameter('host')->id);
'host' => gethostbyname($this->input('host')),
]);
$rules = app()->make(DatabaseHost::class)->getRules();
if ($this->method() === 'PATCH') {
$rules['host'] = $rules['host'] . ',' . $this->route()->parameter('host')->id;
} }
return $rules; return DatabaseHost::getCreateRules();
} }
} }

View File

@ -24,11 +24,12 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable; use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class Database extends Model class Database extends Model implements ValidableContract
{ {
use Eloquence, Validable; use Eloquence, Validable;
@ -52,7 +53,7 @@ class Database extends Model
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'server_id', 'database_host_id', 'database', 'username', 'remote', 'server_id', 'database_host_id', 'database', 'username', 'password', 'remote',
]; ];
/** /**

View File

@ -27,8 +27,9 @@ namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable; use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class DatabaseHost extends Model class DatabaseHost extends Model implements ValidableContract
{ {
use Eloquence, Validable; use Eloquence, Validable;
@ -52,7 +53,7 @@ class DatabaseHost extends Model
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'name', 'host', 'port', 'username', 'max_databases', 'node_id', 'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id',
]; ];
/** /**
@ -79,11 +80,12 @@ class DatabaseHost extends Model
'node_id' => 'sometimes|required', 'node_id' => 'sometimes|required',
]; ];
/** /**
* Validation rules to assign to this model. * Validation rules to assign to this model.
* *
* @var array * @var array
*/ */
// @todo the node_id field doesn't validate correctly if no node is provided in request
protected static $dataIntegrityRules = [ protected static $dataIntegrityRules = [
'name' => 'string|max:255', 'name' => 'string|max:255',
'host' => 'ip|unique:database_hosts,host', 'host' => 'ip|unique:database_hosts,host',

View File

@ -24,32 +24,32 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Models\Database; use Pterodactyl\Models\Database;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Database\ConnectionResolver;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
{ {
/** /**
* @var \Illuminate\Database\ConnectionResolverInterface * @var \Illuminate\Database\DatabaseManager
*/ */
protected $connection; protected $database;
/** /**
* DatabaseRepository constructor. * DatabaseRepository constructor.
* *
* @param \Illuminate\Foundation\Application $application * @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\ConnectionResolver $connection * @param \Illuminate\Database\DatabaseManager $database
*/ */
public function __construct( public function __construct(
Application $application, Application $application,
ConnectionResolver $connection DatabaseManager $database
) { ) {
parent::__construct($application); parent::__construct($application);
$this->connection = $connection; $this->database = $database;
} }
/** /**
@ -150,6 +150,6 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
*/ */
protected function runStatement($statement, $connection = null) protected function runStatement($statement, $connection = null)
{ {
return $this->connection->connection($connection)->statement($statement); return $this->database->connection($connection)->statement($statement);
} }
} }

View File

@ -93,4 +93,22 @@ class UserRepository extends SearchableRepository implements UserRepositoryInter
return $user->delete(); return $user->delete();
} }
/**
* {@inheritdoc}
*/
public function filterUsersByQuery($query)
{
$this->withColumns([
'id', 'email', 'username', 'name_first', 'name_last',
]);
$instance = $this->getBuilder()->search($query)->get($this->getColumns());
return $instance->transform(function ($item) {
$item->md5 = md5(strtolower($item->email));
return $item;
});
}
} }

View File

@ -24,8 +24,7 @@
namespace Pterodactyl\Services\Database; namespace Pterodactyl\Services\Database;
use Illuminate\Database\ConnectionResolver; use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -33,12 +32,7 @@ use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class CreationService class CreationService
{ {
/** /**
* @var \Illuminate\Database\ConnectionResolver * @var \Illuminate\Database\DatabaseManager
*/
protected $connection;
/**
* @var \Illuminate\Database\ConnectionInterface
*/ */
protected $database; protected $database;
@ -60,20 +54,17 @@ class CreationService
/** /**
* CreationService constructor. * CreationService constructor.
* *
* @param \Illuminate\Database\ConnectionInterface $database * @param \Illuminate\Database\DatabaseManager $database
* @param \Illuminate\Database\ConnectionResolver $connection
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic * @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/ */
public function __construct( public function __construct(
ConnectionInterface $database, DatabaseManager $database,
ConnectionResolver $connection,
DynamicDatabaseConnection $dynamic, DynamicDatabaseConnection $dynamic,
DatabaseRepositoryInterface $repository, DatabaseRepositoryInterface $repository,
Encrypter $encrypter Encrypter $encrypter
) { ) {
$this->connection = $connection;
$this->database = $database; $this->database = $database;
$this->dynamic = $dynamic; $this->dynamic = $dynamic;
$this->encrypter = $encrypter; $this->encrypter = $encrypter;
@ -83,6 +74,7 @@ class CreationService
/** /**
* Create a new database that is linked to a specific host. * Create a new database that is linked to a specific host.
* *
* @param int $server
* @param array $data * @param array $data
* @return \Illuminate\Database\Eloquent\Model * @return \Illuminate\Database\Eloquent\Model
* *
@ -90,10 +82,11 @@ class CreationService
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function create(array $data) public function create($server, array $data)
{ {
$data['database'] = sprintf('d%d_%s', $data['server_id'], $data['database']); $data['server_id'] = $server;
$data['username'] = sprintf('u%d_%s', $data['server_id'], str_random(10)); $data['database'] = sprintf('d%d_%s', $server, $data['database']);
$data['username'] = sprintf('u%d_%s', $server, str_random(10));
$data['password'] = $this->encrypter->encrypt(str_random(16)); $data['password'] = $this->encrypter->encrypt(str_random(16));
$this->database->beginTransaction(); $this->database->beginTransaction();

View File

@ -24,8 +24,8 @@
namespace Pterodactyl\Services\Database; namespace Pterodactyl\Services\Database;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -54,14 +54,14 @@ class DatabaseHostService
/** /**
* DatabaseHostService constructor. * DatabaseHostService constructor.
* *
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Illuminate\Database\DatabaseManager $database * @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic * @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/ */
public function __construct( public function __construct(
DatabaseHostRepositoryInterface $repository,
DatabaseManager $database, DatabaseManager $database,
DatabaseHostRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic, DynamicDatabaseConnection $dynamic,
Encrypter $encrypter Encrypter $encrypter
) { ) {
@ -74,7 +74,7 @@ class DatabaseHostService
/** /**
* Create a new database host and persist it to the database. * Create a new database host and persist it to the database.
* *
* @param array $data * @param array $data
* @return \Pterodactyl\Models\DatabaseHost * @return \Pterodactyl\Models\DatabaseHost
* *
* @throws \Throwable * @throws \Throwable
@ -106,11 +106,11 @@ class DatabaseHostService
/** /**
* Update a database host and persist to the database. * Update a database host and persist to the database.
* *
* @param int $id * @param int $id
* @param array $data * @param array $data
* @return mixed * @return mixed
* *
* @throws \PDOException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function update($id, array $data) public function update($id, array $data)
{ {
@ -135,7 +135,7 @@ class DatabaseHostService
/** /**
* Delete a database host if it has no active databases attached to it. * Delete a database host if it has no active databases attached to it.
* *
* @param int $id * @param int $id
* @return bool|null * @return bool|null
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException

View File

@ -117,7 +117,7 @@
<div class="form-group"> <div class="form-group">
<label for="pNodeId" class="form-label">Linked Node</label> <label for="pNodeId" class="form-label">Linked Node</label>
<select name="node_id" id="pNodeId" class="form-control"> <select name="node_id" id="pNodeId" class="form-control">
<option value="0">None</option> <option value="null">None</option>
@foreach($locations as $location) @foreach($locations as $location)
<optgroup label="{{ $location->short }}"> <optgroup label="{{ $location->short }}">
@foreach($location->nodes as $node) @foreach($location->nodes as $node)

View File

@ -91,8 +91,8 @@
<form action="{{ route('admin.servers.view.database', $server->id) }}" method="POST"> <form action="{{ route('admin.servers.view.database', $server->id) }}" method="POST">
<div class="box-body"> <div class="box-body">
<div class="form-group"> <div class="form-group">
<label for="pDatabaseHost" class="control-label">Database Host</label> <label for="pDatabaseHostId" class="control-label">Database Host</label>
<select id="pDatabaseHost" name="host" class="form-control"> <select id="pDatabaseHostId" name="database_host_id" class="form-control">
@foreach($hosts as $host) @foreach($hosts as $host)
<option value="{{ $host->id }}">{{ $host->name }}</option> <option value="{{ $host->id }}">{{ $host->name }}</option>
@endforeach @endforeach
@ -107,8 +107,8 @@
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="pConnections" class="control-label">Connections</label> <label for="pRemote" class="control-label">Connections</label>
<input id="pConnections" type="text" name="connection" class="form-control" placeholder="%" value="%" /> <input id="pRemote" type="text" name="remote" class="form-control" value="%" />
<p class="text-muted small">This should reflect the IP address that connections are allowed from. Uses standard MySQL notation. If unsure leave as <code>%</code>.</p> <p class="text-muted small">This should reflect the IP address that connections are allowed from. Uses standard MySQL notation. If unsure leave as <code>%</code>.</p>
</div> </div>
</div> </div>

View File

@ -25,13 +25,14 @@
namespace Tests\Unit\Services\Database; namespace Tests\Unit\Services\Database;
use Exception; use Exception;
use Illuminate\Database\DatabaseManager;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use phpmock\phpunit\PHPMock; use phpmock\phpunit\PHPMock;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Services\Database\CreationService; use Pterodactyl\Services\Database\CreationService;
use Illuminate\Database\ConnectionResolver;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -49,12 +50,7 @@ class CreationServiceTest extends TestCase
]; ];
/** /**
* @var \Illuminate\Database\ConnectionResolver * @var \Illuminate\Database\DatabaseManager
*/
protected $connection;
/**
* @var \Illuminate\Database\ConnectionInterface
*/ */
protected $database; protected $database;
@ -85,8 +81,7 @@ class CreationServiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
$this->connection = m::mock(ConnectionResolver::class); $this->database = m::mock(DatabaseManager::class);
$this->database = m::mock(ConnectionInterface::class);
$this->dynamic = m::mock(DynamicDatabaseConnection::class); $this->dynamic = m::mock(DynamicDatabaseConnection::class);
$this->encrypter = m::mock(Encrypter::class); $this->encrypter = m::mock(Encrypter::class);
$this->repository = m::mock(DatabaseRepositoryInterface::class); $this->repository = m::mock(DatabaseRepositoryInterface::class);
@ -96,7 +91,6 @@ class CreationServiceTest extends TestCase
$this->service = new CreationService( $this->service = new CreationService(
$this->database, $this->database,
$this->connection,
$this->dynamic, $this->dynamic,
$this->repository, $this->repository,
$this->encrypter $this->encrypter
@ -110,10 +104,8 @@ class CreationServiceTest extends TestCase
{ {
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password'); $this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with(
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull(); $this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with( $this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic' self::TEST_DATA['database'], 'dynamic'
@ -131,16 +123,15 @@ class CreationServiceTest extends TestCase
$this->repository->shouldReceive('flush')->with('dynamic')->once()->andReturnNull(); $this->repository->shouldReceive('flush')->with('dynamic')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->create([ $response = $this->service->create(1, [
'server_id' => 1,
'database' => 'dbname', 'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3, 'database_host_id' => 3,
]); ]);
$this->assertNotEmpty($response); $this->assertNotEmpty($response);
$this->assertTrue(is_object($response), 'Assert that response is an object.'); $this->assertTrue(is_object($response), 'Assert that response is an object.');
$this->assertEquals(self::TEST_DATA['server_id'], $response->server_id);
$this->assertEquals(self::TEST_DATA['database'], $response->database); $this->assertEquals(self::TEST_DATA['database'], $response->database);
$this->assertEquals(self::TEST_DATA['remote'], $response->remote); $this->assertEquals(self::TEST_DATA['remote'], $response->remote);
$this->assertEquals(self::TEST_DATA['username'], $response->username); $this->assertEquals(self::TEST_DATA['username'], $response->username);
@ -157,16 +148,13 @@ class CreationServiceTest extends TestCase
{ {
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password'); $this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with( $this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andThrow(new Exception('Test Message'));
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andThrow(new Exception('Test Message'));
$this->repository->shouldNotReceive('dropDatabase'); $this->repository->shouldNotReceive('dropDatabase');
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->service->create([ $this->service->create(1, [
'server_id' => 1,
'database' => 'dbname', 'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3, 'database_host_id' => 3,
]); ]);
} }
@ -180,10 +168,7 @@ class CreationServiceTest extends TestCase
{ {
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password'); $this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with( $this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull(); $this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with( $this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic' self::TEST_DATA['database'], 'dynamic'
@ -197,9 +182,9 @@ class CreationServiceTest extends TestCase
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->service->create([ $this->service->create(1, [
'server_id' => 1,
'database' => 'dbname', 'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3, 'database_host_id' => 3,
]); ]);
} }
@ -211,10 +196,7 @@ class CreationServiceTest extends TestCase
{ {
$this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password'); $this->encrypter->shouldReceive('encrypt')->with('str_random')->once()->andReturn('enc_password');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('createIfNotExists')->with( $this->repository->shouldReceive('createIfNotExists')->with(self::TEST_DATA)->once()->andReturn((object) self::TEST_DATA);
collect(self::TEST_DATA)->except('remote')->toArray()
)->once()->andReturn((object) self::TEST_DATA);
$this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull(); $this->dynamic->shouldReceive('set')->with('dynamic', self::TEST_DATA['database_host_id'])->once()->andReturnNull();
$this->repository->shouldReceive('createDatabase')->with( $this->repository->shouldReceive('createDatabase')->with(
self::TEST_DATA['database'], 'dynamic' self::TEST_DATA['database'], 'dynamic'
@ -226,9 +208,9 @@ class CreationServiceTest extends TestCase
$this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try { try {
$this->service->create([ $this->service->create(1, [
'server_id' => 1,
'database' => 'dbname', 'database' => 'dbname',
'remote' => '%',
'database_host_id' => 3, 'database_host_id' => 3,
]); ]);
} catch (Exception $ex) { } catch (Exception $ex) {

View File

@ -24,9 +24,11 @@
namespace Tests\Unit\Services\Administrative; namespace Tests\Unit\Services\Administrative;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\DatabaseManager;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -72,8 +74,8 @@ class DatabaseHostServiceTest extends TestCase
$this->repository = m::mock(DatabaseHostRepositoryInterface::class); $this->repository = m::mock(DatabaseHostRepositoryInterface::class);
$this->service = new DatabaseHostService( $this->service = new DatabaseHostService(
$this->repository,
$this->database, $this->database,
$this->repository,
$this->dynamic, $this->dynamic,
$this->encrypter $this->encrypter
); );