|
3 | 3 | namespace Pterodactyl\Tests\Integration\Services\Servers; |
4 | 4 |
|
5 | 5 | use Exception; |
| 6 | +use Ramsey\Uuid\Uuid; |
| 7 | +use Pterodactyl\Models\Egg; |
| 8 | +use Pterodactyl\Models\User; |
| 9 | +use Pterodactyl\Models\Nest; |
6 | 10 | use Pterodactyl\Models\Server; |
7 | 11 | use Pterodactyl\Models\ServerVariable; |
8 | 12 | use Illuminate\Validation\ValidationException; |
9 | 13 | use Pterodactyl\Tests\Integration\IntegrationTestCase; |
| 14 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
10 | 15 | use Pterodactyl\Services\Servers\StartupModificationService; |
11 | 16 |
|
12 | 17 | class StartupModificationServiceTest extends IntegrationTestCase |
@@ -46,20 +51,121 @@ public function testNonAdminCanModifyServerVariables() |
46 | 51 |
|
47 | 52 | ServerVariable::query()->where('variable_id', $server->variables[1]->id)->delete(); |
48 | 53 |
|
49 | | - /** @var \Pterodactyl\Models\Server $result */ |
50 | | - $result = $this->app->make(StartupModificationService::class)->handle($server, [ |
51 | | - 'egg_id' => $server->egg_id + 1, |
52 | | - 'startup' => 'random gibberish', |
53 | | - 'environment' => [ |
54 | | - 'BUNGEE_VERSION' => '1234', |
55 | | - 'SERVER_JARFILE' => 'test.jar', |
56 | | - ], |
57 | | - ]); |
| 54 | + $result = $this->getService() |
| 55 | + ->handle($server, [ |
| 56 | + 'egg_id' => $server->egg_id + 1, |
| 57 | + 'startup' => 'random gibberish', |
| 58 | + 'environment' => [ |
| 59 | + 'BUNGEE_VERSION' => '1234', |
| 60 | + 'SERVER_JARFILE' => 'test.jar', |
| 61 | + ], |
| 62 | + ]); |
58 | 63 |
|
59 | 64 | $this->assertInstanceOf(Server::class, $result); |
60 | 65 | $this->assertCount(2, $result->variables); |
61 | 66 | $this->assertSame($server->startup, $result->startup); |
62 | 67 | $this->assertSame('1234', $result->variables[0]->server_value); |
63 | 68 | $this->assertSame('test.jar', $result->variables[1]->server_value); |
64 | 69 | } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test that modifying an egg as an admin properly updates the data for the server. |
| 73 | + */ |
| 74 | + public function testServerIsProperlyModifiedAsAdminUser() |
| 75 | + { |
| 76 | + /** @var \Pterodactyl\Models\Egg $nextEgg */ |
| 77 | + $nextEgg = Nest::query()->findOrFail(2)->eggs()->firstOrFail(); |
| 78 | + |
| 79 | + $server = $this->createServerModel(['egg_id' => 1]); |
| 80 | + |
| 81 | + $this->assertNotSame($nextEgg->id, $server->egg_id); |
| 82 | + $this->assertNotSame($nextEgg->nest_id, $server->nest_id); |
| 83 | + |
| 84 | + $response = $this->getService() |
| 85 | + ->setUserLevel(User::USER_LEVEL_ADMIN) |
| 86 | + ->handle($server, [ |
| 87 | + 'egg_id' => $nextEgg->id, |
| 88 | + 'startup' => 'sample startup', |
| 89 | + 'skip_scripts' => true, |
| 90 | + 'docker_image' => 'docker/hodor', |
| 91 | + ]); |
| 92 | + |
| 93 | + $this->assertInstanceOf(Server::class, $response); |
| 94 | + $this->assertSame($nextEgg->id, $response->egg_id); |
| 95 | + $this->assertSame($nextEgg->nest_id, $response->nest_id); |
| 96 | + $this->assertSame('sample startup', $response->startup); |
| 97 | + $this->assertSame('docker/hodor', $response->image); |
| 98 | + $this->assertTrue($response->skip_scripts); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test that hidden variables can be updated by an admin but are not affected by a |
| 103 | + * regular user who attempts to pass them through. |
| 104 | + */ |
| 105 | + public function testEnvironmentVariablesCanBeUpdatedByAdmin() |
| 106 | + { |
| 107 | + $server = $this->createServerModel(); |
| 108 | + $server->loadMissing(['egg', 'variables']); |
| 109 | + |
| 110 | + $clone = $this->cloneEggAndVariables($server->egg); |
| 111 | + // This makes the BUNGEE_VERSION variable not user editable. |
| 112 | + $clone->variables()->first()->update([ |
| 113 | + 'user_editable' => false, |
| 114 | + ]); |
| 115 | + |
| 116 | + $server->fill(['egg_id' => $clone->id])->saveOrFail(); |
| 117 | + $server->refresh(); |
| 118 | + |
| 119 | + ServerVariable::query()->updateOrCreate([ |
| 120 | + 'server_id' => $server->id, |
| 121 | + 'variable_id' => $server->variables[0]->id, |
| 122 | + ], ['variable_value' => 'EXIST']); |
| 123 | + |
| 124 | + $response = $this->getService()->handle($server, [ |
| 125 | + 'environment' => [ |
| 126 | + 'BUNGEE_VERSION' => '1234', |
| 127 | + 'SERVER_JARFILE' => 'test.jar', |
| 128 | + ], |
| 129 | + ]); |
| 130 | + |
| 131 | + $this->assertCount(2, $response->variables); |
| 132 | + $this->assertSame('EXIST', $response->variables[0]->server_value); |
| 133 | + $this->assertSame('test.jar', $response->variables[1]->server_value); |
| 134 | + |
| 135 | + $response = $this->getService() |
| 136 | + ->setUserLevel(User::USER_LEVEL_ADMIN) |
| 137 | + ->handle($server, [ |
| 138 | + 'environment' => [ |
| 139 | + 'BUNGEE_VERSION' => '1234', |
| 140 | + 'SERVER_JARFILE' => 'test.jar', |
| 141 | + ], |
| 142 | + ]); |
| 143 | + |
| 144 | + $this->assertCount(2, $response->variables); |
| 145 | + $this->assertSame('1234', $response->variables[0]->server_value); |
| 146 | + $this->assertSame('test.jar', $response->variables[1]->server_value); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Test that passing an invalid egg ID into the function throws an exception |
| 151 | + * rather than silently failing or skipping. |
| 152 | + */ |
| 153 | + public function testInvalidEggIdTriggersException() |
| 154 | + { |
| 155 | + $server = $this->createServerModel(); |
| 156 | + |
| 157 | + $this->expectException(ModelNotFoundException::class); |
| 158 | + |
| 159 | + $this->getService() |
| 160 | + ->setUserLevel(User::USER_LEVEL_ADMIN) |
| 161 | + ->handle($server, ['egg_id' => 123456789]); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @return \Pterodactyl\Services\Servers\StartupModificationService |
| 166 | + */ |
| 167 | + private function getService() |
| 168 | + { |
| 169 | + return $this->app->make(StartupModificationService::class); |
| 170 | + } |
65 | 171 | } |
0 commit comments