Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\User;
7
use Illuminate\Http\Response;
8
use Pterodactyl\Models\Subuser;
9
use Pterodactyl\Models\Permission;
10
use Illuminate\Foundation\Testing\WithFaker;
11
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
12
13
class CreateServerSubuserTest extends ClientApiIntegrationTestCase
14
{
15
use WithFaker;
16
17
/**
18
* Test that a subuser can be created for a server.
19
*/
20
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
21
public function testSubuserCanBeCreated(array $permissions)
22
{
23
[$user, $server] = $this->generateTestAccount($permissions);
24
25
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
26
'email' => $email = $this->faker->email,
27
'permissions' => [
28
Permission::ACTION_USER_CREATE,
29
],
30
]);
31
32
$response->assertOk();
33
34
/** @var User $subuser */
35
$subuser = User::query()->where('email', $email)->firstOrFail();
36
37
$response->assertJsonPath('object', Subuser::RESOURCE_NAME);
38
$response->assertJsonPath('attributes.uuid', $subuser->uuid);
39
$response->assertJsonPath('attributes.permissions', [
40
Permission::ACTION_USER_CREATE,
41
Permission::ACTION_WEBSOCKET_CONNECT,
42
]);
43
44
$expected = $response->json('attributes');
45
unset($expected['permissions']);
46
47
$this->assertJsonTransformedWith($expected, $subuser);
48
}
49
50
/**
51
* Tests that an error is returned if a subuser attempts to create a new subuser and assign
52
* permissions that their account does not also possess.
53
*/
54
public function testErrorIsReturnedIfAssigningPermissionsNotAssignedToSelf()
55
{
56
[$user, $server] = $this->generateTestAccount([
57
Permission::ACTION_USER_CREATE,
58
Permission::ACTION_USER_READ,
59
Permission::ACTION_CONTROL_CONSOLE,
60
]);
61
62
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
63
'email' => $this->faker->email,
64
'permissions' => [
65
Permission::ACTION_USER_CREATE,
66
Permission::ACTION_USER_UPDATE, // This permission is not assigned to the subuser.
67
],
68
]);
69
70
$response->assertForbidden();
71
$response->assertJsonPath('errors.0.code', 'HttpForbiddenException');
72
$response->assertJsonPath('errors.0.detail', 'Cannot assign permissions to a subuser that your account does not actively possess.');
73
}
74
75
/**
76
* Throws some bad data at the API and ensures that a subuser cannot be created.
77
*/
78
public function testSubuserWithExcessivelyLongEmailCannotBeCreated()
79
{
80
[$user, $server] = $this->generateTestAccount();
81
82
$email = str_repeat(Str::random(20), 9) . '[email protected]'; // 191 is the hard limit for the column in MySQL.
83
84
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
85
'email' => $email,
86
'permissions' => [
87
Permission::ACTION_USER_CREATE,
88
],
89
]);
90
91
$response->assertOk();
92
93
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
94
'email' => $email . '.au',
95
'permissions' => [
96
Permission::ACTION_USER_CREATE,
97
],
98
]);
99
100
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
101
$response->assertJsonPath('errors.0.detail', 'The email must be between 1 and 191 characters.');
102
$response->assertJsonPath('errors.0.meta.source_field', 'email');
103
}
104
105
/**
106
* Test that creating a subuser when there is already an account with that email runs
107
* as expected and does not create a new account.
108
*/
109
public function testCreatingSubuserWithSameEmailAsExistingUserWorks()
110
{
111
[$user, $server] = $this->generateTestAccount();
112
113
/** @var User $existing */
114
$existing = User::factory()->create(['email' => $this->faker->email]);
115
116
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
117
'email' => $existing->email,
118
'permissions' => [
119
Permission::ACTION_USER_CREATE,
120
],
121
]);
122
123
$response->assertOk();
124
$response->assertJsonPath('object', Subuser::RESOURCE_NAME);
125
$response->assertJsonPath('attributes.uuid', $existing->uuid);
126
}
127
128
/**
129
* Test that an error is returned if the account associated with an email address is already
130
* associated with the server instance.
131
*/
132
public function testAddingSubuserThatAlreadyIsAssignedReturnsError()
133
{
134
[$user, $server] = $this->generateTestAccount();
135
136
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
137
'email' => $email = $this->faker->email,
138
'permissions' => [
139
Permission::ACTION_USER_CREATE,
140
],
141
]);
142
143
$response->assertOk();
144
145
$response = $this->actingAs($user)->postJson($this->link($server) . '/users', [
146
'email' => $email,
147
'permissions' => [
148
Permission::ACTION_USER_CREATE,
149
],
150
]);
151
152
$response->assertStatus(Response::HTTP_BAD_REQUEST);
153
$response->assertJsonPath('errors.0.code', 'ServerSubuserExistsException');
154
$response->assertJsonPath('errors.0.detail', 'A user with that email address is already assigned as a subuser for this server.');
155
}
156
157
public static function permissionsDataProvider(): array
158
{
159
return [[[]], [[Permission::ACTION_USER_CREATE]]];
160
}
161
}
162
163