Add some additional test coverage and clean up modification service and suspension service

This commit is contained in:
Dane Everitt 2020-10-07 21:56:44 -07:00
parent 83efb2d7b6
commit d087bebc93
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 214 additions and 503 deletions

View file

@ -9,7 +9,6 @@ use Pterodactyl\Models\Node;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Model;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Location;

View file

@ -0,0 +1,65 @@
<?php
namespace Pterodactyl\Tests\Integration\Services\Servers;
use Exception;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\ServerVariable;
use Illuminate\Validation\ValidationException;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Services\Servers\StartupModificationService;
class StartupModificationServiceTest extends IntegrationTestCase
{
/**
* Test that a non-admin request to modify the server startup parameters does
* not perform any egg or nest updates. This also attempts to pass through an
* egg_id variable which should have no impact if the request is coming from
* a non-admin entity.
*/
public function testNonAdminCanModifyServerVariables()
{
// Theoretically lines up with the Bungeecord Minecraft egg.
$server = $this->createServerModel(['egg_id' => 1]);
try {
$this->app->make(StartupModificationService::class)->handle($server, [
'egg_id' => $server->egg_id + 1,
'environment' => [
'BUNGEE_VERSION' => '$$',
'SERVER_JARFILE' => 'server.jar',
],
]);
$this->assertTrue(false, 'This assertion should not be called.');
} catch (Exception $exception) {
$this->assertInstanceOf(ValidationException::class, $exception);
/** @var \Illuminate\Validation\ValidationException $exception */
$errors = $exception->validator->errors()->toArray();
$this->assertCount(1, $errors);
$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);
$this->assertCount(1, $errors['environment.BUNGEE_VERSION']);
$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);
}
ServerVariable::query()->where('variable_id', $server->variables[1]->id)->delete();
/** @var \Pterodactyl\Models\Server $result */
$result = $this->app->make(StartupModificationService::class)->handle($server, [
'egg_id' => $server->egg_id + 1,
'startup' => 'random gibberish',
'environment' => [
'BUNGEE_VERSION' => '1234',
'SERVER_JARFILE' => 'test.jar',
],
]);
$this->assertInstanceOf(Server::class, $result);
$this->assertCount(2, $result->variables);
$this->assertSame($server->startup, $result->startup);
$this->assertSame('1234', $result->variables[0]->server_value);
$this->assertSame('test.jar', $result->variables[1]->server_value);
}
}

View file

@ -0,0 +1,80 @@
<?php
namespace Pterodactyl\Tests\Integration\Services\Servers;
use Mockery;
use InvalidArgumentException;
use Pterodactyl\Services\Servers\SuspensionService;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
class SuspensionServiceTest extends IntegrationTestCase
{
/** @var \Mockery\MockInterface */
private $repository;
/**
* Setup test instance.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = Mockery::mock(DaemonServerRepository::class);
$this->app->instance(DaemonServerRepository::class, $this->repository);
}
public function testServerIsSuspendedAndUnsuspended()
{
$server = $this->createServerModel(['suspended' => false]);
$this->repository->expects('setServer')->twice()->andReturnSelf();
$this->repository->expects('suspend')->with(false)->andReturnUndefined();
$this->getService()->toggle($server, SuspensionService::ACTION_SUSPEND);
$server->refresh();
$this->assertTrue($server->suspended);
$this->repository->expects('suspend')->with(true)->andReturnUndefined();
$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$server->refresh();
$this->assertFalse($server->suspended);
}
public function testNoActionIsTakenIfSuspensionStatusIsUnchanged()
{
$server = $this->createServerModel(['suspended' => false]);
$this->getService()->toggle($server, SuspensionService::ACTION_UNSUSPEND);
$server->refresh();
$this->assertFalse($server->suspended);
$server->update(['suspended' => true]);
$this->getService()->toggle($server, SuspensionService::ACTION_SUSPEND);
$server->refresh();
$this->assertTrue($server->suspended);
}
public function testExceptionIsThrownIfInvalidActionsArePassed()
{
$server = $this->createServerModel();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected one of: "suspend", "unsuspend". Got: "foo"');
$this->getService()->toggle($server, 'foo');
}
/**
* @return \Pterodactyl\Services\Servers\SuspensionService
*/
private function getService()
{
return $this->app->make(SuspensionService::class);
}
}