Skip to content

Commit 47697e0

Browse files
committed
Add test coverage for creating a subuser
1 parent cc6f98c commit 47697e0

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
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+
* @param array $permissions
21+
* @dataProvider permissionsDataProvider
22+
*/
23+
public function testSubuserCanBeCreated($permissions)
24+
{
25+
[$user, $server] = $this->generateTestAccount($permissions);
26+
27+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
28+
'email' => $email = $this->faker->email,
29+
'permissions' => [
30+
Permission::ACTION_USER_CREATE,
31+
],
32+
]);
33+
34+
$response->assertOk();
35+
36+
/** @var \Pterodactyl\Models\User $subuser */
37+
$subuser = User::query()->where('email', $email)->firstOrFail();
38+
39+
$response->assertJsonPath('object', Subuser::RESOURCE_NAME);
40+
$response->assertJsonPath('attributes.uuid', $subuser->uuid);
41+
$response->assertJsonPath('attributes.permissions', [
42+
Permission::ACTION_USER_CREATE,
43+
Permission::ACTION_WEBSOCKET_CONNECT,
44+
]);
45+
46+
$expected = $response->json('attributes');
47+
unset($expected['permissions']);
48+
49+
$this->assertJsonTransformedWith($expected, $subuser);
50+
}
51+
52+
/**
53+
* Tests that an error is returned if a subuser attempts to create a new subuser and assign
54+
* permissions that their account does not also possess.
55+
*/
56+
public function testErrorIsReturnedIfAssigningPermissionsNotAssignedToSelf()
57+
{
58+
[$user, $server] = $this->generateTestAccount([
59+
Permission::ACTION_USER_CREATE,
60+
Permission::ACTION_USER_READ,
61+
Permission::ACTION_CONTROL_CONSOLE,
62+
]);
63+
64+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
65+
'email' => $email = $this->faker->email,
66+
'permissions' => [
67+
Permission::ACTION_USER_CREATE,
68+
Permission::ACTION_USER_UPDATE, // This permission is not assigned to the subuser.
69+
],
70+
]);
71+
72+
$response->assertForbidden();
73+
$response->assertJsonPath('errors.0.code', 'HttpForbiddenException');
74+
$response->assertJsonPath('errors.0.detail', 'Cannot assign permissions to a subuser that your account does not actively possess.');
75+
}
76+
77+
/**
78+
* Throws some bad data at the API and ensures that a subuser cannot be created.
79+
*/
80+
public function testSubuserWithExcessivelyLongEmailCannotBeCreated()
81+
{
82+
[$user, $server] = $this->generateTestAccount();
83+
84+
$email = str_repeat(Str::random(20), 9) . '1@gmail.com'; // 191 is the hard limit for the column in MySQL.
85+
86+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
87+
'email' => $email,
88+
'permissions' => [
89+
Permission::ACTION_USER_CREATE,
90+
],
91+
]);
92+
93+
$response->assertOk();
94+
95+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
96+
'email' => $email . '.au',
97+
'permissions' => [
98+
Permission::ACTION_USER_CREATE,
99+
],
100+
]);
101+
102+
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
103+
$response->assertJsonPath('errors.0.detail', 'The email must be between 1 and 191 characters.');
104+
$response->assertJsonPath('errors.0.meta.source_field', 'email');
105+
}
106+
107+
/**
108+
* Test that creating a subuser when there is already an account with that email runs
109+
* as expected and does not create a new account.
110+
*/
111+
public function testCreatingSubuserWithSameEmailAsExistingUserWorks()
112+
{
113+
[$user, $server] = $this->generateTestAccount();
114+
115+
/** @var \Pterodactyl\Models\User $existing */
116+
$existing = factory(User::class)->create(['email' => $this->faker->email]);
117+
118+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
119+
'email' => $existing->email,
120+
'permissions' => [
121+
Permission::ACTION_USER_CREATE,
122+
],
123+
]);
124+
125+
$response->assertOk();
126+
$response->assertJsonPath('object', Subuser::RESOURCE_NAME);
127+
$response->assertJsonPath('attributes.uuid', $existing->uuid);
128+
}
129+
130+
/**
131+
* Test that an error is returned if the account associated with an email address is already
132+
* associated with the server instance.
133+
*/
134+
public function testAddingSubuserThatAlreadyIsAssignedReturnsError()
135+
{
136+
[$user, $server] = $this->generateTestAccount();
137+
138+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
139+
'email' => $email = $this->faker->email,
140+
'permissions' => [
141+
Permission::ACTION_USER_CREATE,
142+
],
143+
]);
144+
145+
$response->assertOk();
146+
147+
$response = $this->actingAs($user)->postJson($this->link($server) . "/users", [
148+
'email' => $email,
149+
'permissions' => [
150+
Permission::ACTION_USER_CREATE,
151+
],
152+
]);
153+
154+
$response->assertStatus(Response::HTTP_BAD_REQUEST);
155+
$response->assertJsonPath('errors.0.code', 'ServerSubuserExistsException');
156+
$response->assertJsonPath('errors.0.detail', 'A user with that email address is already assigned as a subuser for this server.');
157+
}
158+
159+
/**
160+
* @return array
161+
*/
162+
public function permissionsDataProvider(): array
163+
{
164+
return [[[]], [[Permission::ACTION_USER_CREATE]]];
165+
}
166+
}

0 commit comments

Comments
 (0)