Fix tests
This commit is contained in:
parent
d50ea18598
commit
532025a348
10 changed files with 463 additions and 573 deletions
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Databases;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Database;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Services\Databases\DatabasePasswordService;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
|
||||
class DatabasePasswordServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection|\Mockery\Mock
|
||||
*/
|
||||
private $dynamic;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
||||
*/
|
||||
private $encrypter;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
|
||||
$this->encrypter = m::mock(Encrypter::class);
|
||||
$this->repository = m::mock(DatabaseRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a password can be updated.
|
||||
*
|
||||
* @dataProvider useModelDataProvider
|
||||
*/
|
||||
public function testPasswordIsChanged($useModel)
|
||||
{
|
||||
$model = factory(Database::class)->make();
|
||||
|
||||
if (! $useModel) {
|
||||
$this->repository->shouldReceive('find')->with(1234)->once()->andReturn($model);
|
||||
}
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $model->database_host_id)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->encrypter->shouldReceive('encrypt')->with('test123')->once()->andReturn('enc123');
|
||||
|
||||
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('update')->with($model->id, ['password' => 'enc123'])->once()->andReturn(true);
|
||||
|
||||
$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, 'test123')->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->handle($useModel ? $model : 1234, 'test123');
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertTrue($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider to determine if a model should be passed or an int.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function useModelDataProvider(): array
|
||||
{
|
||||
return [[false], [true]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Databases\DatabasePasswordService
|
||||
*/
|
||||
private function getService(): DatabasePasswordService
|
||||
{
|
||||
return new DatabasePasswordService($this->connection, $this->repository, $this->dynamic, $this->encrypter);
|
||||
}
|
||||
}
|
101
tests/Unit/Services/Databases/Hosts/HostCreationServiceTest.php
Normal file
101
tests/Unit/Services/Databases/Hosts/HostCreationServiceTest.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Databases\Hosts;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
class HostCreationServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\DatabaseManager|\Mockery\Mock
|
||||
*/
|
||||
private $databaseManager;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection|\Mockery\Mock
|
||||
*/
|
||||
private $dynamic;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
||||
*/
|
||||
private $encrypter;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->databaseManager = m::mock(DatabaseManager::class);
|
||||
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
|
||||
$this->encrypter = m::mock(Encrypter::class);
|
||||
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a database host can be created.
|
||||
*/
|
||||
public function testDatabaseHostIsCreated()
|
||||
{
|
||||
$model = factory(DatabaseHost::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->encrypter->shouldReceive('encrypt')->with('test123')->once()->andReturn('enc123');
|
||||
$this->repository->shouldReceive('create')->with(m::subset([
|
||||
'password' => 'enc123',
|
||||
'username' => $model->username,
|
||||
'node_id' => $model->node_id,
|
||||
]))->once()->andReturn($model);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $model)->once()->andReturnNull();
|
||||
$this->databaseManager->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf();
|
||||
$this->databaseManager->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->handle([
|
||||
'password' => 'test123',
|
||||
'username' => $model->username,
|
||||
'node_id' => $model->node_id,
|
||||
]);
|
||||
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertSame($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Databases\Hosts\HostCreationService
|
||||
*/
|
||||
private function getService(): HostCreationService
|
||||
{
|
||||
return new HostCreationService(
|
||||
$this->connection,
|
||||
$this->databaseManager,
|
||||
$this->repository,
|
||||
$this->dynamic,
|
||||
$this->encrypter
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Databases\Hosts;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Exceptions\Service\HasActiveServersException;
|
||||
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
class HostDeletionServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $databaseRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
||||
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a host can be deleted.
|
||||
*/
|
||||
public function testHostIsDeleted()
|
||||
{
|
||||
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1234]])->once()->andReturn(0);
|
||||
$this->repository->shouldReceive('delete')->with(1234)->once()->andReturn(1);
|
||||
|
||||
$response = $this->getService()->handle(1234);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertSame(1, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if a host with databases is deleted.
|
||||
*
|
||||
* @dataProvider databaseCountDataProvider
|
||||
*/
|
||||
public function testExceptionIsThrownIfDeletingHostWithDatabases($count)
|
||||
{
|
||||
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1234]])->once()->andReturn($count);
|
||||
|
||||
try {
|
||||
$this->getService()->handle(1234);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(HasActiveServersException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.databases.delete_has_databases'), $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider to ensure exceptions are thrown for any value > 0.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function databaseCountDataProvider(): array
|
||||
{
|
||||
return [[1], [2], [10]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Databases\Hosts\HostDeletionService
|
||||
*/
|
||||
private function getService(): HostDeletionService
|
||||
{
|
||||
return new HostDeletionService($this->databaseRepository, $this->repository);
|
||||
}
|
||||
}
|
112
tests/Unit/Services/Databases/Hosts/HostUpdateServiceTest.php
Normal file
112
tests/Unit/Services/Databases/Hosts/HostUpdateServiceTest.php
Normal file
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Databases\Hosts;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\DatabaseHost;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Pterodactyl\Extensions\DynamicDatabaseConnection;
|
||||
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
class HostUpdateServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\DatabaseManager|\Mockery\Mock
|
||||
*/
|
||||
private $databaseManager;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection|\Mockery\Mock
|
||||
*/
|
||||
private $dynamic;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
||||
*/
|
||||
private $encrypter;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->databaseManager = m::mock(DatabaseManager::class);
|
||||
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
|
||||
$this->encrypter = m::mock(Encrypter::class);
|
||||
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a password is encrypted before storage if provided.
|
||||
*/
|
||||
public function testPasswordIsEncryptedWhenProvided()
|
||||
{
|
||||
$model = factory(DatabaseHost::class)->make();
|
||||
|
||||
$this->encrypter->shouldReceive('encrypt')->with('test123')->once()->andReturn('enc123');
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('update')->with(1234, ['password' => 'enc123'])->once()->andReturn($model);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $model)->once()->andReturnNull();
|
||||
$this->databaseManager->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf();
|
||||
$this->databaseManager->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->handle(1234, ['password' => 'test123']);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertSame($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that updates still occur when no password is provided.
|
||||
*/
|
||||
public function testUpdateOccursWhenNoPasswordIsProvided()
|
||||
{
|
||||
$model = factory(DatabaseHost::class)->make();
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->repository->shouldReceive('update')->with(1234, ['username' => 'test'])->once()->andReturn($model);
|
||||
|
||||
$this->dynamic->shouldReceive('set')->with('dynamic', $model)->once()->andReturnNull();
|
||||
$this->databaseManager->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf();
|
||||
$this->databaseManager->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->handle(1234, ['password' => '', 'username' => 'test']);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertSame($model, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Databases\Hosts\HostUpdateService
|
||||
*/
|
||||
private function getService(): HostUpdateService
|
||||
{
|
||||
return new HostUpdateService(
|
||||
$this->connection,
|
||||
$this->databaseManager,
|
||||
$this->repository,
|
||||
$this->dynamic,
|
||||
$this->encrypter
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue