Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/Users/ExternalUserControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application\Users;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\User;
7
use Illuminate\Http\Response;
8
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
9
10
class ExternalUserControllerTest extends ApplicationApiIntegrationTestCase
11
{
12
/**
13
* Test that a user can be retrieved by their external ID.
14
*/
15
public function testGetRemoteUser()
16
{
17
$user = User::factory()->create(['external_id' => Str::random()]);
18
19
$response = $this->getJson('/api/application/users/external/' . $user->external_id);
20
$response->assertStatus(Response::HTTP_OK);
21
$response->assertJsonCount(2);
22
$response->assertJsonStructure([
23
'object',
24
'attributes' => [
25
'id', 'external_id', 'uuid', 'username', 'email', 'first_name', 'last_name',
26
'language', 'root_admin', '2fa', 'created_at', 'updated_at',
27
],
28
]);
29
30
$response->assertJson([
31
'object' => 'user',
32
'attributes' => [
33
'id' => $user->id,
34
'external_id' => $user->external_id,
35
'uuid' => $user->uuid,
36
'username' => $user->username,
37
'email' => $user->email,
38
'first_name' => $user->name_first,
39
'last_name' => $user->name_last,
40
'language' => $user->language,
41
'root_admin' => (bool) $user->root_admin,
42
'2fa' => (bool) $user->totp_enabled,
43
'created_at' => $this->formatTimestamp($user->created_at),
44
'updated_at' => $this->formatTimestamp($user->updated_at),
45
],
46
], true);
47
}
48
49
/**
50
* Test that an invalid external ID returns a 404 error.
51
*/
52
public function testGetMissingUser()
53
{
54
$response = $this->getJson('/api/application/users/external/nil');
55
$this->assertNotFoundJson($response);
56
}
57
58
/**
59
* Test that an authentication error occurs if a key does not have permission
60
* to access a resource.
61
*/
62
public function testErrorReturnedIfNoPermission()
63
{
64
$user = User::factory()->create(['external_id' => Str::random()]);
65
$this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => 0]);
66
67
$response = $this->getJson('/api/application/users/external/' . $user->external_id);
68
$this->assertAccessDeniedJson($response);
69
}
70
}
71
72