|
13 | 13 | use Pterodactyl\Models\User; |
14 | 14 | use GuzzleHttp\Psr7\Response; |
15 | 15 | use Pterodactyl\Models\Server; |
| 16 | +use GuzzleHttp\Psr7\ServerRequest; |
| 17 | +use GuzzleHttp\Exception\ConnectException; |
| 18 | +use GuzzleHttp\Exception\RequestException; |
16 | 19 | use Tests\Assertions\ControllerAssertionsTrait; |
17 | 20 | use Tests\Unit\Http\Controllers\ControllerTestCase; |
18 | 21 | use Pterodactyl\Http\Controllers\Base\IndexController; |
19 | 22 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 23 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
20 | 24 | use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; |
21 | 25 | use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
22 | 26 | use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; |
@@ -134,4 +138,44 @@ public function testStatusControllerWhenServerIsSuspended() |
134 | 138 | $this->assertResponseCodeEquals(200, $response); |
135 | 139 | $this->assertResponseJsonEquals(['status' => 30], $response); |
136 | 140 | } |
| 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 | + } |
137 | 181 | } |
0 commit comments