|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Application\Users; |
| 4 | + |
| 5 | +use Pterodactyl\Models\User; |
| 6 | +use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase; |
| 7 | + |
| 8 | +class ExternalUserControllerTest extends ApplicationApiIntegrationTestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Test that a user can be retrieved by their external ID. |
| 12 | + */ |
| 13 | + public function testGetRemoteUser() |
| 14 | + { |
| 15 | + $user = factory(User::class)->create(); |
| 16 | + |
| 17 | + $response = $this->json('GET', '/api/application/users/external/' . $user->external_id); |
| 18 | + $response->assertStatus(200); |
| 19 | + $response->assertJsonCount(2); |
| 20 | + $response->assertJsonStructure([ |
| 21 | + 'object', |
| 22 | + 'attributes' => [ |
| 23 | + 'id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', |
| 24 | + 'language', 'root_admin', '2fa', 'created_at', 'updated_at', |
| 25 | + ], |
| 26 | + ]); |
| 27 | + |
| 28 | + $response->assertJson([ |
| 29 | + 'object' => 'user', |
| 30 | + 'attributes' => [ |
| 31 | + 'id' => $user->id, |
| 32 | + 'external_id' => $user->external_id, |
| 33 | + 'uuid' => $user->uuid, |
| 34 | + 'username' => $user->username, |
| 35 | + 'email' => $user->email, |
| 36 | + 'first_name' => $user->name_first, |
| 37 | + 'last_name' => $user->name_last, |
| 38 | + 'language' => $user->language, |
| 39 | + 'root_admin' => (bool) $user->root_admin, |
| 40 | + '2fa' => (bool) $user->totp_enabled, |
| 41 | + 'created_at' => $this->formatTimestamp($user->created_at), |
| 42 | + 'updated_at' => $this->formatTimestamp($user->updated_at), |
| 43 | + ], |
| 44 | + ], true); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test that an invalid external ID returns a 404 error. |
| 49 | + */ |
| 50 | + public function testGetMissingLocation() |
| 51 | + { |
| 52 | + $response = $this->json('GET', '/api/application/users/external/nil'); |
| 53 | + $this->assertNotFoundJson($response); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test that an authentication error occurs if a key does not have permission |
| 58 | + * to access a resource. |
| 59 | + */ |
| 60 | + public function testErrorReturnedIfNoPermission() |
| 61 | + { |
| 62 | + $user = factory(User::class)->create(); |
| 63 | + $this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => 0]); |
| 64 | + |
| 65 | + $response = $this->json('GET', '/api/application/users/external/' . $user->external_id); |
| 66 | + $this->assertAccessDeniedJson($response); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test that a users's existence is not exposed unless an API key has permission |
| 71 | + * to access the resource. |
| 72 | + */ |
| 73 | + public function testResourceIsNotExposedWithoutPermissions() |
| 74 | + { |
| 75 | + $this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => 0]); |
| 76 | + |
| 77 | + $response = $this->json('GET', '/api/application/users/external/nil'); |
| 78 | + $this->assertAccessDeniedJson($response); |
| 79 | + } |
| 80 | +} |
0 commit comments