forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableValidatorServiceTest.php
More file actions
137 lines (114 loc) · 5.26 KB
/
VariableValidatorServiceTest.php
File metadata and controls
137 lines (114 loc) · 5.26 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
<?php
namespace Pterodactyl\Tests\Integration\Services\Servers;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Servers\VariableValidatorService;
class VariableValidatorServiceTest extends IntegrationTestCase
{
/**
* Test that enviornment variables for a server are validated as expected.
*/
public function testEnvironmentVariablesCanBeValidated()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
try {
$this->getService()->handle($egg->id, [
'BUNGEE_VERSION' => '1.2.3',
]);
$this->assertTrue(false, 'This statement should not be reached.');
} catch (ValidationException $exception) {
$errors = $exception->errors();
$this->assertCount(2, $errors);
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);
$this->assertArrayHasKey('environment.SERVER_JARFILE', $errors);
$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);
$this->assertSame('The Bungeecord Jar File variable field is required.', $errors['environment.SERVER_JARFILE'][0]);
}
$response = $this->getService()->handle($egg->id, [
'BUNGEE_VERSION' => '1234',
'SERVER_JARFILE' => 'server.jar',
]);
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(2, $response);
$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);
$this->assertSame('1234', $response->get(0)->value);
$this->assertSame('SERVER_JARFILE', $response->get(1)->key);
$this->assertSame('server.jar', $response->get(1)->value);
}
/**
* Test that variables that are user_editable=false do not get validated (or returned) by
* the handler.
*/
public function testNormalUserCannotValidateNonUserEditableVariables()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg->variables()->first()->update([
'user_editable' => false,
]);
$response = $this->getService()->handle($egg->id, [
// This is an invalid value, but it shouldn't cause any issues since it should be skipped.
'BUNGEE_VERSION' => '1.2.3',
'SERVER_JARFILE' => 'server.jar',
]);
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(1, $response);
$this->assertSame('SERVER_JARFILE', $response->get(0)->key);
$this->assertSame('server.jar', $response->get(0)->value);
}
public function testEnvironmentVariablesCanBeUpdatedAsAdmin()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg->variables()->first()->update([
'user_editable' => false,
]);
try {
$this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
'BUNGEE_VERSION' => '1.2.3',
'SERVER_JARFILE' => 'server.jar',
]);
$this->assertTrue(false, 'This statement should not be reached.');
} catch (ValidationException $exception) {
$this->assertCount(1, $exception->errors());
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors());
}
$response = $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [
'BUNGEE_VERSION' => '123',
'SERVER_JARFILE' => 'server.jar',
]);
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(2, $response);
$this->assertSame('BUNGEE_VERSION', $response->get(0)->key);
$this->assertSame('123', $response->get(0)->value);
$this->assertSame('SERVER_JARFILE', $response->get(1)->key);
$this->assertSame('server.jar', $response->get(1)->value);
}
public function testNullableEnvironmentVariablesCanBeUsedCorrectly()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete();
$egg->variables()->update(['rules' => 'nullable|string']);
$response = $this->getService()->handle($egg->id, []);
$this->assertCount(1, $response);
$this->assertNull($response->get(0)->value);
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => null]);
$this->assertCount(1, $response);
$this->assertNull($response->get(0)->value);
$response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => '']);
$this->assertCount(1, $response);
$this->assertSame('', $response->get(0)->value);
}
/**
* @return \Pterodactyl\Services\Servers\VariableValidatorService
*/
private function getService()
{
return $this->app->make(VariableValidatorService::class);
}
}