Skip to content

Commit

Permalink
Get database unit tests back into passing shape
Browse files Browse the repository at this point in the history
  • Loading branch information
DaneEveritt committed Jun 25, 2020
1 parent 756a21f commit a5d9faf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ coverage.xml
resources/lang/locales.js
resources/assets/pterodactyl/scripts/helpers/ziggy.js
resources/assets/scripts/helpers/ziggy.js
.phpunit.result.cache
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

17 changes: 9 additions & 8 deletions tests/Unit/Services/Databases/DatabasePasswordServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ public function setUp(): void
*/
public function testPasswordIsChanged()
{
$model = factory(Database::class)->make();
/** @var \Pterodactyl\Models\Database $model */
$model = factory(Database::class)->make(['max_connections' => 0]);

$this->connection->expects('transaction')->with(m::on(function ($closure) {
return is_null($closure());
}));

$this->dynamic->shouldReceive('set')->with('dynamic', $model->database_host_id)->once()->andReturnNull();
$this->dynamic->expects('set')->with('dynamic', $model->database_host_id)->andReturnNull();

$this->encrypter->expects('encrypt')->with(m::on(function ($string) {
preg_match_all('/[!@+=.^-]/', $string, $matches, PREG_SET_ORDER);
Expand All @@ -67,13 +68,13 @@ public function testPasswordIsChanged()
return true;
}))->andReturn('enc123');

$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->repository->shouldReceive('update')->with($model->id, ['password' => 'enc123'])->once()->andReturn(true);
$this->repository->expects('withoutFreshModel')->withNoArgs()->andReturnSelf();
$this->repository->expects('update')->with($model->id, ['password' => 'enc123'])->andReturn(true);

$this->repository->shouldReceive('dropUser')->with($model->username, $model->remote)->once()->andReturn(true);
$this->repository->shouldReceive('createUser')->with($model->username, $model->remote, m::any())->once()->andReturn(true);
$this->repository->shouldReceive('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->once()->andReturn(true);
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturn(true);
$this->repository->expects('dropUser')->with($model->username, $model->remote)->andReturn(true);
$this->repository->expects('createUser')->with($model->username, $model->remote, m::any(), 0)->andReturn(true);
$this->repository->expects('assignUserToDatabase')->with($model->database, $model->username, $model->remote)->andReturn(true);
$this->repository->expects('flush')->withNoArgs()->andReturn(true);

$response = $this->getService()->handle($model);
$this->assertNotEmpty($response);
Expand Down
74 changes: 10 additions & 64 deletions tests/Unit/Services/Databases/DeployServerDatabaseServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException;

class DeployServerDatabaseServiceTest extends TestCase
{
Expand Down Expand Up @@ -51,24 +52,17 @@ public function setUp(): void
*/
public function testNonRandomFoundHost($limit, $count)
{
config()->set('pterodactyl.client_features.databases.allow_random', false);

$server = factory(Server::class)->make(['database_limit' => $limit]);
$model = factory(Database::class)->make();

$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn($count);

$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect([$model]));

$this->managementService->shouldReceive('create')
->once()
->with($server->id, [
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
Expand All @@ -83,25 +77,20 @@ public function testNonRandomFoundHost($limit, $count)

/**
* Test that an exception is thrown if in non-random mode and no host is found.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
*/
public function testNonRandomNoHost()
{
config()->set('pterodactyl.client_features.databases.allow_random', false);
$this->expectException(NoSuitableDatabaseHostException::class);

$server = factory(Server::class)->make(['database_limit' => 1]);

$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);

$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());

$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());

$this->getService()->handle($server, []);
}

Expand All @@ -113,11 +102,6 @@ public function testRandomFoundHost()
$server = factory(Server::class)->make(['database_limit' => 1]);
$model = factory(Database::class)->make();

$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);

$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
->with([['node_id', '=', $server->node_id]])
Expand All @@ -129,7 +113,7 @@ public function testRandomFoundHost()

$this->managementService->shouldReceive('create')
->once()
->with($server->id, [
->with($server, [
'database_host_id' => $model->id,
'database' => 'testdb',
'remote' => null,
Expand All @@ -144,60 +128,22 @@ public function testRandomFoundHost()

/**
* Test that an exception is thrown when no host is found and random is allowed.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
*/
public function testRandomNoHost()
{
$server = factory(Server::class)->make(['database_limit' => 1]);
$this->expectException(NoSuitableDatabaseHostException::class);

$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn(0);
$server = factory(Server::class)->make(['database_limit' => 1]);

$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
->once()
$this->databaseHostRepository->expects('setColumns->findWhere')
->with([['node_id', '=', $server->node_id]])
->andReturn(collect());

$this->databaseHostRepository->shouldReceive('setColumns->all')
->once()
->andReturn(collect());

$this->getService()->handle($server, []);
}

/**
* Test that a server over the database limit throws an exception.
*
* @dataProvider databaseExceedingLimitDataProvider
* @expectedException \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
*/
public function testServerOverDatabaseLimit($limit, $count)
{
$server = factory(Server::class)->make(['database_limit' => $limit]);

$this->repository->shouldReceive('findCountWhere')
->once()
->with([['server_id', '=', $server->id]])
->andReturn($count);
$this->databaseHostRepository->expects('setColumns->all')->withNoArgs()->andReturn(collect());

$this->getService()->handle($server, []);
}

/**
* Test that an exception is thrown if the feature is not enabled.
*
* @expectedException \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
*/
public function testFeatureNotEnabled()
{
config()->set('pterodactyl.client_features.databases.enabled', false);

$this->getService()->handle(factory(Server::class)->make(), []);
}

/**
* Provide limits and current database counts for testing.
*
Expand Down

0 comments on commit a5d9faf

Please sign in to comment.