|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Pterodactyl\Models\User; |
| 8 | +use Pterodactyl\Models\Subuser; |
| 9 | +use Illuminate\Pagination\LengthAwarePaginator; |
| 10 | +use Pterodactyl\Tests\Integration\IntegrationTestCase; |
| 11 | +use Pterodactyl\Http\Controllers\Admin\UserController; |
| 12 | + |
| 13 | +class UserControllerTest extends IntegrationTestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Test that the index route controller for the user listing returns the expected user |
| 17 | + * data with the number of servers they are assigned to, and the number of servers they |
| 18 | + * are a subuser of. |
| 19 | + * |
| 20 | + * @see https://github.com/pterodactyl/panel/issues/2469 |
| 21 | + */ |
| 22 | + public function testIndexReturnsExpectedData() |
| 23 | + { |
| 24 | + $unique = Str::random(16); |
| 25 | + $users = [ |
| 26 | + factory(User::class)->create(['username' => $unique . '_1']), |
| 27 | + factory(User::class)->create(['username' => $unique . '_2']), |
| 28 | + ]; |
| 29 | + |
| 30 | + $servers = [ |
| 31 | + $this->createServerModel(['owner_id' => $users[0]->id]), |
| 32 | + $this->createServerModel(['owner_id' => $users[0]->id]), |
| 33 | + $this->createServerModel(['owner_id' => $users[0]->id]), |
| 34 | + $this->createServerModel(['owner_id' => $users[1]->id]), |
| 35 | + ]; |
| 36 | + |
| 37 | + Subuser::query()->forceCreate(['server_id' => $servers[0]->id, 'user_id' => $users[1]->id]); |
| 38 | + Subuser::query()->forceCreate(['server_id' => $servers[1]->id, 'user_id' => $users[1]->id]); |
| 39 | + |
| 40 | + /** @var \Pterodactyl\Http\Controllers\Admin\UserController $controller */ |
| 41 | + $controller = $this->app->make(UserController::class); |
| 42 | + |
| 43 | + $request = Request::create('/admin/users?filter[username]=' . $unique, 'GET'); |
| 44 | + $this->app->instance(Request::class, $request); |
| 45 | + |
| 46 | + $data = $controller->index($request)->getData(); |
| 47 | + $this->assertArrayHasKey('users', $data); |
| 48 | + $this->assertInstanceOf(LengthAwarePaginator::class, $data['users']); |
| 49 | + |
| 50 | + /** @var \Pterodactyl\Models\User[] $response */ |
| 51 | + $response = $data['users']->items(); |
| 52 | + $this->assertCount(2, $response); |
| 53 | + $this->assertInstanceOf(User::class, $response[0]); |
| 54 | + $this->assertSame(3, (int)$response[0]->servers_count); |
| 55 | + $this->assertSame(0, (int)$response[0]->subuser_of_count); |
| 56 | + $this->assertSame(1, (int)$response[1]->servers_count); |
| 57 | + $this->assertSame(2, (int)$response[1]->subuser_of_count); |
| 58 | + } |
| 59 | +} |
0 commit comments