Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/Users/UserControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application\Users;
4
5
use Pterodactyl\Models\User;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Services\Acl\Api\AdminAcl;
8
use Pterodactyl\Transformers\Api\Application\UserTransformer;
9
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
10
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
11
12
class UserControllerTest extends ApplicationApiIntegrationTestCase
13
{
14
/**
15
* Test the response when requesting all users on the panel.
16
*/
17
public function testGetUsers()
18
{
19
$user = User::factory()->create();
20
21
$response = $this->getJson('/api/application/users?per_page=60');
22
$response->assertStatus(Response::HTTP_OK);
23
$response->assertJsonCount(2, 'data');
24
$response->assertJsonStructure([
25
'object',
26
'data' => [
27
['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at']],
28
['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at']],
29
],
30
'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],
31
]);
32
33
$response
34
->assertJson([
35
'object' => 'list',
36
'data' => [[], []],
37
'meta' => [
38
'pagination' => [
39
'total' => 2,
40
'count' => 2,
41
'per_page' => 60,
42
'current_page' => 1,
43
'total_pages' => 1,
44
],
45
],
46
])
47
->assertJsonFragment([
48
'object' => 'user',
49
'attributes' => [
50
'id' => $this->getApiUser()->id,
51
'external_id' => $this->getApiUser()->external_id,
52
'uuid' => $this->getApiUser()->uuid,
53
'username' => $this->getApiUser()->username,
54
'email' => $this->getApiUser()->email,
55
'first_name' => $this->getApiUser()->name_first,
56
'last_name' => $this->getApiUser()->name_last,
57
'language' => $this->getApiUser()->language,
58
'root_admin' => $this->getApiUser()->root_admin,
59
'2fa' => (bool) $this->getApiUser()->totp_enabled,
60
'created_at' => $this->formatTimestamp($this->getApiUser()->created_at),
61
'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at),
62
],
63
])
64
->assertJsonFragment([
65
'object' => 'user',
66
'attributes' => [
67
'id' => $user->id,
68
'external_id' => $user->external_id,
69
'uuid' => $user->uuid,
70
'username' => $user->username,
71
'email' => $user->email,
72
'first_name' => $user->name_first,
73
'last_name' => $user->name_last,
74
'language' => $user->language,
75
'root_admin' => (bool) $user->root_admin,
76
'2fa' => (bool) $user->totp_enabled,
77
'created_at' => $this->formatTimestamp($user->created_at),
78
'updated_at' => $this->formatTimestamp($user->updated_at),
79
],
80
]);
81
}
82
83
/**
84
* Test getting a single user.
85
*/
86
public function testGetSingleUser()
87
{
88
$user = User::factory()->create();
89
90
$response = $this->getJson('/api/application/users/' . $user->id);
91
$response->assertStatus(Response::HTTP_OK);
92
$response->assertJsonCount(2);
93
$response->assertJsonStructure([
94
'object',
95
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at'],
96
]);
97
98
$response->assertJson([
99
'object' => 'user',
100
'attributes' => [
101
'id' => $user->id,
102
'external_id' => $user->external_id,
103
'uuid' => $user->uuid,
104
'username' => $user->username,
105
'email' => $user->email,
106
'first_name' => $user->name_first,
107
'last_name' => $user->name_last,
108
'language' => $user->language,
109
'root_admin' => (bool) $user->root_admin,
110
'2fa' => (bool) $user->totp_enabled,
111
'created_at' => $this->formatTimestamp($user->created_at),
112
'updated_at' => $this->formatTimestamp($user->updated_at),
113
],
114
]);
115
}
116
117
/**
118
* Test that the correct relationships can be loaded.
119
*/
120
public function testRelationshipsCanBeLoaded()
121
{
122
$user = User::factory()->create();
123
$server = $this->createServerModel(['user_id' => $user->id]);
124
125
$response = $this->getJson('/api/application/users/' . $user->id . '?include=servers');
126
$response->assertStatus(Response::HTTP_OK);
127
$response->assertJsonCount(2);
128
$response->assertJsonStructure([
129
'object',
130
'attributes' => [
131
'id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at',
132
'relationships' => ['servers' => ['object', 'data' => [['object', 'attributes' => []]]]],
133
],
134
]);
135
136
$response->assertJsonFragment([
137
'object' => 'list',
138
'data' => [
139
[
140
'object' => 'server',
141
'attributes' => $this->getTransformer(ServerTransformer::class)->transform($server),
142
],
143
],
144
]);
145
}
146
147
/**
148
* Test that attempting to load a relationship that the key does not have permission
149
* for returns a null object.
150
*/
151
public function testKeyWithoutPermissionCannotLoadRelationship()
152
{
153
$this->createNewDefaultApiKey($this->getApiUser(), ['r_servers' => 0]);
154
155
$user = User::factory()->create();
156
$this->createServerModel(['user_id' => $user->id]);
157
158
$response = $this->getJson('/api/application/users/' . $user->id . '?include=servers');
159
$response->assertStatus(Response::HTTP_OK);
160
$response->assertJsonCount(2)->assertJsonCount(1, 'attributes.relationships');
161
$response->assertJsonStructure([
162
'attributes' => [
163
'relationships' => [
164
'servers' => ['object', 'attributes'],
165
],
166
],
167
]);
168
169
// Just assert that we see the expected relationship IDs in the response.
170
$response->assertJson([
171
'attributes' => [
172
'relationships' => [
173
'servers' => [
174
'object' => 'null_resource',
175
'attributes' => null,
176
],
177
],
178
],
179
]);
180
}
181
182
/**
183
* Test that an invalid external ID returns a 404 error.
184
*/
185
public function testGetMissingUser()
186
{
187
$response = $this->getJson('/api/application/users/nil');
188
$this->assertNotFoundJson($response);
189
}
190
191
/**
192
* Test that an authentication error occurs if a key does not have permission
193
* to access a resource.
194
*/
195
public function testErrorReturnedIfNoPermission()
196
{
197
$user = User::factory()->create();
198
$this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => 0]);
199
200
$response = $this->getJson('/api/application/users/' . $user->id);
201
$this->assertAccessDeniedJson($response);
202
}
203
204
/**
205
* Test that a user can be created.
206
*/
207
public function testCreateUser()
208
{
209
$response = $this->postJson('/api/application/users', [
210
'username' => 'testuser',
211
'email' => '[email protected]',
212
'first_name' => 'Test',
213
'last_name' => 'User',
214
]);
215
216
$response->assertStatus(Response::HTTP_CREATED);
217
$response->assertJsonCount(3);
218
$response->assertJsonStructure([
219
'object',
220
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at'],
221
'meta' => ['resource'],
222
]);
223
224
$this->assertDatabaseHas('users', ['username' => 'testuser', 'email' => '[email protected]']);
225
226
$user = User::where('username', 'testuser')->first();
227
$response->assertJson([
228
'object' => 'user',
229
'attributes' => $this->getTransformer(UserTransformer::class)->transform($user),
230
'meta' => [
231
'resource' => route('api.application.users.view', $user->id),
232
],
233
], true);
234
}
235
236
/**
237
* Test that a user can be updated.
238
*/
239
public function testUpdateUser()
240
{
241
$user = User::factory()->create();
242
243
$response = $this->patchJson('/api/application/users/' . $user->id, [
244
'username' => 'new.test.name',
245
'email' => '[email protected]',
246
'first_name' => $user->name_first,
247
'last_name' => $user->name_last,
248
]);
249
$response->assertStatus(Response::HTTP_OK);
250
$response->assertJsonCount(2);
251
$response->assertJsonStructure([
252
'object',
253
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name', 'language', 'root_admin', '2fa', 'created_at', 'updated_at'],
254
]);
255
256
$this->assertDatabaseHas('users', ['username' => 'new.test.name', 'email' => '[email protected]']);
257
$user = $user->fresh();
258
259
$response->assertJson([
260
'object' => 'user',
261
'attributes' => $this->getTransformer(UserTransformer::class)->transform($user),
262
]);
263
}
264
265
/**
266
* Test that a user can be deleted from the database.
267
*/
268
public function testDeleteUser()
269
{
270
$user = User::factory()->create();
271
$this->assertDatabaseHas('users', ['id' => $user->id]);
272
273
$response = $this->delete('/api/application/users/' . $user->id);
274
$response->assertStatus(Response::HTTP_NO_CONTENT);
275
276
$this->assertDatabaseMissing('users', ['id' => $user->id]);
277
}
278
279
/**
280
* Test that an API key without write permissions cannot create, update, or
281
* delete a user model.
282
*/
283
#[\PHPUnit\Framework\Attributes\DataProvider('userWriteEndpointsDataProvider')]
284
public function testApiKeyWithoutWritePermissions(string $method, string $url)
285
{
286
$this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => AdminAcl::READ]);
287
288
if (str_contains($url, '{id}')) {
289
$user = User::factory()->create();
290
$url = str_replace('{id}', $user->id, $url);
291
}
292
293
$response = $this->$method($url);
294
$this->assertAccessDeniedJson($response);
295
}
296
297
/**
298
* Endpoints that should return a 403 error when the key does not have write
299
* permissions for user management.
300
*/
301
public static function userWriteEndpointsDataProvider(): array
302
{
303
return [
304
['postJson', '/api/application/users'],
305
['patchJson', '/api/application/users/{id}'],
306
['delete', '/api/application/users/{id}'],
307
];
308
}
309
}
310
311