Skip to content

Commit 341f6e4

Browse files
authored
Merge branch 'develop' into feature/upgrade-laravel-to-5.6
2 parents 5d06a96 + a0cdd3f commit 341f6e4

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tests/Unit/Http/Controllers/Base/IndexControllerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
use Pterodactyl\Models\User;
1414
use GuzzleHttp\Psr7\Response;
1515
use Pterodactyl\Models\Server;
16+
use GuzzleHttp\Psr7\ServerRequest;
17+
use GuzzleHttp\Exception\ConnectException;
18+
use GuzzleHttp\Exception\RequestException;
1619
use Tests\Assertions\ControllerAssertionsTrait;
1720
use Tests\Unit\Http\Controllers\ControllerTestCase;
1821
use Pterodactyl\Http\Controllers\Base\IndexController;
1922
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
23+
use Symfony\Component\HttpKernel\Exception\HttpException;
2024
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
2125
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
2226
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@@ -134,4 +138,44 @@ public function testStatusControllerWhenServerIsSuspended()
134138
$this->assertResponseCodeEquals(200, $response);
135139
$this->assertResponseJsonEquals(['status' => 30], $response);
136140
}
141+
142+
/**
143+
* Test the status controller with a ServerConnectionException.
144+
*/
145+
public function testStatusControllerWithServerConnectionException()
146+
{
147+
$user = factory(User::class)->make();
148+
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
149+
150+
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
151+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
152+
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
153+
154+
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
155+
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
156+
->shouldReceive('details')->withNoArgs()->once()->andThrow(new ConnectException('bad connection', new ServerRequest('', '')));
157+
158+
$this->expectExceptionObject(new HttpException(500, 'bad connection'));
159+
$this->controller->status($this->request, $server->uuidShort);
160+
}
161+
162+
/**
163+
* Test the status controller with a RequestException.
164+
*/
165+
public function testStatusControllerWithRequestException()
166+
{
167+
$user = factory(User::class)->make();
168+
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
169+
170+
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
171+
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
172+
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
173+
174+
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
175+
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
176+
->shouldReceive('details')->withNoArgs()->once()->andThrow(new RequestException('bad request', new ServerRequest('', '')));
177+
178+
$this->expectExceptionObject(new HttpException(500, 'bad request'));
179+
$this->controller->status($this->request, $server->uuidShort);
180+
}
137181
}

tests/Unit/Http/Controllers/Base/SecurityControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ public function testGenerateTotpController()
9898
$this->assertResponseJsonEquals(['qrImage' => 'qrCodeImage'], $response);
9999
}
100100

101+
/**
102+
* Test TOTP setting controller when no exception is thrown by the service.
103+
*/
104+
public function testSetTotpControllerSuccess()
105+
{
106+
$model = $this->generateRequestUserModel();
107+
108+
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
109+
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once();
110+
111+
$response = $this->getController()->setTotp($this->request);
112+
$this->assertIsResponse($response);
113+
$this->assertSame('true', $response->getContent());
114+
}
115+
116+
/**
117+
* Test TOTP setting controller when an exception is thrown by the service.
118+
*/
119+
public function testSetTotpControllerWhenExceptionIsThrown()
120+
{
121+
$model = $this->generateRequestUserModel();
122+
123+
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
124+
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once()->andThrow(new TwoFactorAuthenticationTokenInvalid());
125+
126+
$response = $this->getController()->setTotp($this->request);
127+
$this->assertIsResponse($response);
128+
$this->assertSame('false', $response->getContent());
129+
}
130+
101131
/**
102132
* Test the disable totp controller when no exception is thrown by the service.
103133
*/

0 commit comments

Comments
 (0)