Skip to content

Commit 532025a

Browse files
committed
Fix tests
1 parent d50ea18 commit 532025a

File tree

10 files changed

+463
-573
lines changed

10 files changed

+463
-573
lines changed

app/Services/Databases/DatabaseManagementService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Pterodactyl\Services\Databases;
1111

12+
use Pterodactyl\Models\Database;
1213
use Illuminate\Database\DatabaseManager;
1314
use Illuminate\Contracts\Encryption\Encrypter;
1415
use Pterodactyl\Extensions\DynamicDatabaseConnection;
@@ -95,7 +96,7 @@ public function create($server, array $data)
9596
$this->database->commit();
9697
} catch (\Exception $ex) {
9798
try {
98-
if (isset($database)) {
99+
if (isset($database) && $database instanceof Database) {
99100
$this->repository->dropDatabase($database->database);
100101
$this->repository->dropUser($database->username, $database->remote);
101102
$this->repository->flush();

database/factories/ModelFactory.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
});
4040

4141
$factory->define(Pterodactyl\Models\User::class, function (Faker\Generator $faker) {
42+
static $password;
43+
4244
return [
4345
'id' => $faker->unique()->randomNumber(),
4446
'external_id' => null,
@@ -47,7 +49,7 @@
4749
'email' => $faker->safeEmail,
4850
'name_first' => $faker->firstName,
4951
'name_last' => $faker->lastName,
50-
'password' => bcrypt('password'),
52+
'password' => $password ?: $password = bcrypt('password'),
5153
'language' => 'en',
5254
'root_admin' => false,
5355
'use_totp' => false,
@@ -173,6 +175,21 @@
173175
];
174176
});
175177

178+
$factory->define(Pterodactyl\Models\Database::class, function (Faker\Generator $faker) {
179+
static $password;
180+
181+
return [
182+
'id' => $faker->unique()->randomNumber(),
183+
'server_id' => $faker->randomNumber(),
184+
'database_host_id' => $faker->randomNumber(),
185+
'database' => str_random(10),
186+
'username' => str_random(10),
187+
'password' => $password ?: bcrypt('test123'),
188+
'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
189+
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
190+
];
191+
});
192+
176193
$factory->define(Pterodactyl\Models\Schedule::class, function (Faker\Generator $faker) {
177194
return [
178195
'id' => $faker->unique()->randomNumber(),

tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Prologue\Alerts\AlertsMessageBag;
1515
use Tests\Assertions\ControllerAssertionsTrait;
1616
use Pterodactyl\Http\Controllers\Admin\DatabaseController;
17+
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
18+
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
19+
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
1720
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
1821
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
1922

@@ -22,29 +25,34 @@ class DatabaseControllerTest extends TestCase
2225
use ControllerAssertionsTrait;
2326

2427
/**
25-
* @var \Prologue\Alerts\AlertsMessageBag
28+
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
2629
*/
27-
protected $alert;
30+
private $alert;
2831

2932
/**
30-
* @var \Pterodactyl\Http\Controllers\Admin\DatabaseController
33+
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService|\Mockery\Mock
3134
*/
32-
protected $controller;
35+
private $creationService;
3336

3437
/**
35-
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
38+
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService|\Mockery\Mock
3639
*/
37-
protected $locationRepository;
40+
private $deletionService;
3841

3942
/**
40-
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
43+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface|\Mockery\Mock
4144
*/
42-
protected $repository;
45+
private $locationRepository;
4346

4447
/**
45-
* @var \Pterodactyl\Services\Databases\HostsUpdateService
48+
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface|\Mockery\Mock
4649
*/
47-
protected $service;
50+
private $repository;
51+
52+
/**
53+
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService|\Mockery\Mock
54+
*/
55+
private $updateService;
4856

4957
/**
5058
* Setup tests.
@@ -54,16 +62,11 @@ public function setUp()
5462
parent::setUp();
5563

5664
$this->alert = m::mock(AlertsMessageBag::class);
65+
$this->creationService = m::mock(HostCreationService::class);
66+
$this->deletionService = m::mock(HostDeletionService::class);
5767
$this->locationRepository = m::mock(LocationRepositoryInterface::class);
5868
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
59-
$this->service = m::mock(HostUpdateService::class);
60-
61-
$this->controller = new DatabaseController(
62-
$this->alert,
63-
$this->repository,
64-
$this->service,
65-
$this->locationRepository
66-
);
69+
$this->updateService = m::mock(HostUpdateService::class);
6770
}
6871

6972
/**
@@ -74,7 +77,7 @@ public function testIndexController()
7477
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
7578
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('getWithViewDetails');
7679

77-
$response = $this->controller->index();
80+
$response = $this->getController()->index();
7881

7982
$this->assertIsViewResponse($response);
8083
$this->assertViewNameEquals('admin.databases.index', $response);
@@ -92,7 +95,7 @@ public function testViewController()
9295
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
9396
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
9497

95-
$response = $this->controller->view(1);
98+
$response = $this->getController()->view(1);
9699

97100
$this->assertIsViewResponse($response);
98101
$this->assertViewNameEquals('admin.databases.view', $response);
@@ -101,4 +104,21 @@ public function testViewController()
101104
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
102105
$this->assertViewKeyEquals('host', 'getWithServers', $response);
103106
}
107+
108+
/**
109+
* Return an instance of the DatabaseController with mock dependencies.
110+
*
111+
* @return \Pterodactyl\Http\Controllers\Admin\DatabaseController
112+
*/
113+
private function getController(): DatabaseController
114+
{
115+
return new DatabaseController(
116+
$this->alert,
117+
$this->repository,
118+
$this->creationService,
119+
$this->deletionService,
120+
$this->updateService,
121+
$this->locationRepository
122+
);
123+
}
104124
}

tests/Unit/Repositories/Eloquent/DatabaseRepositoryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testExceptionIsThrownIfDatabaseAlreadyExists()
9696
public function testCreateDatabaseStatement()
9797
{
9898
$query = sprintf('CREATE DATABASE IF NOT EXISTS `%s`', 'test_database');
99-
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
99+
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
100100

101101
$this->assertTrue($this->repository->createDatabase('test_database', 'test'));
102102
}
@@ -107,7 +107,7 @@ public function testCreateDatabaseStatement()
107107
public function testCreateUserStatement()
108108
{
109109
$query = sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', 'test', '%', 'password');
110-
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
110+
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
111111

112112
$this->assertTrue($this->repository->createUser('test', '%', 'password', 'test'));
113113
}
@@ -118,7 +118,7 @@ public function testCreateUserStatement()
118118
public function testUserAssignmentToDatabaseStatement()
119119
{
120120
$query = sprintf('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`', 'test_database', 'test', '%');
121-
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
121+
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
122122

123123
$this->assertTrue($this->repository->assignUserToDatabase('test_database', 'test', '%', 'test'));
124124
}
@@ -128,7 +128,7 @@ public function testUserAssignmentToDatabaseStatement()
128128
*/
129129
public function testFlushStatement()
130130
{
131-
$this->repository->shouldReceive('runStatement')->with('FLUSH PRIVILEGES', 'test')->once()->andReturn(true);
131+
$this->repository->shouldReceive('runStatement')->with('FLUSH PRIVILEGES')->once()->andReturn(true);
132132

133133
$this->assertTrue($this->repository->flush('test'));
134134
}
@@ -139,7 +139,7 @@ public function testFlushStatement()
139139
public function testDropDatabaseStatement()
140140
{
141141
$query = sprintf('DROP DATABASE IF EXISTS `%s`', 'test_database');
142-
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
142+
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
143143

144144
$this->assertTrue($this->repository->dropDatabase('test_database', 'test'));
145145
}
@@ -150,7 +150,7 @@ public function testDropDatabaseStatement()
150150
public function testDropUserStatement()
151151
{
152152
$query = sprintf('DROP USER IF EXISTS `%s`@`%s`', 'test', '%');
153-
$this->repository->shouldReceive('runStatement')->with($query, 'test')->once()->andReturn(true);
153+
$this->repository->shouldReceive('runStatement')->with($query)->once()->andReturn(true);
154154

155155
$this->assertTrue($this->repository->dropUser('test', '%', 'test'));
156156
}

0 commit comments

Comments
 (0)