Skip to content

Commit e39353a

Browse files
committed
Add tests for new service
1 parent bcb6960 commit e39353a

File tree

3 files changed

+256
-20
lines changed

3 files changed

+256
-20
lines changed

app/Services/Databases/DeployServerDatabaseService.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DeployServerDatabaseService
2121
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
2222
*/
2323
private $managementService;
24+
2425
/**
2526
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
2627
*/
@@ -63,21 +64,23 @@ public function handle(Server $server, array $data): Database
6364
}
6465

6566
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
66-
$host = $this->databaseHostRepository->setColumns(['id'])->findWhere([
67+
$hosts = $this->databaseHostRepository->setColumns(['id'])->findWhere([
6768
['node_id', '=', $server->node_id],
68-
])->random();
69+
]);
6970

70-
if (empty($host) && ! $allowRandom) {
71+
if ($hosts->isEmpty() && ! $allowRandom) {
7172
throw new NoSuitableDatabaseHostException;
7273
}
7374

74-
if (empty($host)) {
75-
$host = $this->databaseHostRepository->setColumns(['id'])->all()->random();
76-
if (empty($host)) {
75+
if ($hosts->isEmpty()) {
76+
$hosts = $this->databaseHostRepository->setColumns(['id'])->all();
77+
if ($hosts->isEmpty()) {
7778
throw new NoSuitableDatabaseHostException;
7879
}
7980
}
8081

82+
$host = $hosts->random();
83+
8184
return $this->managementService->create($server->id, [
8285
'database_host_id' => $host->id,
8386
'database' => array_get($data, 'database'),

tests/Unit/Http/Controllers/Server/Files/DownloadControllerTest.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Tests\Unit\Http\Controllers\Server\Files;
114

125
use Mockery as m;
13-
use phpmock\phpunit\PHPMock;
146
use Pterodactyl\Models\Node;
7+
use Tests\Traits\MocksUuids;
158
use Pterodactyl\Models\Server;
169
use Illuminate\Cache\Repository;
1710
use Tests\Unit\Http\Controllers\ControllerTestCase;
1811
use Pterodactyl\Http\Controllers\Server\Files\DownloadController;
1912

2013
class DownloadControllerTest extends ControllerTestCase
2114
{
22-
use PHPMock;
15+
use MocksUuids;
2316

2417
/**
2518
* @var \Illuminate\Cache\Repository|\Mockery\Mock
@@ -48,16 +41,20 @@ public function testIndexController()
4841
$this->setRequestAttribute('server', $server);
4942

5043
$controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
51-
$this->getFunctionMock('\\Pterodactyl\\Http\\Controllers\\Server\\Files', 'str_random')
52-
->expects($this->once())->willReturn('randomString');
5344

54-
$this->cache->shouldReceive('tags')->with(['Server:Downloads'])->once()->andReturnSelf();
55-
$this->cache->shouldReceive('put')->with('randomString', ['server' => $server->uuid, 'path' => '/my/file.txt'], 5)->once()->andReturnNull();
45+
$this->cache->shouldReceive('put')
46+
->once()
47+
->with('Server:Downloads:' . $this->getKnownUuid(), ['server' => $server->uuid, 'path' => '/my/file.txt'], 5)
48+
->andReturnNull();
5649

5750
$response = $controller->index($this->request, $server->uuidShort, '/my/file.txt');
5851
$this->assertIsRedirectResponse($response);
5952
$this->assertRedirectUrlEquals(sprintf(
60-
'%s://%s:%s/v1/server/file/download/%s', $server->node->scheme, $server->node->fqdn, $server->node->daemonListen, 'randomString'
53+
'%s://%s:%s/v1/server/file/download/%s',
54+
$server->node->scheme,
55+
$server->node->fqdn,
56+
$server->node->daemonListen,
57+
$this->getKnownUuid()
6158
), $response);
6259
}
6360

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
3+
namespace Tests\Unit\Services\Databases;
4+
5+
use Mockery as m;
6+
use Tests\TestCase;
7+
use Pterodactyl\Models\Server;
8+
use Pterodactyl\Models\Database;
9+
use Pterodactyl\Services\Databases\DatabaseManagementService;
10+
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
11+
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
12+
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
13+
14+
class DeployServerDatabaseServiceTest extends TestCase
15+
{
16+
/**
17+
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
18+
*/
19+
private $databaseHostRepository;
20+
21+
/**
22+
* @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
23+
*/
24+
private $managementService;
25+
26+
/**
27+
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
28+
*/
29+
private $repository;
30+
31+
/**
32+
* Setup tests.
33+
*/
34+
public function setUp()
35+
{
36+
parent::setUp();
37+
38+
$this->databaseHostRepository = m::mock(DatabaseHostRepositoryInterface::class);
39+
$this->managementService = m::mock(DatabaseManagementService::class);
40+
$this->repository = m::mock(DatabaseRepositoryInterface::class);
41+
42+
// Set configs for testing instances.
43+
config()->set('pterodactyl.client_features.databases.enabled', true);
44+
config()->set('pterodactyl.client_features.databases.allow_random', true);
45+
}
46+
47+
/**
48+
* Test handling of non-random hosts when a host is found.
49+
*
50+
* @dataProvider databaseLimitDataProvider
51+
*/
52+
public function testNonRandomFoundHost($limit, $count)
53+
{
54+
config()->set('pterodactyl.client_features.databases.allow_random', false);
55+
56+
$server = factory(Server::class)->make(['database_limit' => $limit]);
57+
$model = factory(Database::class)->make();
58+
59+
$this->repository->shouldReceive('findCountWhere')
60+
->once()
61+
->with([['server_id', '=', $server->id]])
62+
->andReturn($count);
63+
64+
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
65+
->once()
66+
->with([['node_id', '=', $server->node_id]])
67+
->andReturn(collect([$model]));
68+
69+
$this->managementService->shouldReceive('create')
70+
->once()
71+
->with($server->id, [
72+
'database_host_id' => $model->id,
73+
'database' => 'testdb',
74+
'remote' => null,
75+
])
76+
->andReturn($model);
77+
78+
$response = $this->getService()->handle($server, ['database' => 'testdb']);
79+
80+
$this->assertInstanceOf(Database::class, $response);
81+
$this->assertSame($model, $response);
82+
}
83+
84+
/**
85+
* Test that an exception is thrown if in non-random mode and no host is found.
86+
*
87+
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
88+
*/
89+
public function testNonRandomNoHost()
90+
{
91+
config()->set('pterodactyl.client_features.databases.allow_random', false);
92+
93+
$server = factory(Server::class)->make(['database_limit' => 1]);
94+
95+
$this->repository->shouldReceive('findCountWhere')
96+
->once()
97+
->with([['server_id', '=', $server->id]])
98+
->andReturn(0);
99+
100+
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
101+
->once()
102+
->with([['node_id', '=', $server->node_id]])
103+
->andReturn(collect());
104+
105+
$this->getService()->handle($server, []);
106+
}
107+
108+
/**
109+
* Test handling of random host selection.
110+
*/
111+
public function testRandomFoundHost()
112+
{
113+
$server = factory(Server::class)->make(['database_limit' => 1]);
114+
$model = factory(Database::class)->make();
115+
116+
$this->repository->shouldReceive('findCountWhere')
117+
->once()
118+
->with([['server_id', '=', $server->id]])
119+
->andReturn(0);
120+
121+
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
122+
->once()
123+
->with([['node_id', '=', $server->node_id]])
124+
->andReturn(collect());
125+
126+
$this->databaseHostRepository->shouldReceive('setColumns->all')
127+
->once()
128+
->andReturn(collect([$model]));
129+
130+
$this->managementService->shouldReceive('create')
131+
->once()
132+
->with($server->id, [
133+
'database_host_id' => $model->id,
134+
'database' => 'testdb',
135+
'remote' => null,
136+
])
137+
->andReturn($model);
138+
139+
$response = $this->getService()->handle($server, ['database' => 'testdb']);
140+
141+
$this->assertInstanceOf(Database::class, $response);
142+
$this->assertSame($model, $response);
143+
}
144+
145+
/**
146+
* Test that an exception is thrown when no host is found and random is allowed.
147+
*
148+
* @expectedException \Pterodactyl\Exceptions\Service\Database\NoSuitableDatabaseHostException
149+
*/
150+
public function testRandomNoHost()
151+
{
152+
$server = factory(Server::class)->make(['database_limit' => 1]);
153+
154+
$this->repository->shouldReceive('findCountWhere')
155+
->once()
156+
->with([['server_id', '=', $server->id]])
157+
->andReturn(0);
158+
159+
$this->databaseHostRepository->shouldReceive('setColumns->findWhere')
160+
->once()
161+
->with([['node_id', '=', $server->node_id]])
162+
->andReturn(collect());
163+
164+
$this->databaseHostRepository->shouldReceive('setColumns->all')
165+
->once()
166+
->andReturn(collect());
167+
168+
$this->getService()->handle($server, []);
169+
}
170+
171+
/**
172+
* Test that a server over the database limit throws an exception.
173+
*
174+
* @dataProvider databaseExceedingLimitDataProvider
175+
* @expectedException \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
176+
*/
177+
public function testServerOverDatabaseLimit($limit, $count)
178+
{
179+
$server = factory(Server::class)->make(['database_limit' => $limit]);
180+
181+
$this->repository->shouldReceive('findCountWhere')
182+
->once()
183+
->with([['server_id', '=', $server->id]])
184+
->andReturn($count);
185+
186+
$this->getService()->handle($server, []);
187+
}
188+
189+
/**
190+
* Test that an exception is thrown if the feature is not enabled.
191+
*
192+
* @expectedException \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
193+
*/
194+
public function testFeatureNotEnabled()
195+
{
196+
config()->set('pterodactyl.client_features.databases.enabled', false);
197+
198+
$this->getService()->handle(factory(Server::class)->make(), []);
199+
}
200+
201+
/**
202+
* Provide limits and current database counts for testing.
203+
*
204+
* @return array
205+
*/
206+
public function databaseLimitDataProvider(): array
207+
{
208+
return [
209+
[null, 10],
210+
[1, 0],
211+
];
212+
}
213+
214+
/**
215+
* Provide data for servers over their database limit.
216+
*
217+
* @return array
218+
*/
219+
public function databaseExceedingLimitDataProvider(): array
220+
{
221+
return [
222+
[2, 2],
223+
[2, 3],
224+
];
225+
}
226+
227+
/**
228+
* Return an instance of the service with mocked dependencies for testing.
229+
*
230+
* @return \Pterodactyl\Services\Databases\DeployServerDatabaseService
231+
*/
232+
private function getService(): DeployServerDatabaseService
233+
{
234+
return new DeployServerDatabaseService($this->repository, $this->databaseHostRepository, $this->managementService);
235+
}
236+
}

0 commit comments

Comments
 (0)