Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Startup/UpdateStartupVariableTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Startup;
4
5
use Pterodactyl\Models\User;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Models\Permission;
8
use Pterodactyl\Models\EggVariable;
9
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
10
11
class UpdateStartupVariableTest extends ClientApiIntegrationTestCase
12
{
13
/**
14
* Test that a startup variable can be edited successfully for a server.
15
*/
16
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
17
public function testStartupVariableCanBeUpdated(array $permissions)
18
{
19
/** @var \Pterodactyl\Models\Server $server */
20
[$user, $server] = $this->generateTestAccount($permissions);
21
$server->fill([
22
'startup' => 'java {{SERVER_JARFILE}} --version {{BUNGEE_VERSION}}',
23
])->save();
24
25
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
26
'key' => 'BUNGEE_VERSION',
27
'value' => '1.2.3',
28
]);
29
30
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
31
$response->assertJsonPath('errors.0.code', 'ValidationException');
32
$response->assertJsonPath('errors.0.detail', 'The value may only contain letters and numbers.');
33
34
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
35
'key' => 'BUNGEE_VERSION',
36
'value' => '123',
37
]);
38
39
$response->assertOk();
40
$response->assertJsonPath('object', EggVariable::RESOURCE_NAME);
41
$this->assertJsonTransformedWith($response->json('attributes'), $server->variables[0]);
42
$response->assertJsonPath('meta.startup_command', 'java bungeecord.jar --version 123');
43
$response->assertJsonPath('meta.raw_startup_command', $server->startup);
44
}
45
46
/**
47
* Test that variables that are either not user_viewable, or not user_editable, cannot be
48
* updated via this endpoint.
49
*/
50
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
51
public function testStartupVariableCannotBeUpdatedIfNotUserViewableOrEditable(array $permissions)
52
{
53
/** @var \Pterodactyl\Models\Server $server */
54
[$user, $server] = $this->generateTestAccount($permissions);
55
56
$egg = $this->cloneEggAndVariables($server->egg);
57
$egg->variables()->where('env_variable', 'BUNGEE_VERSION')->update(['user_viewable' => false]);
58
$egg->variables()->where('env_variable', 'SERVER_JARFILE')->update(['user_editable' => false]);
59
60
$server->fill(['egg_id' => $egg->id])->save();
61
$server->refresh();
62
63
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
64
'key' => 'BUNGEE_VERSION',
65
'value' => '123',
66
]);
67
68
$response->assertStatus(Response::HTTP_BAD_REQUEST);
69
$response->assertJsonPath('errors.0.code', 'BadRequestHttpException');
70
$response->assertJsonPath('errors.0.detail', 'The environment variable you are trying to edit does not exist.');
71
72
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
73
'key' => 'SERVER_JARFILE',
74
'value' => 'server2.jar',
75
]);
76
77
$response->assertStatus(Response::HTTP_BAD_REQUEST);
78
$response->assertJsonPath('errors.0.code', 'BadRequestHttpException');
79
$response->assertJsonPath('errors.0.detail', 'The environment variable you are trying to edit is read-only.');
80
}
81
82
/**
83
* Test that a hidden variable is not included in the startup_command output for the server if
84
* a different variable is updated.
85
*/
86
public function testHiddenVariablesAreNotReturnedInStartupCommandWhenUpdatingVariable()
87
{
88
/** @var \Pterodactyl\Models\Server $server */
89
[$user, $server] = $this->generateTestAccount();
90
91
$egg = $this->cloneEggAndVariables($server->egg);
92
$egg->variables()->first()->update(['user_viewable' => false]);
93
94
$server->fill([
95
'egg_id' => $egg->id,
96
'startup' => 'java {{SERVER_JARFILE}} --version {{BUNGEE_VERSION}}',
97
])->save();
98
99
$server->refresh();
100
101
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
102
'key' => 'SERVER_JARFILE',
103
'value' => 'server2.jar',
104
]);
105
106
$response->assertOk();
107
$response->assertJsonPath('meta.startup_command', 'java server2.jar --version [hidden]');
108
$response->assertJsonPath('meta.raw_startup_command', $server->startup);
109
}
110
111
/**
112
* Test that an egg variable with a validation rule of 'nullable|string' works if no value
113
* is passed through in the request.
114
*
115
* @see https://github.com/pterodactyl/panel/issues/2433
116
*/
117
public function testEggVariableWithNullableStringIsNotRequired()
118
{
119
/** @var \Pterodactyl\Models\Server $server */
120
[$user, $server] = $this->generateTestAccount();
121
122
$egg = $this->cloneEggAndVariables($server->egg);
123
$egg->variables()->first()->update(['rules' => 'nullable|string']);
124
125
$server->fill(['egg_id' => $egg->id])->save();
126
$server->refresh();
127
128
$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [
129
'key' => 'BUNGEE_VERSION',
130
'value' => '',
131
]);
132
133
$response->assertOk();
134
$response->assertJsonPath('attributes.server_value', null);
135
}
136
137
/**
138
* Test that a variable cannot be updated if the user does not have permission to perform
139
* that action, or they aren't assigned at all to the server.
140
*/
141
public function testStartupVariableCannotBeUpdatedIfNotUserViewable()
142
{
143
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
144
$this->actingAs($user)->putJson($this->link($server) . '/startup/variable')->assertForbidden();
145
146
$user2 = User::factory()->create();
147
$this->actingAs($user2)->putJson($this->link($server) . '/startup/variable')->assertNotFound();
148
}
149
150
public static function permissionsDataProvider(): array
151
{
152
return [[[]], [[Permission::ACTION_STARTUP_UPDATE]]];
153
}
154
}
155
156