|
| 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 | + /** |
| 15 | + * Test that enviornment variables for a server are validated as expected. |
| 16 | + */ |
| 17 | + public function testEnvironmentVariablesCanBeValidated() |
| 18 | + { |
| 19 | + /** @noinspection PhpParamsInspection */ |
| 20 | + $egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1)); |
| 21 | + |
| 22 | + try { |
| 23 | + $this->getService()->handle($egg->id, [ |
| 24 | + 'BUNGEE_VERSION' => '1.2.3', |
| 25 | + ]); |
| 26 | + |
| 27 | + $this->assertTrue(false, 'This statement should not be reached.'); |
| 28 | + } catch (ValidationException $exception) { |
| 29 | + $errors = $exception->errors(); |
| 30 | + |
| 31 | + $this->assertCount(2, $errors); |
| 32 | + $this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors); |
| 33 | + $this->assertArrayHasKey('environment.SERVER_JARFILE', $errors); |
| 34 | + $this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]); |
| 35 | + $this->assertSame('The Bungeecord Jar File variable field is required.', $errors['environment.SERVER_JARFILE'][0]); |
| 36 | + } |
| 37 | + |
| 38 | + $response = $this->getService()->handle($egg->id, [ |
| 39 | + 'BUNGEE_VERSION' => '1234', |
| 40 | + 'SERVER_JARFILE' => 'server.jar', |
| 41 | + ]); |
| 42 | + |
| 43 | + $this->assertInstanceOf(Collection::class, $response); |
| 44 | + $this->assertCount(2, $response); |
| 45 | + $this->assertSame('BUNGEE_VERSION', $response->get(0)->key); |
| 46 | + $this->assertSame('1234', $response->get(0)->value); |
| 47 | + $this->assertSame('SERVER_JARFILE', $response->get(1)->key); |
| 48 | + $this->assertSame('server.jar', $response->get(1)->value); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that variables that are user_editable=false do not get validated (or returned) by |
| 53 | + * the handler. |
| 54 | + */ |
| 55 | + public function testNormalUserCannotValidateNonUserEditableVariables() |
| 56 | + { |
| 57 | + /** @noinspection PhpParamsInspection */ |
| 58 | + $egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1)); |
| 59 | + $egg->variables()->first()->update([ |
| 60 | + 'user_editable' => false, |
| 61 | + ]); |
| 62 | + |
| 63 | + $response = $this->getService()->handle($egg->id, [ |
| 64 | + // This is an invalid value, but it shouldn't cause any issues since it should be skipped. |
| 65 | + 'BUNGEE_VERSION' => '1.2.3', |
| 66 | + 'SERVER_JARFILE' => 'server.jar', |
| 67 | + ]); |
| 68 | + |
| 69 | + $this->assertInstanceOf(Collection::class, $response); |
| 70 | + $this->assertCount(1, $response); |
| 71 | + $this->assertSame('SERVER_JARFILE', $response->get(0)->key); |
| 72 | + $this->assertSame('server.jar', $response->get(0)->value); |
| 73 | + } |
| 74 | + |
| 75 | + public function testEnvironmentVariablesCanBeUpdatedAsAdmin() |
| 76 | + { |
| 77 | + /** @noinspection PhpParamsInspection */ |
| 78 | + $egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1)); |
| 79 | + $egg->variables()->first()->update([ |
| 80 | + 'user_editable' => false, |
| 81 | + ]); |
| 82 | + |
| 83 | + try { |
| 84 | + $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [ |
| 85 | + 'BUNGEE_VERSION' => '1.2.3', |
| 86 | + 'SERVER_JARFILE' => 'server.jar', |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->assertTrue(false, 'This statement should not be reached.'); |
| 90 | + } catch (ValidationException $exception) { |
| 91 | + $this->assertCount(1, $exception->errors()); |
| 92 | + $this->assertArrayHasKey('environment.BUNGEE_VERSION', $exception->errors()); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + $response = $this->getService()->setUserLevel(User::USER_LEVEL_ADMIN)->handle($egg->id, [ |
| 97 | + 'BUNGEE_VERSION' => '123', |
| 98 | + 'SERVER_JARFILE' => 'server.jar', |
| 99 | + ]); |
| 100 | + |
| 101 | + $this->assertInstanceOf(Collection::class, $response); |
| 102 | + $this->assertCount(2, $response); |
| 103 | + $this->assertSame('BUNGEE_VERSION', $response->get(0)->key); |
| 104 | + $this->assertSame('123', $response->get(0)->value); |
| 105 | + $this->assertSame('SERVER_JARFILE', $response->get(1)->key); |
| 106 | + $this->assertSame('server.jar', $response->get(1)->value); |
| 107 | + } |
| 108 | + |
| 109 | + public function testNullableEnvironmentVariablesCanBeUsedCorrectly() |
| 110 | + { |
| 111 | + /** @noinspection PhpParamsInspection */ |
| 112 | + $egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1)); |
| 113 | + $egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete(); |
| 114 | + |
| 115 | + $egg->variables()->update(['rules' => 'nullable|string']); |
| 116 | + |
| 117 | + $response = $this->getService()->handle($egg->id, []); |
| 118 | + $this->assertCount(1, $response); |
| 119 | + $this->assertNull($response->get(0)->value); |
| 120 | + |
| 121 | + $response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => null]); |
| 122 | + $this->assertCount(1, $response); |
| 123 | + $this->assertNull($response->get(0)->value); |
| 124 | + |
| 125 | + $response = $this->getService()->handle($egg->id, ['BUNGEE_VERSION' => '']); |
| 126 | + $this->assertCount(1, $response); |
| 127 | + $this->assertSame('', $response->get(0)->value); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @return \Pterodactyl\Services\Servers\VariableValidatorService |
| 132 | + */ |
| 133 | + private function getService() |
| 134 | + { |
| 135 | + return $this->app->make(VariableValidatorService::class); |
| 136 | + } |
| 137 | +} |
0 commit comments