|
| 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\Http\Controllers\Base; |
| 26 | + |
| 27 | +use Mockery as m; |
| 28 | +use Tests\TestCase; |
| 29 | +use Illuminate\Http\Request; |
| 30 | +use Pterodactyl\Models\User; |
| 31 | +use Illuminate\Http\Response; |
| 32 | +use Prologue\Alerts\AlertsMessageBag; |
| 33 | +use Tests\Assertions\ControllerAssertionsTrait; |
| 34 | +use Pterodactyl\Services\Api\KeyCreationService; |
| 35 | +use Pterodactyl\Http\Controllers\Base\APIController; |
| 36 | +use Pterodactyl\Http\Requests\Base\ApiKeyFormRequest; |
| 37 | +use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; |
| 38 | + |
| 39 | +class APIControllerTest extends TestCase |
| 40 | +{ |
| 41 | + use ControllerAssertionsTrait; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Prologue\Alerts\AlertsMessageBag |
| 45 | + */ |
| 46 | + protected $alert; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Pterodactyl\Http\Controllers\Base\APIController |
| 50 | + */ |
| 51 | + protected $controller; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Pterodactyl\Services\Api\KeyCreationService |
| 55 | + */ |
| 56 | + protected $keyService; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface |
| 60 | + */ |
| 61 | + protected $repository; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var \Illuminate\Http\Request |
| 65 | + */ |
| 66 | + protected $request; |
| 67 | + |
| 68 | + /** |
| 69 | + * Setup tests. |
| 70 | + */ |
| 71 | + public function setUp() |
| 72 | + { |
| 73 | + parent::setUp(); |
| 74 | + |
| 75 | + $this->alert = m::mock(AlertsMessageBag::class); |
| 76 | + $this->keyService = m::mock(KeyCreationService::class); |
| 77 | + $this->repository = m::mock(ApiKeyRepositoryInterface::class); |
| 78 | + $this->request = m::mock(Request::class); |
| 79 | + |
| 80 | + $this->controller = new APIController($this->alert, $this->repository, $this->keyService); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test the index controller. |
| 85 | + */ |
| 86 | + public function testIndexController() |
| 87 | + { |
| 88 | + $model = factory(User::class)->make(); |
| 89 | + |
| 90 | + $this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model); |
| 91 | + $this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(['testkeys']); |
| 92 | + |
| 93 | + $response = $this->controller->index($this->request); |
| 94 | + $this->assertViewNameEquals('base.api.index', $response); |
| 95 | + $this->assertViewHasKey('keys', $response); |
| 96 | + $this->assertViewKeyEquals('keys', ['testkeys'], $response); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Test the create API view controller. |
| 101 | + * |
| 102 | + * @dataProvider rootAdminDataProvider |
| 103 | + */ |
| 104 | + public function testCreateController($admin) |
| 105 | + { |
| 106 | + $model = factory(User::class)->make(['root_admin' => $admin]); |
| 107 | + $this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model); |
| 108 | + |
| 109 | + $response = $this->controller->create($this->request); |
| 110 | + $this->assertViewNameEquals('base.api.new', $response); |
| 111 | + $this->assertViewHasKey('permissions.user', $response); |
| 112 | + $this->assertViewHasKey('permissions.admin', $response); |
| 113 | + |
| 114 | + if ($admin) { |
| 115 | + $this->assertViewKeyNotEquals('permissions.admin', null, $response); |
| 116 | + } else { |
| 117 | + $this->assertViewKeyEquals('permissions.admin', null, $response); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test the store functionality for a non-admin user. |
| 123 | + * |
| 124 | + * @dataProvider rootAdminDataProvider |
| 125 | + */ |
| 126 | + public function testStoreController($admin) |
| 127 | + { |
| 128 | + $this->request = m::mock(ApiKeyFormRequest::class); |
| 129 | + $model = factory(User::class)->make(['root_admin' => $admin]); |
| 130 | + |
| 131 | + if ($admin) { |
| 132 | + $this->request->shouldReceive('input')->with('admin_permissions', [])->once()->andReturn(['admin.permission']); |
| 133 | + } |
| 134 | + |
| 135 | + $this->request->shouldReceive('user')->withNoArgs()->andReturn($model); |
| 136 | + $this->request->shouldReceive('input')->with('allowed_ips')->once()->andReturnNull(); |
| 137 | + $this->request->shouldReceive('input')->with('memo')->once()->andReturnNull(); |
| 138 | + $this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['test.permission']); |
| 139 | + |
| 140 | + $this->keyService->shouldReceive('handle')->with([ |
| 141 | + 'user_id' => $model->id, |
| 142 | + 'allowed_ips' => null, |
| 143 | + 'memo' => null, |
| 144 | + ], ['test.permission'], ($admin) ? ['admin.permission'] : [])->once()->andReturn('testToken'); |
| 145 | + |
| 146 | + $this->alert->shouldReceive('success')->with(trans('base.api.index.keypair_created', ['token' => 'testToken']))->once()->andReturnSelf() |
| 147 | + ->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); |
| 148 | + |
| 149 | + $response = $this->controller->store($this->request); |
| 150 | + $this->assertRouteRedirectEquals('account.api', $response); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Test the API key revocation controller. |
| 155 | + */ |
| 156 | + public function testRevokeController() |
| 157 | + { |
| 158 | + $model = factory(User::class)->make(); |
| 159 | + $this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model); |
| 160 | + |
| 161 | + $this->repository->shouldReceive('deleteWhere')->with([ |
| 162 | + ['user_id', '=', $model->id], |
| 163 | + ['public', '=', 'testKey123'], |
| 164 | + ])->once()->andReturnNull(); |
| 165 | + |
| 166 | + $response = $this->controller->revoke($this->request, 'testKey123'); |
| 167 | + $this->assertInstanceOf(Response::class, $response); |
| 168 | + $this->assertEmpty($response->getContent()); |
| 169 | + $this->assertEquals(204, $response->getStatusCode()); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Data provider to determine if a user is a root admin. |
| 174 | + * |
| 175 | + * @return array |
| 176 | + */ |
| 177 | + public function rootAdminDataProvider() |
| 178 | + { |
| 179 | + return [[0], [1]]; |
| 180 | + } |
| 181 | +} |
0 commit comments