Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Services/Servers/VariableValidatorServiceTest.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Services\Servers;
4
5
use Pterodactyl\Models\Egg;
6
use Pterodactyl\Models\User;
7
use Illuminate\Support\Collection;
8
use Illuminate\Validation\ValidationException;
9
use Pterodactyl\Tests\Integration\IntegrationTestCase;
10
use Pterodactyl\Services\Servers\VariableValidatorService;
11
12
class VariableValidatorServiceTest extends IntegrationTestCase
13
{
14
protected Egg $egg;
15
16
public function setUp(): void
17
{
18
parent::setUp();
19
20
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
21
$this->egg = Egg::query()
22
->where('author', '[email protected]')
23
->where('name', 'Bungeecord')
24
->firstOrFail();
25
}
26
27
/**
28
* Test that environment variables for a server are validated as expected.
29
*/
30
public function testEnvironmentVariablesCanBeValidated()
31
{
32
$egg = $this->cloneEggAndVariables($this->egg);
33
34
try {
35
$this->getService()->handle($egg->id, [
36
'BUNGEE_VERSION' => '1.2.3',
37
]);
38
39
$this->fail('This statement should not be reached.');
40
} catch (ValidationException $exception) {
41
$errors = $exception->errors();
42
43
$this->assertCount(2, $errors);
44
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);
45
$this->assertArrayHasKey('environment.SERVER_JARFILE', $errors);
46
$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);
47
$this->assertSame('The Bungeecord Jar File variable field is required.', $errors['environment.SERVER_JARFILE'][0]);
48
}
49
50
$response = $this->getService()->handle($egg->id, [
51
'BUNGEE_VERSION' => '1234',
52
'SERVER_JARFILE' => 'server.jar',
53
]);
54
55
$this->assertInstanceOf(Collection::class, $response);
56
$this->assertCount(2, $response);
57
$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);
58
$this->assertSame('1234', $response->get(0)->value);
59
$this->assertSame('SERVER_JARFILE', $response->get(1)->key);
60
$this->assertSame('server.jar', $response->get(1)->value);
61
}
62
63
/**
64
* Test that variables that are user_editable=false do not get validated (or returned) by
65
* the handler.
66
*/
67
public function testNormalUserCannotValidateNonUserEditableVariables()
68
{
69
$egg = $this->cloneEggAndVariables($this->egg);
70
$egg->variables()->first()->update([
71
'user_editable' => false,
72
]);
73
74
$response = $this->getService()->handle($egg->id, [
75
// This is an invalid value, but it shouldn't cause any issues since it should be skipped.
76
'BUNGEE_VERSION' => '1.2.3',
77
'SERVER_JARFILE' => 'server.jar',
78
]);
79
80
$this->assertInstanceOf(Collection::class, $response);
81
$this->assertCount(1, $response);
82
$this->assertSame('SERVER_JARFILE', $response->get(0)->key);
83
$this->assertSame('server.jar', $response->get(0)->value);
84
}
85
86
public function testEnvironmentVariablesCanBeUpdatedAsAdmin()
87
{
88
$egg = $this->cloneEggAndVariables($this->egg);
89
$egg->variables()->first()->update([
90
'user_editable' => false,
91
]);
92
93
try {
94
$this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
95
'BUNGEE_VERSION' => '1.2.3',
96
'SERVER_JARFILE' => 'server.jar',
97
]);
98
99
$this->fail('This statement should not be reached.');
100
} catch (ValidationException $exception) {
101
$this->assertCount(1, $exception->errors());
102
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());
103
}
104
105
$response = $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
106
'BUNGEE_VERSION' => '123',
107
'SERVER_JARFILE' => 'server.jar',
108
]);
109
110
$this->assertInstanceOf(Collection::class, $response);
111
$this->assertCount(2, $response);
112
$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);
113
$this->assertSame('123', $response->get(0)->value);
114
$this->assertSame('SERVER_JARFILE', $response->get(1)->key);
115
$this->assertSame('server.jar', $response->get(1)->value);
116
}
117
118
public function testNullableEnvironmentVariablesCanBeUsedCorrectly()
119
{
120
$egg = $this->cloneEggAndVariables($this->egg);
121
$egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete();
122
123
$egg->variables()->update(['rules' => 'nullable|string']);
124
125
$response = $this->getService()->handle($egg->id, []);
126
$this->assertCount(1, $response);
127
$this->assertNull($response->get(0)->value);
128
129
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => null]);
130
$this->assertCount(1, $response);
131
$this->assertNull($response->get(0)->value);
132
133
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => '']);
134
$this->assertCount(1, $response);
135
$this->assertSame('', $response->get(0)->value);
136
}
137
138
private function getService(): VariableValidatorService
139
{
140
return $this->app->make(VariableValidatorService::class);
141
}
142
}
143
144