|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Tests\Unit\Services\Subusers; |
| 26 | + |
| 27 | +use GuzzleHttp\Exception\RequestException; |
| 28 | +use Illuminate\Database\ConnectionInterface; |
| 29 | +use Illuminate\Log\Writer; |
| 30 | +use Mockery as m; |
| 31 | +use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; |
| 32 | +use Pterodactyl\Exceptions\DisplayException; |
| 33 | +use Pterodactyl\Models\Server; |
| 34 | +use Pterodactyl\Models\Subuser; |
| 35 | +use Pterodactyl\Services\Subusers\SubuserDeletionService; |
| 36 | +use Tests\TestCase; |
| 37 | +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; |
| 38 | + |
| 39 | +class SubuserDeletionServiceTest extends TestCase |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @var \Illuminate\Database\ConnectionInterface |
| 43 | + */ |
| 44 | + protected $connection; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface |
| 48 | + */ |
| 49 | + protected $daemonRepository; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \GuzzleHttp\Exception\RequestException |
| 53 | + */ |
| 54 | + protected $exception; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface |
| 58 | + */ |
| 59 | + protected $repository; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var \Pterodactyl\Services\Subusers\SubuserDeletionService |
| 63 | + */ |
| 64 | + protected $service; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var \Illuminate\Log\Writer |
| 68 | + */ |
| 69 | + protected $writer; |
| 70 | + |
| 71 | + /** |
| 72 | + * Setup tests. |
| 73 | + */ |
| 74 | + public function setUp() |
| 75 | + { |
| 76 | + parent::setUp(); |
| 77 | + |
| 78 | + $this->connection = m::mock(ConnectionInterface::class); |
| 79 | + $this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class); |
| 80 | + $this->exception = m::mock(RequestException::class); |
| 81 | + $this->repository = m::mock(SubuserRepositoryInterface::class); |
| 82 | + $this->writer = m::mock(Writer::class); |
| 83 | + |
| 84 | + $this->service = new SubuserDeletionService( |
| 85 | + $this->connection, |
| 86 | + $this->daemonRepository, |
| 87 | + $this->repository, |
| 88 | + $this->writer |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that a subuser is deleted correctly. |
| 94 | + */ |
| 95 | + public function testSubuserIsDeleted() |
| 96 | + { |
| 97 | + $subuser = factory(Subuser::class)->make(); |
| 98 | + $subuser->server = factory(Server::class)->make(); |
| 99 | + |
| 100 | + $this->repository->shouldReceive('getWithServerAndPermissions')->with($subuser->id)->once()->andReturn($subuser); |
| 101 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 102 | + $this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturn(1); |
| 103 | + |
| 104 | + $this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf() |
| 105 | + ->shouldReceive('setAccessServer')->with($subuser->server->uuid)->once()->andReturnSelf() |
| 106 | + ->shouldReceive('setSubuserKey')->with($subuser->daemonSecret, [])->once()->andReturnNull(); |
| 107 | + |
| 108 | + $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); |
| 109 | + |
| 110 | + $response = $this->service->handle($subuser->id); |
| 111 | + $this->assertEquals(1, $response); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test that an exception caused by the daemon is properly handled. |
| 116 | + */ |
| 117 | + public function testExceptionIsThrownIfDaemonConnectionFails() |
| 118 | + { |
| 119 | + $subuser = factory(Subuser::class)->make(); |
| 120 | + $subuser->server = factory(Server::class)->make(); |
| 121 | + |
| 122 | + $this->repository->shouldReceive('getWithServerAndPermissions')->with($subuser->id)->once()->andReturn($subuser); |
| 123 | + $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); |
| 124 | + $this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturn(1); |
| 125 | + |
| 126 | + $this->daemonRepository->shouldReceive('setNode->setAccessServer->setSubuserKey')->once()->andThrow($this->exception); |
| 127 | + |
| 128 | + $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); |
| 129 | + $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); |
| 130 | + $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); |
| 131 | + |
| 132 | + try { |
| 133 | + $this->service->handle($subuser->id); |
| 134 | + } catch (DisplayException $exception) { |
| 135 | + $this->assertEquals(trans('admin/exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED']), $exception->getMessage()); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments