2020-06-26 05:12:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client;
|
|
|
|
|
2022-05-29 21:52:14 +00:00
|
|
|
use Illuminate\Support\Str;
|
2020-06-26 05:12:09 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Http\Response;
|
2021-08-16 00:37:12 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2020-06-26 05:12:09 +00:00
|
|
|
|
2020-06-27 19:04:41 +00:00
|
|
|
class AccountControllerTest extends ClientApiIntegrationTestCase
|
2020-06-26 05:12:09 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test that the user's account details are returned from the account endpoint.
|
|
|
|
*/
|
|
|
|
public function testAccountDetailsAreReturned()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->get('/api/client/account');
|
|
|
|
|
|
|
|
$response->assertOk()->assertJson([
|
|
|
|
'object' => 'user',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $user->id,
|
|
|
|
'admin' => false,
|
|
|
|
'username' => $user->username,
|
|
|
|
'email' => $user->email,
|
|
|
|
'language' => $user->language,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the user's email address can be updated via the API.
|
|
|
|
*/
|
|
|
|
public function testEmailIsUpdated()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
|
2022-05-29 21:52:14 +00:00
|
|
|
'email' => $email = Str::random() . '@example.com',
|
2020-06-26 05:12:09 +00:00
|
|
|
'password' => 'password',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_NO_CONTENT);
|
|
|
|
|
2022-05-29 21:52:14 +00:00
|
|
|
$this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email]);
|
2020-06-26 05:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Tests that an email is not updated if the password provided in the request is not
|
2020-06-26 05:12:09 +00:00
|
|
|
* valid for the account.
|
|
|
|
*/
|
|
|
|
public function testEmailIsNotUpdatedWhenPasswordIsInvalid()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
|
|
|
|
'email' => 'hodor@example.com',
|
|
|
|
'password' => 'invalid',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_BAD_REQUEST);
|
|
|
|
$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');
|
|
|
|
$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that an email is not updated if an invalid email address is passed through
|
|
|
|
* in the request.
|
|
|
|
*/
|
|
|
|
public function testEmailIsNotUpdatedWhenNotValid()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
|
|
|
|
'email' => '',
|
|
|
|
'password' => 'password',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
2020-08-28 04:19:01 +00:00
|
|
|
$response->assertJsonPath('errors.0.meta.rule', 'required');
|
2020-06-26 05:12:09 +00:00
|
|
|
$response->assertJsonPath('errors.0.detail', 'The email field is required.');
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
|
|
|
|
'email' => 'invalid',
|
|
|
|
'password' => 'password',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
2020-08-28 04:19:01 +00:00
|
|
|
$response->assertJsonPath('errors.0.meta.rule', 'email');
|
2020-06-26 05:12:09 +00:00
|
|
|
$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the password for an account can be successfully updated.
|
|
|
|
*/
|
|
|
|
public function testPasswordIsUpdated()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
2021-08-16 00:37:12 +00:00
|
|
|
$initialHash = $user->password;
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
|
|
|
|
'current_password' => 'password',
|
|
|
|
'password' => 'New_Password1',
|
|
|
|
'password_confirmation' => 'New_Password1',
|
|
|
|
]);
|
|
|
|
|
2021-08-16 00:37:12 +00:00
|
|
|
$user = $user->refresh();
|
|
|
|
|
|
|
|
$this->assertNotEquals($user->password, $initialHash);
|
|
|
|
$this->assertTrue(Hash::check('New_Password1', $user->password));
|
|
|
|
$this->assertFalse(Hash::check('password', $user->password));
|
|
|
|
|
2020-06-26 05:12:09 +00:00
|
|
|
$response->assertStatus(Response::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the password for an account is not updated if the current password is not
|
|
|
|
* provided correctly.
|
|
|
|
*/
|
|
|
|
public function testPasswordIsNotUpdatedIfCurrentPasswordIsInvalid()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
|
|
|
|
'current_password' => 'invalid',
|
|
|
|
'password' => 'New_Password1',
|
|
|
|
'password_confirmation' => 'New_Password1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_BAD_REQUEST);
|
|
|
|
$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');
|
|
|
|
$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:28:46 +00:00
|
|
|
/**
|
|
|
|
* Test that a validation error is returned to the user if no password is provided or if
|
|
|
|
* the password is below the minimum password length.
|
|
|
|
*/
|
|
|
|
public function testErrorIsReturnedForInvalidRequestData()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-11-29 21:28:46 +00:00
|
|
|
|
|
|
|
$this->actingAs($user)->putJson('/api/client/account/password', [
|
|
|
|
'current_password' => 'password',
|
|
|
|
])
|
|
|
|
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
|
|
|
|
->assertJsonPath('errors.0.meta.rule', 'required');
|
|
|
|
|
|
|
|
$this->actingAs($user)->putJson('/api/client/account/password', [
|
|
|
|
'current_password' => 'password',
|
|
|
|
'password' => 'pass',
|
|
|
|
'password_confirmation' => 'pass',
|
|
|
|
])
|
|
|
|
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
|
|
|
|
->assertJsonPath('errors.0.meta.rule', 'min');
|
|
|
|
}
|
|
|
|
|
2020-06-26 05:12:09 +00:00
|
|
|
/**
|
|
|
|
* Test that a validation error is returned if the password passed in the request
|
|
|
|
* does not have a confirmation, or the confirmation is not the same as the password.
|
|
|
|
*/
|
|
|
|
public function testErrorIsReturnedIfPasswordIsNotConfirmed()
|
|
|
|
{
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->create();
|
2020-06-26 05:12:09 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
|
|
|
|
'current_password' => 'password',
|
|
|
|
'password' => 'New_Password1',
|
|
|
|
'password_confirmation' => 'Invalid_New_Password',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
2020-08-28 04:19:01 +00:00
|
|
|
$response->assertJsonPath('errors.0.meta.rule', 'confirmed');
|
2020-06-26 05:12:09 +00:00
|
|
|
$response->assertJsonPath('errors.0.detail', 'The password confirmation does not match.');
|
|
|
|
}
|
|
|
|
}
|