Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/AccountControllerTest.php
7459 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client;
4
5
use Illuminate\Support\Str;
6
use Pterodactyl\Models\User;
7
use Illuminate\Http\Response;
8
use Illuminate\Support\Facades\Hash;
9
10
class AccountControllerTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Test that the user's account details are returned from the account endpoint.
14
*/
15
public function testAccountDetailsAreReturned()
16
{
17
/** @var User $user */
18
$user = User::factory()->create();
19
20
$response = $this->actingAs($user)->get('/api/client/account');
21
22
$response->assertOk()->assertJson([
23
'object' => 'user',
24
'attributes' => [
25
'id' => $user->id,
26
'admin' => false,
27
'username' => $user->username,
28
'email' => $user->email,
29
'first_name' => $user->name_first,
30
'last_name' => $user->name_last,
31
'language' => $user->language,
32
],
33
]);
34
}
35
36
/**
37
* Test that the user's email address can be updated via the API.
38
*/
39
public function testEmailIsUpdated()
40
{
41
/** @var User $user */
42
$user = User::factory()->create();
43
44
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
45
'email' => $email = Str::random() . '@example.com',
46
'password' => 'password',
47
]);
48
49
$response->assertStatus(Response::HTTP_NO_CONTENT);
50
51
$this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email]);
52
}
53
54
/**
55
* Tests that an email is not updated if the password provided in the request is not
56
* valid for the account.
57
*/
58
public function testEmailIsNotUpdatedWhenPasswordIsInvalid()
59
{
60
/** @var User $user */
61
$user = User::factory()->create();
62
63
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
64
'email' => '[email protected]',
65
'password' => 'invalid',
66
]);
67
68
$response->assertStatus(Response::HTTP_BAD_REQUEST);
69
$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');
70
$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');
71
}
72
73
/**
74
* Tests that an email is not updated if an invalid email address is passed through
75
* in the request.
76
*/
77
public function testEmailIsNotUpdatedWhenNotValid()
78
{
79
/** @var User $user */
80
$user = User::factory()->create();
81
82
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
83
'email' => '',
84
'password' => 'password',
85
]);
86
87
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
88
$response->assertJsonPath('errors.0.meta.rule', 'required');
89
$response->assertJsonPath('errors.0.detail', 'The email field is required.');
90
91
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
92
'email' => 'invalid',
93
'password' => 'password',
94
]);
95
96
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
97
$response->assertJsonPath('errors.0.meta.rule', 'email');
98
$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');
99
}
100
101
/**
102
* Test that the password for an account can be successfully updated.
103
*/
104
public function testPasswordIsUpdated()
105
{
106
/** @var User $user */
107
$user = User::factory()->create();
108
109
$initialHash = $user->password;
110
111
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
112
'current_password' => 'password',
113
'password' => 'New_Password1',
114
'password_confirmation' => 'New_Password1',
115
]);
116
117
$user = $user->refresh();
118
119
$this->assertNotEquals($user->password, $initialHash);
120
$this->assertTrue(Hash::check('New_Password1', $user->password));
121
$this->assertFalse(Hash::check('password', $user->password));
122
123
$response->assertStatus(Response::HTTP_NO_CONTENT);
124
}
125
126
/**
127
* Test that the password for an account is not updated if the current password is not
128
* provided correctly.
129
*/
130
public function testPasswordIsNotUpdatedIfCurrentPasswordIsInvalid()
131
{
132
/** @var User $user */
133
$user = User::factory()->create();
134
135
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
136
'current_password' => 'invalid',
137
'password' => 'New_Password1',
138
'password_confirmation' => 'New_Password1',
139
]);
140
141
$response->assertStatus(Response::HTTP_BAD_REQUEST);
142
$response->assertJsonPath('errors.0.code', 'InvalidPasswordProvidedException');
143
$response->assertJsonPath('errors.0.detail', 'The password provided was invalid for this account.');
144
}
145
146
/**
147
* Test that a validation error is returned to the user if no password is provided or if
148
* the password is below the minimum password length.
149
*/
150
public function testErrorIsReturnedForInvalidRequestData()
151
{
152
$user = User::factory()->create();
153
154
$this->actingAs($user)->putJson('/api/client/account/password', [
155
'current_password' => 'password',
156
])
157
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
158
->assertJsonPath('errors.0.meta.rule', 'required');
159
160
$this->actingAs($user)->putJson('/api/client/account/password', [
161
'current_password' => 'password',
162
'password' => 'pass',
163
'password_confirmation' => 'pass',
164
])
165
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
166
->assertJsonPath('errors.0.meta.rule', 'min');
167
}
168
169
/**
170
* Test that a validation error is returned if the password passed in the request
171
* does not have a confirmation, or the confirmation is not the same as the password.
172
*/
173
public function testErrorIsReturnedIfPasswordIsNotConfirmed()
174
{
175
/** @var User $user */
176
$user = User::factory()->create();
177
178
$response = $this->actingAs($user)->putJson('/api/client/account/password', [
179
'current_password' => 'password',
180
'password' => 'New_Password1',
181
'password_confirmation' => 'Invalid_New_Password',
182
]);
183
184
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
185
$response->assertJsonPath('errors.0.meta.rule', 'confirmed');
186
$response->assertJsonPath('errors.0.detail', 'The password confirmation does not match.');
187
}
188
}
189
190