Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Services\Databases;
4
5
use Mockery\MockInterface;
6
use Pterodactyl\Models\Database;
7
use Pterodactyl\Models\DatabaseHost;
8
use Pterodactyl\Tests\Integration\IntegrationTestCase;
9
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
10
use Pterodactyl\Services\Databases\DatabaseManagementService;
11
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
12
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
13
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
14
15
class DatabaseManagementServiceTest extends IntegrationTestCase
16
{
17
private MockInterface $repository;
18
19
/**
20
* Setup tests.
21
*/
22
public function setUp(): void
23
{
24
parent::setUp();
25
26
config()->set('pterodactyl.client_features.databases.enabled', true);
27
28
$this->repository = $this->mock(DatabaseRepository::class);
29
}
30
31
/**
32
* Test that the name generated by the unique name function is what we expect.
33
*/
34
public function testUniqueDatabaseNameIsGeneratedCorrectly()
35
{
36
$this->assertSame('s1_example', DatabaseManagementService::generateUniqueDatabaseName('example', 1));
37
$this->assertSame('s123_something_else', DatabaseManagementService::generateUniqueDatabaseName('something_else', 123));
38
$this->assertSame('s123_' . str_repeat('a', 43), DatabaseManagementService::generateUniqueDatabaseName(str_repeat('a', 100), 123));
39
}
40
41
/**
42
* Test that disabling the client database feature flag prevents the creation of databases.
43
*/
44
public function testExceptionIsThrownIfClientDatabasesAreNotEnabled()
45
{
46
config()->set('pterodactyl.client_features.databases.enabled', false);
47
48
$this->expectException(DatabaseClientFeatureNotEnabledException::class);
49
50
$server = $this->createServerModel();
51
$this->getService()->create($server, []);
52
}
53
54
/**
55
* Test that a server at its database limit cannot have an additional one created if
56
* the $validateDatabaseLimit flag is not set to false.
57
*/
58
public function testDatabaseCannotBeCreatedIfServerHasReachedLimit()
59
{
60
$server = $this->createServerModel(['database_limit' => 2]);
61
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
62
63
Database::factory()->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]);
64
65
$this->expectException(TooManyDatabasesException::class);
66
67
$this->getService()->create($server, []);
68
}
69
70
/**
71
* Test that a missing or invalid database name format causes an exception to be thrown.
72
*/
73
#[\PHPUnit\Framework\Attributes\DataProvider('invalidDataDataProvider')]
74
public function testEmptyDatabaseNameOrInvalidNameTriggersAnException(array $data)
75
{
76
$server = $this->createServerModel();
77
78
$this->expectException(\InvalidArgumentException::class);
79
$this->expectExceptionMessage('The database name passed to DatabaseManagementService::handle MUST be prefixed with "s{server_id}_".');
80
81
$this->getService()->create($server, $data);
82
}
83
84
/**
85
* Test that creating a server database with an identical name triggers an exception.
86
*/
87
public function testCreatingDatabaseWithIdenticalNameTriggersAnException()
88
{
89
$server = $this->createServerModel();
90
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
91
92
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
93
$host2 = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
94
Database::factory()->create([
95
'database' => $name,
96
'database_host_id' => $host->id,
97
'server_id' => $server->id,
98
]);
99
100
$this->expectException(DuplicateDatabaseNameException::class);
101
$this->expectExceptionMessage('A database with that name already exists for this server.');
102
103
// Try to create a database with the same name as a database on a different host. We expect
104
// this to fail since we don't account for the specific host when checking uniqueness.
105
$this->getService()->create($server, [
106
'database' => $name,
107
'database_host_id' => $host2->id,
108
]);
109
110
$this->assertDatabaseMissing('databases', ['server_id' => $server->id]);
111
}
112
113
/**
114
* Test that a server database can be created successfully.
115
*/
116
public function testServerDatabaseCanBeCreated()
117
{
118
$server = $this->createServerModel();
119
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
120
121
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
122
123
$this->repository->expects('createDatabase')->with($name);
124
125
$username = null;
126
$secondUsername = null;
127
$password = null;
128
129
// The value setting inside the closures if to avoid throwing an exception during the
130
// assertions that would get caught by the functions catcher and thus lead to the exception
131
// being swallowed incorrectly.
132
$this->repository->expects('createUser')->with(
133
\Mockery::on(function ($value) use (&$username) {
134
$username = $value;
135
136
return true;
137
}),
138
'%',
139
\Mockery::on(function ($value) use (&$password) {
140
$password = $value;
141
142
return true;
143
}),
144
null
145
);
146
147
$this->repository->expects('assignUserToDatabase')->with($name, \Mockery::on(function ($value) use (&$secondUsername) {
148
$secondUsername = $value;
149
150
return true;
151
}), '%');
152
153
$this->repository->expects('flush')->withNoArgs();
154
155
$response = $this->getService()->create($server, [
156
'remote' => '%',
157
'database' => $name,
158
'database_host_id' => $host->id,
159
]);
160
161
$this->assertInstanceOf(Database::class, $response);
162
$this->assertSame($response->server_id, $server->id);
163
$this->assertMatchesRegularExpression('/^(u\d+_)(\w){10}$/', $username);
164
$this->assertSame($username, $secondUsername);
165
$this->assertSame(24, strlen($password));
166
167
$this->assertDatabaseHas('databases', ['server_id' => $server->id, 'id' => $response->id]);
168
}
169
170
/**
171
* Test that an exception encountered while creating the database leads to the cleanup code
172
* being called and any exceptions encountered while cleaning up go unreported.
173
*/
174
public function testExceptionEncounteredWhileCreatingDatabaseAttemptsToCleanup()
175
{
176
$server = $this->createServerModel();
177
$name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id);
178
179
$host = DatabaseHost::factory()->create(['node_id' => $server->node_id]);
180
181
$this->repository->expects('createDatabase')->with($name)->andThrows(new \BadMethodCallException());
182
$this->repository->expects('dropDatabase')->with($name);
183
$this->repository->expects('dropUser')->withAnyArgs()->andThrows(new \InvalidArgumentException());
184
185
$this->expectException(\BadMethodCallException::class);
186
187
$this->getService()->create($server, [
188
'remote' => '%',
189
'database' => $name,
190
'database_host_id' => $host->id,
191
]);
192
193
$this->assertDatabaseMissing('databases', ['server_id' => $server->id]);
194
}
195
196
public static function invalidDataDataProvider(): array
197
{
198
return [
199
[[]],
200
[['database' => '']],
201
[['database' => 'something']],
202
[['database' => 's_something']],
203
[['database' => 's12s_something']],
204
[['database' => 's12something']],
205
];
206
}
207
208
private function getService(): DatabaseManagementService
209
{
210
return $this->app->make(DatabaseManagementService::class);
211
}
212
}
213
214