2018-03-05 04:35:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Application\Users;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\UserTransformer;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
|
|
|
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
|
|
|
|
|
|
|
|
class UserControllerTest extends ApplicationApiIntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test the response when requesting all users on the panel.
|
|
|
|
*/
|
|
|
|
public function testGetUsers()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2018-03-05 04:35:57 +00:00
|
|
|
|
2021-04-17 19:02:08 +00:00
|
|
|
$response = $this->getJson('/api/application/users?per_page=60');
|
2018-03-05 04:35:57 +00:00
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
|
|
|
$response->assertJsonCount(2, 'data');
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
|
|
|
'data' => [
|
2022-12-15 02:53:07 +00:00
|
|
|
['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at']],
|
|
|
|
['object', 'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at']],
|
2018-03-05 04:35:57 +00:00
|
|
|
],
|
|
|
|
'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response
|
|
|
|
->assertJson([
|
|
|
|
'object' => 'list',
|
|
|
|
'data' => [[], []],
|
|
|
|
'meta' => [
|
|
|
|
'pagination' => [
|
|
|
|
'total' => 2,
|
|
|
|
'count' => 2,
|
2021-04-17 19:02:08 +00:00
|
|
|
'per_page' => 60,
|
2018-03-05 04:35:57 +00:00
|
|
|
'current_page' => 1,
|
|
|
|
'total_pages' => 1,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->assertJsonFragment([
|
|
|
|
'object' => 'user',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $this->getApiUser()->id,
|
|
|
|
'external_id' => $this->getApiUser()->external_id,
|
|
|
|
'uuid' => $this->getApiUser()->uuid,
|
|
|
|
'username' => $this->getApiUser()->username,
|
|
|
|
'email' => $this->getApiUser()->email,
|
|
|
|
'language' => $this->getApiUser()->language,
|
2022-12-15 02:53:07 +00:00
|
|
|
'admin_role_id' => $this->getApiUser()->admin_role_id,
|
2022-10-14 16:59:20 +00:00
|
|
|
'root_admin' => $this->getApiUser()->root_admin,
|
2022-12-15 01:23:46 +00:00
|
|
|
'2fa' => $this->getApiUser()->use_totp,
|
2022-12-15 19:26:34 +00:00
|
|
|
'avatar_url' => $this->getApiUser()->avatar_url,
|
|
|
|
'role_name' => $this->getApiUser()->admin_role_name,
|
2018-03-05 04:35:57 +00:00
|
|
|
'created_at' => $this->formatTimestamp($this->getApiUser()->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($this->getApiUser()->updated_at),
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->assertJsonFragment([
|
|
|
|
'object' => 'user',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $user->id,
|
|
|
|
'external_id' => $user->external_id,
|
|
|
|
'uuid' => $user->uuid,
|
|
|
|
'username' => $user->username,
|
|
|
|
'email' => $user->email,
|
|
|
|
'language' => $user->language,
|
2022-12-15 02:53:07 +00:00
|
|
|
'admin_role_id' => $user->admin_role_id,
|
2018-03-05 04:35:57 +00:00
|
|
|
'root_admin' => (bool) $user->root_admin,
|
2022-12-15 01:23:46 +00:00
|
|
|
'2fa' => (bool) $user->use_totp,
|
2022-12-15 19:26:34 +00:00
|
|
|
'avatar_url' => $user->avatar_url,
|
|
|
|
'role_name' => $user->admin_role_name,
|
2018-03-05 04:35:57 +00:00
|
|
|
'created_at' => $this->formatTimestamp($user->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($user->updated_at),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single user.
|
|
|
|
*/
|
|
|
|
public function testGetSingleUser()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2018-03-05 04:35:57 +00:00
|
|
|
|
|
|
|
$response = $this->getJson('/api/application/users/' . $user->id);
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
|
|
|
$response->assertJsonCount(2);
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
2022-12-15 02:53:07 +00:00
|
|
|
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at'],
|
2018-03-05 04:35:57 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'user',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $user->id,
|
|
|
|
'external_id' => $user->external_id,
|
|
|
|
'uuid' => $user->uuid,
|
|
|
|
'username' => $user->username,
|
|
|
|
'email' => $user->email,
|
|
|
|
'language' => $user->language,
|
2022-12-15 02:53:07 +00:00
|
|
|
'admin_role_id' => $user->admin_role_id,
|
2018-03-05 04:35:57 +00:00
|
|
|
'root_admin' => (bool) $user->root_admin,
|
2022-12-15 02:53:07 +00:00
|
|
|
'2fa' => (bool) $user->use_totp,
|
2022-12-15 19:26:34 +00:00
|
|
|
'avatar_url' => $user->avatar_url,
|
|
|
|
'role_name' => $user->admin_role_name,
|
2018-03-05 04:35:57 +00:00
|
|
|
'created_at' => $this->formatTimestamp($user->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($user->updated_at),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the correct relationships can be loaded.
|
|
|
|
*/
|
|
|
|
public function testRelationshipsCanBeLoaded()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2018-03-05 04:35:57 +00:00
|
|
|
$server = $this->createServerModel(['user_id' => $user->id]);
|
|
|
|
|
|
|
|
$response = $this->getJson('/api/application/users/' . $user->id . '?include=servers');
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
|
|
|
$response->assertJsonCount(2);
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
|
|
|
'attributes' => [
|
2022-12-15 02:53:07 +00:00
|
|
|
'id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at',
|
2018-03-05 04:35:57 +00:00
|
|
|
'relationships' => ['servers' => ['object', 'data' => [['object', 'attributes' => []]]]],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertJsonFragment([
|
|
|
|
'object' => 'list',
|
|
|
|
'data' => [
|
|
|
|
[
|
|
|
|
'object' => 'server',
|
2023-01-12 20:19:01 +00:00
|
|
|
'attributes' => (new ServerTransformer())->transform($server),
|
2018-03-05 04:35:57 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that attempting to load a relationship that the key does not have permission
|
|
|
|
* for returns a null object.
|
|
|
|
*/
|
|
|
|
public function testKeyWithoutPermissionCannotLoadRelationship()
|
|
|
|
{
|
2022-12-15 02:53:07 +00:00
|
|
|
$this->markTestSkipped('todo: implement proper admin api key permissions system');
|
2018-03-05 04:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an invalid external ID returns a 404 error.
|
|
|
|
*/
|
|
|
|
public function testGetMissingUser()
|
|
|
|
{
|
2022-11-25 20:29:04 +00:00
|
|
|
$response = $this->getJson('/api/application/users/0');
|
2018-03-05 04:35:57 +00:00
|
|
|
$this->assertNotFoundJson($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an authentication error occurs if a key does not have permission
|
|
|
|
* to access a resource.
|
|
|
|
*/
|
|
|
|
public function testErrorReturnedIfNoPermission()
|
|
|
|
{
|
2022-12-15 02:53:07 +00:00
|
|
|
$this->markTestSkipped('todo: implement proper admin api key permissions system');
|
2018-03-05 04:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user can be created.
|
|
|
|
*/
|
|
|
|
public function testCreateUser()
|
|
|
|
{
|
|
|
|
$response = $this->postJson('/api/application/users', [
|
|
|
|
'username' => 'testuser',
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_CREATED);
|
2023-01-12 20:19:01 +00:00
|
|
|
$response->assertJsonCount(2);
|
2018-03-05 04:35:57 +00:00
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
2022-12-15 02:53:07 +00:00
|
|
|
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at'],
|
2018-03-05 04:35:57 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('users', ['username' => 'testuser', 'email' => 'test@example.com']);
|
|
|
|
|
|
|
|
$user = User::where('username', 'testuser')->first();
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'user',
|
2023-01-12 20:19:01 +00:00
|
|
|
'attributes' => (new UserTransformer())->transform($user),
|
2018-03-05 04:35:57 +00:00
|
|
|
], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user can be updated.
|
|
|
|
*/
|
|
|
|
public function testUpdateUser()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2018-03-05 04:35:57 +00:00
|
|
|
|
|
|
|
$response = $this->patchJson('/api/application/users/' . $user->id, [
|
|
|
|
'username' => 'new.test.name',
|
|
|
|
'email' => 'new@emailtest.com',
|
|
|
|
]);
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
|
|
|
$response->assertJsonCount(2);
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
2022-12-15 02:53:07 +00:00
|
|
|
'attributes' => ['id', 'external_id', 'uuid', 'username', 'email', 'language', 'admin_role_id', 'root_admin', '2fa', 'avatar_url', 'role_name', 'created_at', 'updated_at'],
|
2018-03-05 04:35:57 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('users', ['username' => 'new.test.name', 'email' => 'new@emailtest.com']);
|
|
|
|
$user = $user->fresh();
|
|
|
|
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'user',
|
2023-01-12 20:19:01 +00:00
|
|
|
'attributes' => (new UserTransformer())->transform($user),
|
2018-03-05 04:35:57 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user can be deleted from the database.
|
|
|
|
*/
|
|
|
|
public function testDeleteUser()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2018-03-05 04:35:57 +00:00
|
|
|
$this->assertDatabaseHas('users', ['id' => $user->id]);
|
|
|
|
|
|
|
|
$response = $this->delete('/api/application/users/' . $user->id);
|
|
|
|
$response->assertStatus(Response::HTTP_NO_CONTENT);
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('users', ['id' => $user->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an API key without write permissions cannot create, update, or
|
|
|
|
* delete a user model.
|
|
|
|
*
|
|
|
|
* @dataProvider userWriteEndpointsDataProvider
|
|
|
|
*/
|
|
|
|
public function testApiKeyWithoutWritePermissions(string $method, string $url)
|
|
|
|
{
|
2022-12-15 02:53:07 +00:00
|
|
|
$this->markTestSkipped('todo: implement proper admin api key permissions system');
|
2018-03-05 04:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints that should return a 403 error when the key does not have write
|
|
|
|
* permissions for user management.
|
|
|
|
*/
|
2023-02-23 19:30:16 +00:00
|
|
|
public static function userWriteEndpointsDataProvider(): array
|
2018-03-05 04:35:57 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['postJson', '/api/application/users'],
|
|
|
|
['patchJson', '/api/application/users/{id}'],
|
|
|
|
['delete', '/api/application/users/{id}'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|