From 038e5e1b7d48d20b54483cf852d7f5846ec6f974 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 13 May 2018 15:37:02 -0400 Subject: [PATCH] Add exception tests --- .../Controllers/Base/IndexControllerTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php index 6056747ed..312e40890 100644 --- a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php @@ -9,10 +9,14 @@ namespace Tests\Unit\Http\Controllers\Base; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Psr7\ServerRequest; use Mockery as m; use Pterodactyl\Models\User; use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; +use Symfony\Component\HttpKernel\Exception\HttpException; use Tests\Assertions\ControllerAssertionsTrait; use Tests\Unit\Http\Controllers\ControllerTestCase; use Pterodactyl\Http\Controllers\Base\IndexController; @@ -20,6 +24,7 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Psr\Http\Message\RequestInterface; class IndexControllerTest extends ControllerTestCase { @@ -134,4 +139,44 @@ class IndexControllerTest extends ControllerTestCase $this->assertResponseCodeEquals(200, $response); $this->assertResponseJsonEquals(['status' => 30], $response); } + + /** + * Test the status controller when we can't connect to a server. + */ + public function testStatusControllerWithServerConnectionException() + { + $user = factory(User::class)->make(); + $server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]); + + $this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user); + $this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server); + $this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123'); + + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('test123')->once()->andReturnSelf() + ->shouldReceive('details')->withNoArgs()->once()->andThrow(new ConnectException('bad connection', new ServerRequest('', ''))); + + $this->expectExceptionObject(new HttpException(500, 'bad connection')); + $this->controller->status($this->request, $server->uuidShort); + } + + /** + * Test the status controller when we can't connect to a server. + */ + public function testStatusControllerWithRequestException() + { + $user = factory(User::class)->make(); + $server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]); + + $this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user); + $this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server); + $this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123'); + + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('test123')->once()->andReturnSelf() + ->shouldReceive('details')->withNoArgs()->once()->andThrow(new RequestException('bad request', new ServerRequest('', ''))); + + $this->expectExceptionObject(new HttpException(500, 'bad request')); + $this->controller->status($this->request, $server->uuidShort); + } }