-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathAccountControllerTest.php
More file actions
235 lines (192 loc) · 8.71 KB
/
AccountControllerTest.php
File metadata and controls
235 lines (192 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Pterodactyl\Tests\Integration\Api\Client;
use Illuminate\Support\Str;
use Pterodactyl\Models\User;
use Illuminate\Http\Response;
use Pterodactyl\Models\Subuser;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Hash;
use Pterodactyl\Jobs\RevokeSftpAccessJob;
class AccountControllerTest extends ClientApiIntegrationTestCase
{
/**
* Test that the user's account details are returned from the account endpoint.
*/
public function testAccountDetailsAreReturned()
{
/** @var User $user */
$user = User::factory()->create();
$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,
'first_name' => $user->name_first,
'last_name' => $user->name_last,
'language' => $user->language,
],
]);
}
/**
* Test that the user's email address can be updated via the API.
*/
public function testEmailIsUpdated()
{
$user = User::factory()->create();
$this->actingAs($user)
->putJson('/api/client/account/email', [
'email' => $email = Str::random() . '@example.com',
'password' => 'password',
])
->assertNoContent();
$this->assertActivityFor('user:account.email-changed', $user, $user);
$this->assertDatabaseHas('users', ['id' => $user->id, 'email' => $email]);
}
/**
* Tests that an email is not updated if the password provided in the request is not
* valid for the account.
*/
public function testEmailIsNotUpdatedWhenPasswordIsInvalid()
{
/** @var User $user */
$user = User::factory()->create();
$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 User $user */
$user = User::factory()->create();
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
'email' => '',
'password' => 'password',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.meta.rule', 'required');
$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);
$response->assertJsonPath('errors.0.meta.rule', 'email');
$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');
/*
* RFCs limit certain parts of an email to certain character limits.
* A limit of <= 64 for the local, then <= 63 for each domain label.
*/
$local = str_repeat(Str::random(10), 6) . '1234';
$label = str_repeat(Str::random(10), 6) . '1';
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
'email' => "1$local@$label.$label", // exceed RFC limit for local part
'password' => 'password',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');
$response->assertJsonPath('errors.0.meta.source_field', 'email');
$response = $this->actingAs($user)->putJson('/api/client/account/email', [
'email' => "$local@1234$label.$label", // exceed RFC limit for label part
'password' => 'password',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.detail', 'The email must be a valid email address.');
$response->assertJsonPath('errors.0.meta.source_field', 'email');
}
/**
* Test that the password for an account can be successfully updated.
*/
public function testPasswordIsUpdated()
{
$user = User::factory()->create();
// Assign the user to two servers, one as the owner the other as a subuser, both
// on different nodes to ensure our logic fires off correctly and the user has their
// credentials revoked on both nodes.
$server = $this->createServerModel(['owner_id' => $user->id]);
$server2 = $this->createServerModel();
Subuser::factory()->for($server2)->for($user)->create();
$initialHash = $user->password;
Bus::fake([RevokeSftpAccessJob::class]);
$this->actingAs($user)
->putJson('/api/client/account/password', [
'current_password' => 'password',
'password' => 'New_Password1',
'password_confirmation' => 'New_Password1',
])
->assertNoContent();
$user = $user->refresh();
$this->assertNotEquals($user->password, $initialHash);
$this->assertTrue(Hash::check('New_Password1', $user->password));
$this->assertFalse(Hash::check('password', $user->password));
$this->assertActivityFor('user:account.password-changed', $user, $user);
$this->assertNotEquals($server->node_id, $server2->node_id);
Bus::assertDispatchedTimes(RevokeSftpAccessJob::class, 2);
Bus::assertDispatched(fn(RevokeSftpAccessJob $job) => $job->user === $user->uuid && $job->target->is($server->node));
Bus::assertDispatched(fn(RevokeSftpAccessJob $job) => $job->user === $user->uuid && $job->target->is($server2->node));
}
/**
* Test that the password for an account is not updated if the current password is not
* provided correctly.
*/
public function testPasswordIsNotUpdatedIfCurrentPasswordIsInvalid()
{
/** @var User $user */
$user = User::factory()->create();
$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.');
}
/**
* 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()
{
$user = User::factory()->create();
$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');
}
/**
* 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 User $user */
$user = User::factory()->create();
$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);
$response->assertJsonPath('errors.0.meta.rule', 'confirmed');
$response->assertJsonPath('errors.0.detail', 'The password confirmation does not match.');
}
}