|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Allocations; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Tests\TestCase; |
| 7 | +use Pterodactyl\Models\Server; |
| 8 | +use Pterodactyl\Models\Allocation; |
| 9 | +use Tests\Traits\MocksRequestException; |
| 10 | +use GuzzleHttp\Exception\RequestException; |
| 11 | +use Illuminate\Database\ConnectionInterface; |
| 12 | +use Pterodactyl\Exceptions\PterodactylException; |
| 13 | +use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; |
| 14 | +use Pterodactyl\Services\Allocations\SetDefaultAllocationService; |
| 15 | +use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; |
| 16 | +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; |
| 17 | +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface; |
| 18 | + |
| 19 | +class SetDefaultAllocationServiceTest extends TestCase |
| 20 | +{ |
| 21 | + use MocksRequestException; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock |
| 25 | + */ |
| 26 | + private $connection; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock |
| 30 | + */ |
| 31 | + private $daemonRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock |
| 35 | + */ |
| 36 | + private $repository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock |
| 40 | + */ |
| 41 | + private $serverRepository; |
| 42 | + |
| 43 | + /** |
| 44 | + * Setup tests. |
| 45 | + */ |
| 46 | + public function setUp() |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + |
| 50 | + $this->connection = m::mock(ConnectionInterface::class); |
| 51 | + $this->daemonRepository = m::mock(DaemonRepositoryInterface::class); |
| 52 | + $this->repository = m::mock(AllocationRepositoryInterface::class); |
| 53 | + $this->serverRepository = m::mock(ServerRepositoryInterface::class); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that an allocation can be updated. |
| 58 | + * |
| 59 | + * @dataProvider useModelDataProvider |
| 60 | + */ |
| 61 | + public function testAllocationIsUpdated(bool $useModel) |
| 62 | + { |
| 63 | + $allocations = factory(Allocation::class)->times(2)->make(); |
| 64 | + $model = factory(Server::class)->make(); |
| 65 | + if (! $useModel) { |
| 66 | + $this->serverRepository->shouldReceive('find')->with(1234)->once()->andReturn($model); |
| 67 | + } |
| 68 | + |
| 69 | + $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations); |
| 70 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 71 | + $this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf(); |
| 72 | + $this->serverRepository->shouldReceive('update')->with($model->id, [ |
| 73 | + 'allocation_id' => $allocations->first()->id, |
| 74 | + ])->once()->andReturnNull(); |
| 75 | + |
| 76 | + $this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf(); |
| 77 | + $this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); |
| 78 | + $this->daemonRepository->shouldReceive('update')->with([ |
| 79 | + 'build' => [ |
| 80 | + 'default' => [ |
| 81 | + 'ip' => $allocations->first()->ip, |
| 82 | + 'port' => $allocations->first()->port, |
| 83 | + ], |
| 84 | + 'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) { |
| 85 | + return $item->pluck('port'); |
| 86 | + })->toArray(), |
| 87 | + ], |
| 88 | + ])->once()->andReturnNull(); |
| 89 | + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); |
| 90 | + |
| 91 | + $response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id); |
| 92 | + $this->assertNotEmpty($response); |
| 93 | + $this->assertSame($allocations->first(), $response); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Test that an allocation that doesn't belong to a server throws an exception. |
| 98 | + * |
| 99 | + * @expectedException \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException |
| 100 | + */ |
| 101 | + public function testAllocationNotBelongingToServerThrowsException() |
| 102 | + { |
| 103 | + $model = factory(Server::class)->make(); |
| 104 | + $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect()); |
| 105 | + |
| 106 | + $this->getService()->handle($model, 1234); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test that an exception thrown by guzzle is handled properly. |
| 111 | + */ |
| 112 | + public function testExceptionThrownByGuzzleIsHandled() |
| 113 | + { |
| 114 | + $this->configureExceptionMock(); |
| 115 | + |
| 116 | + $allocation = factory(Allocation::class)->make(); |
| 117 | + $model = factory(Server::class)->make(); |
| 118 | + |
| 119 | + $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation])); |
| 120 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 121 | + $this->serverRepository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf(); |
| 122 | + $this->serverRepository->shouldReceive('update')->with($model->id, [ |
| 123 | + 'allocation_id' => $allocation->id, |
| 124 | + ])->once()->andReturnNull(); |
| 125 | + |
| 126 | + $this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock()); |
| 127 | + $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); |
| 128 | + |
| 129 | + try { |
| 130 | + $this->getService()->handle($model, $allocation->id); |
| 131 | + } catch (PterodactylException $exception) { |
| 132 | + $this->assertInstanceOf(DaemonConnectionException::class, $exception); |
| 133 | + $this->assertInstanceOf(RequestException::class, $exception->getPrevious()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Data provider to determine if a model should be passed or an int. |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + public function useModelDataProvider(): array |
| 143 | + { |
| 144 | + return [[false], [true]]; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Return an instance of the service with mocked dependencies. |
| 149 | + * |
| 150 | + * @return \Pterodactyl\Services\Allocations\SetDefaultAllocationService |
| 151 | + */ |
| 152 | + private function getService(): SetDefaultAllocationService |
| 153 | + { |
| 154 | + return new SetDefaultAllocationService($this->repository, $this->connection, $this->daemonRepository, $this->serverRepository); |
| 155 | + } |
| 156 | +} |
0 commit comments