Path: blob/1.0-develop/tests/Integration/Api/Client/AccountControllerTest.php
7459 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client;34use Illuminate\Support\Str;5use Pterodactyl\Models\User;6use Illuminate\Http\Response;7use Illuminate\Support\Facades\Hash;89class AccountControllerTest extends ClientApiIntegrationTestCase10{11/**12* Test that the user's account details are returned from the account endpoint.13*/14public function testAccountDetailsAreReturned()15{16/** @var User $user */17$user = User::factory()->create();1819$response = $this->actingAs($user)->get('/api/client/account');2021$response->assertOk()->assertJson([22'object' => 'user',23'attributes' => [24'id' => $user->id,25'admin' => false,26'username' => $user->username,27'email' => $user->email,28'first_name' => $user->name_first,29'last_name' => $user->name_last,30'language' => $user->language,31],32]);33}3435/**36* Test that the user's email address can be updated via the API.37*/38public function testEmailIsUpdated()39{40/** @var User $user */41$user = User::factory()->create();4243$response = $this->actingAs($user)->putJson('/api/client/account/email', [44'email' => $email = Str::random() . '@example.com',45'password' => 'password',46]);4748$response->assertStatus(Response::HTTP_NO_CONTENT);4950$this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email]);51}5253/**54* Tests that an email is not updated if the password provided in the request is not55* valid for the account.56*/57public function testEmailIsNotUpdatedWhenPasswordIsInvalid()58{59/** @var User $user */60$user = User::factory()->create();6162$response = $this->actingAs($user)->putJson('/api/client/account/email', [63'email' => '[email protected]',64'password' => 'invalid',65]);6667$response->assertStatus(Response::HTTP_BAD_REQUEST);68$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');69$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');70}7172/**73* Tests that an email is not updated if an invalid email address is passed through74* in the request.75*/76public function testEmailIsNotUpdatedWhenNotValid()77{78/** @var User $user */79$user = User::factory()->create();8081$response = $this->actingAs($user)->putJson('/api/client/account/email', [82'email' => '',83'password' => 'password',84]);8586$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);87$response->assertJsonPath('errors.0.meta.rule', 'required');88$response->assertJsonPath('errors.0.detail', 'The email field is required.');8990$response = $this->actingAs($user)->putJson('/api/client/account/email', [91'email' => 'invalid',92'password' => 'password',93]);9495$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);96$response->assertJsonPath('errors.0.meta.rule', 'email');97$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');98}99100/**101* Test that the password for an account can be successfully updated.102*/103public function testPasswordIsUpdated()104{105/** @var User $user */106$user = User::factory()->create();107108$initialHash = $user->password;109110$response = $this->actingAs($user)->putJson('/api/client/account/password', [111'current_password' => 'password',112'password' => 'New_Password1',113'password_confirmation' => 'New_Password1',114]);115116$user = $user->refresh();117118$this->assertNotEquals($user->password, $initialHash);119$this->assertTrue(Hash::check('New_Password1', $user->password));120$this->assertFalse(Hash::check('password', $user->password));121122$response->assertStatus(Response::HTTP_NO_CONTENT);123}124125/**126* Test that the password for an account is not updated if the current password is not127* provided correctly.128*/129public function testPasswordIsNotUpdatedIfCurrentPasswordIsInvalid()130{131/** @var User $user */132$user = User::factory()->create();133134$response = $this->actingAs($user)->putJson('/api/client/account/password', [135'current_password' => 'invalid',136'password' => 'New_Password1',137'password_confirmation' => 'New_Password1',138]);139140$response->assertStatus(Response::HTTP_BAD_REQUEST);141$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');142$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');143}144145/**146* Test that a validation error is returned to the user if no password is provided or if147* the password is below the minimum password length.148*/149public function testErrorIsReturnedForInvalidRequestData()150{151$user = User::factory()->create();152153$this->actingAs($user)->putJson('/api/client/account/password', [154'current_password' => 'password',155])156->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)157->assertJsonPath('errors.0.meta.rule', 'required');158159$this->actingAs($user)->putJson('/api/client/account/password', [160'current_password' => 'password',161'password' => 'pass',162'password_confirmation' => 'pass',163])164->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)165->assertJsonPath('errors.0.meta.rule', 'min');166}167168/**169* Test that a validation error is returned if the password passed in the request170* does not have a confirmation, or the confirmation is not the same as the password.171*/172public function testErrorIsReturnedIfPasswordIsNotConfirmed()173{174/** @var User $user */175$user = User::factory()->create();176177$response = $this->actingAs($user)->putJson('/api/client/account/password', [178'current_password' => 'password',179'password' => 'New_Password1',180'password_confirmation' => 'Invalid_New_Password',181]);182183$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);184$response->assertJsonPath('errors.0.meta.rule', 'confirmed');185$response->assertJsonPath('errors.0.detail', 'The password confirmation does not match.');186}187}188189190