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;
|
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;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Servers\ReinstallServerService;
|
2017-07-23 19:51:18 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ReinstallServerServiceTest extends TestCase
|
2017-07-23 19:51:18 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\ReinstallServerService
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->server = factory(Server::class)->make(['node_id' => 1]);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new ReinstallServerService(
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->database,
|
|
|
|
$this->daemonServerRepository,
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->repository
|
2017-07-23 19:51:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is reinstalled when it's model is passed to the function.
|
|
|
|
*/
|
|
|
|
public function testServerShouldBeReinstalledWhenModelIsPassed()
|
|
|
|
{
|
|
|
|
$this->repository->shouldNotReceive('find');
|
|
|
|
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-02-28 01:43:47 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
|
|
|
|
'installed' => 0,
|
|
|
|
], true, true)->once()->andReturnNull();
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->reinstall($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is reinstalled when the ID of the server is passed to the function.
|
|
|
|
*/
|
|
|
|
public function testServerShouldBeReinstalledWhenServerIdIsPassed()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
|
|
|
|
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-02-28 01:43:47 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
|
|
|
|
'installed' => 0,
|
|
|
|
], true, true)->once()->andReturnNull();
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
|
2017-07-23 19:51:18 +00:00
|
|
|
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->reinstall($this->server->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception thrown by guzzle is rendered as a displayable exception.
|
2018-01-06 00:27:47 +00:00
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2017-07-23 19:51:18 +00:00
|
|
|
*/
|
|
|
|
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
|
|
|
|
{
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-02-28 01:43:47 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
|
|
|
|
'installed' => 0,
|
|
|
|
], true, true)->once()->andReturnNull();
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->service->reinstall($this->server);
|
2017-07-23 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception thrown by something other than guzzle is not transformed to a displayable.
|
|
|
|
*
|
|
|
|
* @expectedException \Exception
|
|
|
|
*/
|
|
|
|
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
|
|
|
|
{
|
|
|
|
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-02-28 01:43:47 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($this->server->id, [
|
|
|
|
'installed' => 0,
|
|
|
|
], true, true)->once()->andReturnNull();
|
2017-07-23 19:51:18 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception());
|
2017-07-23 19:51:18 +00:00
|
|
|
|
|
|
|
$this->service->reinstall($this->server);
|
|
|
|
}
|
|
|
|
}
|