diff --git a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php index 6056747ed..6f18138ad 100644 --- a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php @@ -13,10 +13,14 @@ use Mockery as m; use Pterodactyl\Models\User; use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; +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; @@ -134,4 +138,44 @@ class IndexControllerTest extends ControllerTestCase $this->assertResponseCodeEquals(200, $response); $this->assertResponseJsonEquals(['status' => 30], $response); } + + /** + * Test the status controller with a ServerConnectionException. + */ + 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 with a RequestException. + */ + 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); + } } 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. */