2017-07-23 19:51:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Exception;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Illuminate\Log\Writer;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-23 19:51:18 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Services\Servers\SuspensionService;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
class SuspensionServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $daemonServerRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $database;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \GuzzleHttp\Exception\RequestException
|
|
|
|
*/
|
|
|
|
protected $exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\SuspensionService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Log\Writer
|
|
|
|
*/
|
|
|
|
protected $writer;
|
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
|
|
|
$this->database = m::mock(ConnectionInterface::class);
|
|
|
|
$this->exception = m::mock(RequestException::class)->makePartial();
|
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
2017-07-24 00:57:43 +00:00
|
|
|
$this->writer = m::mock(Writer::class);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
$this->server = factory(Server::class)->make(['suspended' => 0, 'node_id' => 1]);
|
|
|
|
|
|
|
|
$this->service = new SuspensionService(
|
|
|
|
$this->database,
|
|
|
|
$this->daemonServerRepository,
|
2017-07-24 00:57:43 +00:00
|
|
|
$this->repository,
|
|
|
|
$this->writer
|
2017-07-23 19:51:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the function accepts an integer in place of the server model.
|
|
|
|
*
|
|
|
|
* @expectedException \Exception
|
|
|
|
*/
|
|
|
|
public function testFunctionShouldAcceptAnIntegerInPlaceOfAServerModel()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andThrow(new Exception());
|
|
|
|
|
|
|
|
$this->service->toggle($this->server->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that no action being passed suspends a server.
|
|
|
|
*/
|
|
|
|
public function testServerShouldBeSuspendedWhenNoActionIsPassed()
|
|
|
|
{
|
|
|
|
$this->server->suspended = 0;
|
|
|
|
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2017-07-23 19:51:18 +00:00
|
|
|
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('suspend')->withNoArgs()->once()->andReturn(new Response);
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->toggle($this->server));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 22:26:30 +00:00
|
|
|
* Test that server is unsuspended if action=unsuspend.
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
public function testServerShouldBeUnsuspendedWhenUnsuspendActionIsPassed()
|
|
|
|
{
|
|
|
|
$this->server->suspended = 1;
|
|
|
|
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2017-07-23 19:51:18 +00:00
|
|
|
->shouldReceive('update')->with($this->server->id, ['suspended' => false])->once()->andReturnNull();
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('unsuspend')->withNoArgs()->once()->andReturn(new Response);
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->toggle($this->server, 'unsuspend'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 22:26:30 +00:00
|
|
|
* Test that nothing happens if a server is already unsuspended and action=unsuspend.
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
public function testNoActionShouldHappenIfServerIsAlreadyUnsuspendedAndActionIsUnsuspend()
|
|
|
|
{
|
|
|
|
$this->server->suspended = 0;
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->toggle($this->server, 'unsuspend'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-05 22:26:30 +00:00
|
|
|
* Test that nothing happens if a server is already suspended and action=suspend.
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
public function testNoActionShouldHappenIfServerIsAlreadySuspendedAndActionIsSuspend()
|
|
|
|
{
|
|
|
|
$this->server->suspended = 1;
|
|
|
|
|
|
|
|
$this->assertTrue($this->service->toggle($this->server, 'suspend'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception thrown by Guzzle is caught and transformed to a displayable exception.
|
|
|
|
*/
|
|
|
|
public function testExceptionThrownByGuzzleShouldBeCaughtAndTransformedToDisplayable()
|
|
|
|
{
|
|
|
|
$this->server->suspended = 0;
|
|
|
|
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
2017-07-23 19:51:18 +00:00
|
|
|
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)
|
2017-07-23 19:51:18 +00:00
|
|
|
->once()->andThrow($this->exception);
|
|
|
|
|
|
|
|
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
|
|
|
|
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
|
|
|
|
|
2017-07-24 00:57:43 +00:00
|
|
|
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
try {
|
|
|
|
$this->service->toggle($this->server);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(DisplayException::class, $exception);
|
|
|
|
$this->assertEquals(
|
2017-08-22 03:10:48 +00:00
|
|
|
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
|
|
|
|
$exception->getMessage()
|
2017-07-23 19:51:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that if action is not suspend or unsuspend an exception is thrown.
|
|
|
|
*
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testExceptionShouldBeThrownIfActionIsNotValid()
|
|
|
|
{
|
|
|
|
$this->service->toggle($this->server, 'random');
|
|
|
|
}
|
|
|
|
}
|