Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Illuminate\Support\Str;5use Pterodactyl\Models\User;6use Illuminate\Http\Response;7use Pterodactyl\Models\Subuser;8use Pterodactyl\Models\Permission;9use Illuminate\Foundation\Testing\WithFaker;10use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;1112class CreateServerSubuserTest extends ClientApiIntegrationTestCase13{14use WithFaker;1516/**17* Test that a subuser can be created for a server.18*/19#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]20public function testSubuserCanBeCreated(array $permissions)21{22[$user, $server] = $this->generateTestAccount($permissions);2324$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [25'email' => $email = $this->faker->email,26'permissions' => [27Permission::ACTION_USER_CREATE,28],29]);3031$response->assertOk();3233/** @var User $subuser */34$subuser = User::query()->where('email', $email)->firstOrFail();3536$response->assertJsonPath('object', Subuser::RESOURCE_NAME);37$response->assertJsonPath('attributes.uuid', $subuser->uuid);38$response->assertJsonPath('attributes.permissions', [39Permission::ACTION_USER_CREATE,40Permission::ACTION_WEBSOCKET_CONNECT,41]);4243$expected = $response->json('attributes');44unset($expected['permissions']);4546$this->assertJsonTransformedWith($expected, $subuser);47}4849/**50* Tests that an error is returned if a subuser attempts to create a new subuser and assign51* permissions that their account does not also possess.52*/53public function testErrorIsReturnedIfAssigningPermissionsNotAssignedToSelf()54{55[$user, $server] = $this->generateTestAccount([56Permission::ACTION_USER_CREATE,57Permission::ACTION_USER_READ,58Permission::ACTION_CONTROL_CONSOLE,59]);6061$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [62'email' => $this->faker->email,63'permissions' => [64Permission::ACTION_USER_CREATE,65Permission::ACTION_USER_UPDATE, // This permission is not assigned to the subuser.66],67]);6869$response->assertForbidden();70$response->assertJsonPath('errors.0.code', 'HttpForbiddenException');71$response->assertJsonPath('errors.0.detail', 'Cannot assign permissions to a subuser that your account does not actively possess.');72}7374/**75* Throws some bad data at the API and ensures that a subuser cannot be created.76*/77public function testSubuserWithExcessivelyLongEmailCannotBeCreated()78{79[$user, $server] = $this->generateTestAccount();8081$email = str_repeat(Str::random(20), 9) . '[email protected]'; // 191 is the hard limit for the column in MySQL.8283$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [84'email' => $email,85'permissions' => [86Permission::ACTION_USER_CREATE,87],88]);8990$response->assertOk();9192$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [93'email' => $email . '.au',94'permissions' => [95Permission::ACTION_USER_CREATE,96],97]);9899$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);100$response->assertJsonPath('errors.0.detail', 'The email must be between 1 and 191 characters.');101$response->assertJsonPath('errors.0.meta.source_field', 'email');102}103104/**105* Test that creating a subuser when there is already an account with that email runs106* as expected and does not create a new account.107*/108public function testCreatingSubuserWithSameEmailAsExistingUserWorks()109{110[$user, $server] = $this->generateTestAccount();111112/** @var User $existing */113$existing = User::factory()->create(['email' => $this->faker->email]);114115$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [116'email' => $existing->email,117'permissions' => [118Permission::ACTION_USER_CREATE,119],120]);121122$response->assertOk();123$response->assertJsonPath('object', Subuser::RESOURCE_NAME);124$response->assertJsonPath('attributes.uuid', $existing->uuid);125}126127/**128* Test that an error is returned if the account associated with an email address is already129* associated with the server instance.130*/131public function testAddingSubuserThatAlreadyIsAssignedReturnsError()132{133[$user, $server] = $this->generateTestAccount();134135$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [136'email' => $email = $this->faker->email,137'permissions' => [138Permission::ACTION_USER_CREATE,139],140]);141142$response->assertOk();143144$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [145'email' => $email,146'permissions' => [147Permission::ACTION_USER_CREATE,148],149]);150151$response->assertStatus(Response::HTTP_BAD_REQUEST);152$response->assertJsonPath('errors.0.code', 'ServerSubuserExistsException');153$response->assertJsonPath('errors.0.detail', 'A user with that email address is already assigned as a subuser for this server.');154}155156public static function permissionsDataProvider(): array157{158return [[[]], [[Permission::ACTION_USER_CREATE]]];159}160}161162163