From 038e5e1b7d48d20b54483cf852d7f5846ec6f974 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 13 May 2018 15:37:02 -0400 Subject: [PATCH 1/4] 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); + } } From a6bca34677ef1806b0d9113e4952eb8216e4add4 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 13 May 2018 15:57:07 -0400 Subject: [PATCH 2/4] Add more exception tests --- .../Base/SecurityControllerTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Unit/Http/Controllers/Base/SecurityControllerTest.php b/tests/Unit/Http/Controllers/Base/SecurityControllerTest.php index 72435791b..363a59801 100644 --- a/tests/Unit/Http/Controllers/Base/SecurityControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/SecurityControllerTest.php @@ -98,6 +98,36 @@ class SecurityControllerTest extends ControllerTestCase $this->assertResponseJsonEquals(['qrImage' => 'qrCodeImage'], $response); } + /** + * Test TOTP setting controller when no exception is thrown by the service. + */ + public function testSetTotpControllerSuccess() + { + $model = $this->generateRequestUserModel(); + + $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); + $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once(); + + $response = $this->getController()->setTotp($this->request); + $this->assertIsResponse($response); + $this->assertSame('true', $response->getContent()); + } + + /** + * Test TOTP setting controller when an exception is thrown by the service. + */ + public function testSetTotpControllerWhenExceptionIsThrown() + { + $model = $this->generateRequestUserModel(); + + $this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken'); + $this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once()->andThrow(new TwoFactorAuthenticationTokenInvalid()); + + $response = $this->getController()->setTotp($this->request); + $this->assertIsResponse($response); + $this->assertSame('false', $response->getContent()); + } + /** * Test the disable totp controller when no exception is thrown by the service. */ From 06229a328f19dfc6a038f12d0534f5de9a355773 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 13 May 2018 15:59:23 -0400 Subject: [PATCH 3/4] Fix the description of the tests --- tests/Unit/Http/Controllers/Base/IndexControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php index 312e40890..bce928c33 100644 --- a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php @@ -141,7 +141,7 @@ class IndexControllerTest extends ControllerTestCase } /** - * Test the status controller when we can't connect to a server. + * Test the status controller with a ServerConnectionException. */ public function testStatusControllerWithServerConnectionException() { @@ -161,7 +161,7 @@ class IndexControllerTest extends ControllerTestCase } /** - * Test the status controller when we can't connect to a server. + * Test the status controller with a RequestException. */ public function testStatusControllerWithRequestException() { From a17570c1cb31c12cb091ae7e17a59196e2798f63 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 13 May 2018 18:33:51 -0400 Subject: [PATCH 4/4] Fix styling --- tests/Unit/Http/Controllers/Base/IndexControllerTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php index bce928c33..6f18138ad 100644 --- a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php @@ -9,22 +9,21 @@ 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 GuzzleHttp\Psr7\ServerRequest; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Exception\RequestException; use Tests\Assertions\ControllerAssertionsTrait; use Tests\Unit\Http\Controllers\ControllerTestCase; use Pterodactyl\Http\Controllers\Base\IndexController; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Symfony\Component\HttpKernel\Exception\HttpException; 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 {