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