forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateSubuserTest.php
More file actions
133 lines (113 loc) · 4.33 KB
/
UpdateSubuserTest.php
File metadata and controls
133 lines (113 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Permission;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class UpdateSubuserTest extends ClientApiIntegrationTestCase
{
/**
* Test that the correct permissions are applied to the account when making updates
* to a subusers permissions.
*/
public function testCorrectPermissionsAreRequiredForUpdating()
{
[$user, $server] = $this->generateTestAccount(['user.read']);
$subuser = Subuser::factory()
->for(User::factory()->create())
->for($server)
->create([
'permissions' => ['control.start'],
]);
$this->postJson(
$endpoint = "/api/client/servers/$server->uuid/users/{$subuser->user->uuid}",
$data = [
'permissions' => [
'control.start',
'control.stop',
],
]
)
->assertUnauthorized();
$this->actingAs($subuser->user)->postJson($endpoint, $data)->assertForbidden();
$this->actingAs($user)->postJson($endpoint, $data)->assertForbidden();
$server->subusers()->where('user_id', $user->id)->update([
'permissions' => [
Permission::ACTION_USER_UPDATE,
Permission::ACTION_CONTROL_START,
Permission::ACTION_CONTROL_STOP,
],
]);
$this->postJson($endpoint, $data)->assertOk();
}
/**
* Tests that permissions for the account are updated and any extraneous values
* we don't know about are removed.
*/
public function testPermissionsAreSavedToAccount()
{
[$user, $server] = $this->generateTestAccount();
/** @var Subuser $subuser */
$subuser = Subuser::factory()
->for(User::factory()->create())
->for($server)
->create([
'permissions' => ['control.restart', 'websocket.connect', 'foo.bar'],
]);
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/{$subuser->user->uuid}", [
'permissions' => [
'control.start',
'control.stop',
'control.stop',
'foo.bar',
'power.fake',
],
])
->assertOk();
$subuser->refresh();
$this->assertEqualsCanonicalizing(
['control.start', 'control.stop', 'websocket.connect'],
$subuser->permissions
);
}
/**
* Ensure a subuser cannot assign permissions to an account that they do not have
* themselves.
*/
public function testUserCannotAssignPermissionsTheyDoNotHave()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_USER_READ, Permission::ACTION_USER_UPDATE]);
$subuser = Subuser::factory()
->for(User::factory()->create())
->for($server)
->create(['permissions' => ['foo.bar']]);
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/{$subuser->user->uuid}", [
'permissions' => [Permission::ACTION_USER_READ, Permission::ACTION_CONTROL_CONSOLE],
])
->assertForbidden();
$this->assertEqualsCanonicalizing(['foo.bar'], $subuser->refresh()->permissions);
}
/**
* Test that a user cannot update thyself.
*/
public function testUserCannotUpdateSelf()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_USER_READ, Permission::ACTION_USER_UPDATE]);
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/$user->uuid", [])
->assertForbidden();
}
/**
* Test that an error is returned if you attempt to update a subuser on a different account.
*/
public function testCannotUpdateSubuserForDifferentServer()
{
[$user, $server] = $this->generateTestAccount();
[$user2] = $this->generateTestAccount(['foo.bar']);
$this->actingAs($user)
->postJson("/api/client/servers/$server->uuid/users/$user2->uuid", [])
->assertNotFound();
}
}