forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHostServiceTest.php
More file actions
202 lines (163 loc) · 7.1 KB
/
DatabaseHostServiceTest.php
File metadata and controls
202 lines (163 loc) · 7.1 KB
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
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Administrative;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Services\Database\DatabaseHostService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseHostServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $databaseRepository;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
protected $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Database\DatabaseHostService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->database = m::mock(DatabaseManager::class);
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
$this->dynamic = m::mock(DynamicDatabaseConnection::class);
$this->encrypter = m::mock(Encrypter::class);
$this->repository = m::mock(DatabaseHostRepositoryInterface::class);
$this->service = new DatabaseHostService(
$this->database,
$this->databaseRepository,
$this->repository,
$this->dynamic,
$this->encrypter
);
}
/**
* Test that creating a host returns the correct data.
*/
public function testHostIsCreated()
{
$data = [
'password' => 'raw-password',
'name' => 'HostName',
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'someusername',
'node_id' => null,
];
$finalData = (object) array_replace($data, ['password' => 'enc-password']);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->encrypter->shouldReceive('encrypt')->with('raw-password')->once()->andReturn('enc-password');
$this->repository->shouldReceive('create')->with([
'password' => 'enc-password',
'name' => 'HostName',
'host' => '127.0.0.1',
'port' => 3306,
'username' => 'someusername',
'max_databases' => null,
'node_id' => null,
])->once()->andReturn($finalData);
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->create($data);
$this->assertNotNull($response);
$this->assertTrue(is_object($response), 'Assert that response is an object.');
$this->assertEquals('enc-password', $response->password);
$this->assertEquals('HostName', $response->name);
$this->assertEquals('127.0.0.1', $response->host);
$this->assertEquals(3306, $response->port);
$this->assertEquals('someusername', $response->username);
$this->assertNull($response->node_id);
}
/**
* Test that passing a password will store an encrypted version in the DB.
*/
public function testHostIsUpdatedWithPasswordProvided()
{
$finalData = (object) ['password' => 'enc-pass', 'host' => '123.456.78.9'];
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->encrypter->shouldReceive('encrypt')->with('raw-pass')->once()->andReturn('enc-pass');
$this->repository->shouldReceive('update')->with(1, [
'password' => 'enc-pass',
'host' => '123.456.78.9',
])->once()->andReturn($finalData);
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->update(1, ['password' => 'raw-pass', 'host' => '123.456.78.9']);
$this->assertNotNull($response);
$this->assertEquals('enc-pass', $response->password);
$this->assertEquals('123.456.78.9', $response->host);
}
/**
* Test that passing no or empty password will skip storing it.
*/
public function testHostIsUpdatedWithoutPassword()
{
$finalData = (object) ['host' => '123.456.78.9'];
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->encrypter->shouldNotReceive('encrypt');
$this->repository->shouldReceive('update')->with(1, ['host' => '123.456.78.9'])->once()->andReturn($finalData);
$this->dynamic->shouldReceive('set')->with('dynamic', $finalData)->once()->andReturnNull();
$this->database->shouldReceive('connection')->with('dynamic')->once()->andReturnSelf()
->shouldReceive('select')->with('SELECT 1 FROM dual')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->update(1, ['password' => '', 'host' => '123.456.78.9']);
$this->assertNotNull($response);
$this->assertEquals('123.456.78.9', $response->host);
}
/**
* Test that a database host can be deleted.
*/
public function testHostIsDeleted()
{
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1]])->once()->andReturn(0);
$this->repository->shouldReceive('delete')->with(1)->once()->andReturn(true);
$response = $this->service->delete(1);
$this->assertTrue($response, 'Assert that response is true.');
}
/**
* Test exception is thrown when there are databases attached to a host.
*/
public function testExceptionIsThrownIfHostHasDatabases()
{
$this->databaseRepository->shouldReceive('findCountWhere')->with([['database_host_id', '=', 1]])->once()->andReturn(2);
try {
$this->service->delete(1);
} catch (DisplayException $exception) {
$this->assertEquals(trans('exceptions.databases.delete_has_databases'), $exception->getMessage());
}
}
}