This commit is contained in:
Pig Fang 2020-06-22 10:17:08 +08:00
parent 5a42f3c44d
commit 94a28806e1
4 changed files with 17 additions and 17 deletions

View File

@ -248,20 +248,20 @@ class UserController extends Controller
return json(trans('user.profile.password.success'), 0);
case 'email':
$request->validate([
'new_email' => 'required|email',
$data = $request->validate([
'email' => 'required|email',
'password' => 'required|min:6|max:32',
]);
if (User::where('email', $request->new_email)->count() > 0) {
if (User::where('email', $data['email'])->count() > 0) {
return json(trans('user.profile.email.existed'), 1);
}
if (!$user->verifyPassword($request->input('password'))) {
if (!$user->verifyPassword($data['password'])) {
return json(trans('user.profile.email.wrong-password'), 1);
}
$user->email = $request->input('new_email');
$user->email = $data['email'];
$user->verified = false;
$user->save();

View File

@ -11,7 +11,7 @@ export default async function handler(event: Event) {
const { code, message }: ResponseBody = await post(
'/user/profile?action=email',
{
new_email: email,
email,
password,
},
)

View File

@ -30,7 +30,7 @@ test('change email', async () => {
form.dispatchEvent(event)
await flushPromises()
expect(post).toBeCalledWith('/user/profile?action=email', {
new_email: 'a@b.c',
email: 'a@b.c',
password: 'abc',
})
expect(showModal).toBeCalledWith({ mode: 'alert', text: 'w' })

View File

@ -366,38 +366,38 @@ class UserControllerTest extends TestCase
Event::fake();
$user = User::find($user->uid);
// Change email without `new_email` field
// Change email without `email` field
$this->actingAs($user)
->postJson(
'/user/profile',
['action' => 'email']
)
->assertJsonValidationErrors('new_email');
->assertJsonValidationErrors('email');
// Invalid email
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => 'not_an_email',
])->assertJsonValidationErrors('new_email');
'email' => 'not_an_email',
])->assertJsonValidationErrors('email');
// Too short current password
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => 'a@b.c',
'email' => 'a@b.c',
'password' => '1',
])->assertJsonValidationErrors('password');
// Too long current password
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => 'a@b.c',
'email' => 'a@b.c',
'password' => Str::random(33),
])->assertJsonValidationErrors('password');
// Use a duplicated email
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => $user->email,
'email' => $user->email,
'password' => '87654321',
])->assertJson([
'code' => 1,
@ -407,7 +407,7 @@ class UserControllerTest extends TestCase
// Wrong password
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => 'a@b.c',
'email' => 'a@b.c',
'password' => '7654321',
])->assertJson([
'code' => 1,
@ -417,7 +417,7 @@ class UserControllerTest extends TestCase
// Change email successfully
$this->postJson('/user/profile', [
'action' => 'email',
'new_email' => 'a@b.c',
'email' => 'a@b.c',
'password' => '87654321',
])->assertJson([
'code' => 0,
@ -428,7 +428,7 @@ class UserControllerTest extends TestCase
$this->assertEquals($uid, $user->uid);
$this->assertEquals('email', $action);
$this->assertEquals([
'new_email' => 'a@b.c',
'email' => 'a@b.c',
'password' => '87654321',
], $addition);