|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * This software is licensed under the terms of the MIT license. |
| 7 | + * https://opensource.org/licenses/MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Tests\Unit\Services\DaemonKeys; |
| 11 | + |
| 12 | +use Mockery as m; |
| 13 | +use Tests\TestCase; |
| 14 | +use Illuminate\Log\Writer; |
| 15 | +use Pterodactyl\Models\Server; |
| 16 | +use Pterodactyl\Models\DaemonKey; |
| 17 | +use GuzzleHttp\Exception\RequestException; |
| 18 | +use Illuminate\Database\ConnectionInterface; |
| 19 | +use Pterodactyl\Exceptions\DisplayException; |
| 20 | +use Pterodactyl\Exceptions\PterodactylException; |
| 21 | +use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService; |
| 22 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 23 | +use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface; |
| 24 | +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; |
| 25 | + |
| 26 | +class DaemonKeyDeletionServiceTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock |
| 30 | + */ |
| 31 | + protected $connection; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock |
| 35 | + */ |
| 36 | + protected $daemonRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock |
| 40 | + */ |
| 41 | + protected $exception; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock |
| 45 | + */ |
| 46 | + protected $repository; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock |
| 50 | + */ |
| 51 | + protected $serverRepository; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService |
| 55 | + */ |
| 56 | + protected $service; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var \Illuminate\Log\Writer|\Mockery\Mock |
| 60 | + */ |
| 61 | + protected $writer; |
| 62 | + |
| 63 | + /** |
| 64 | + * Setup tests. |
| 65 | + */ |
| 66 | + public function setUp() |
| 67 | + { |
| 68 | + parent::setUp(); |
| 69 | + |
| 70 | + $this->connection = m::mock(ConnectionInterface::class); |
| 71 | + $this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class); |
| 72 | + $this->exception = m::mock(RequestException::class); |
| 73 | + $this->repository = m::mock(DaemonKeyRepositoryInterface::class); |
| 74 | + $this->serverRepository = m::mock(ServerRepositoryInterface::class); |
| 75 | + $this->writer = m::mock(Writer::class); |
| 76 | + |
| 77 | + $this->service = new DaemonKeyDeletionService( |
| 78 | + $this->connection, |
| 79 | + $this->repository, |
| 80 | + $this->daemonRepository, |
| 81 | + $this->serverRepository, |
| 82 | + $this->writer |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Test that a daemon key is deleted correctly. |
| 88 | + */ |
| 89 | + public function testKeyIsDeleted() |
| 90 | + { |
| 91 | + $server = factory(Server::class)->make(); |
| 92 | + $key = factory(DaemonKey::class)->make(); |
| 93 | + |
| 94 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 95 | + $this->repository->shouldReceive('findFirstWhere')->with([ |
| 96 | + ['user_id', '=', 100], |
| 97 | + ['server_id', '=', $server->id], |
| 98 | + ])->once()->andReturn($key); |
| 99 | + |
| 100 | + $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull(); |
| 101 | + $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() |
| 102 | + ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull(); |
| 103 | + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); |
| 104 | + |
| 105 | + $this->service->handle($server, 100); |
| 106 | + $this->assertTrue(true); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test that a daemon key can be deleted when only a server ID is passed. |
| 111 | + */ |
| 112 | + public function testKeyIsDeletedIfIdIsPassedInPlaceOfModel() |
| 113 | + { |
| 114 | + $server = factory(Server::class)->make(); |
| 115 | + $key = factory(DaemonKey::class)->make(); |
| 116 | + |
| 117 | + $this->serverRepository->shouldReceive('find')->with($server->id)->once()->andReturn($server); |
| 118 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 119 | + $this->repository->shouldReceive('findFirstWhere')->with([ |
| 120 | + ['user_id', '=', 100], |
| 121 | + ['server_id', '=', $server->id], |
| 122 | + ])->once()->andReturn($key); |
| 123 | + |
| 124 | + $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull(); |
| 125 | + $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() |
| 126 | + ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull(); |
| 127 | + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); |
| 128 | + |
| 129 | + $this->service->handle($server->id, 100); |
| 130 | + $this->assertTrue(true); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Test that an exception is properly handled if thrown by guzzle. |
| 135 | + */ |
| 136 | + public function testExceptionReturnedByGuzzleIsHandled() |
| 137 | + { |
| 138 | + $server = factory(Server::class)->make(); |
| 139 | + $key = factory(DaemonKey::class)->make(); |
| 140 | + |
| 141 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 142 | + $this->repository->shouldReceive('findFirstWhere')->with([ |
| 143 | + ['user_id', '=', 100], |
| 144 | + ['server_id', '=', $server->id], |
| 145 | + ])->once()->andReturn($key); |
| 146 | + |
| 147 | + $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull(); |
| 148 | + $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->exception); |
| 149 | + $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); |
| 150 | + $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); |
| 151 | + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); |
| 152 | + |
| 153 | + try { |
| 154 | + $this->service->handle($server, 100); |
| 155 | + } catch (PterodactylException $exception) { |
| 156 | + $this->assertInstanceOf(DisplayException::class, $exception); |
| 157 | + $this->assertEquals(trans('admin/server.exceptions.daemon_exception', [ |
| 158 | + 'code' => 'E_CONN_REFUSED', |
| 159 | + ]), $exception->getMessage()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments