2016-05-30 05:50:59 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2016-05-30 05:50:59 +00:00
|
|
|
use App\Models\User;
|
2021-05-21 17:14:00 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2016-05-30 05:50:59 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class ProfileTest extends TestCase
|
2016-05-30 05:50:59 +00:00
|
|
|
{
|
2021-05-21 17:14:00 +00:00
|
|
|
public function testUpdateProfileRequiresCurrentPassword(): void
|
2016-05-30 05:50:59 +00:00
|
|
|
{
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('api/me', [
|
2021-05-21 17:14:00 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
2022-07-27 15:32:36 +00:00
|
|
|
])
|
|
|
|
->assertUnprocessable();
|
2021-05-21 17:14:00 +00:00
|
|
|
}
|
2018-08-22 19:46:36 +00:00
|
|
|
|
2021-05-21 17:14:00 +00:00
|
|
|
public function testUpdateProfileWithoutNewPassword(): void
|
|
|
|
{
|
2022-07-27 15:32:36 +00:00
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create(['password' => Hash::make('secret')]);
|
|
|
|
|
2022-07-27 08:49:33 +00:00
|
|
|
$this->putAs('api/me', [
|
2021-05-21 17:14:00 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'current_password' => 'secret',
|
2022-07-27 15:32:36 +00:00
|
|
|
], $user);
|
2018-08-22 19:46:36 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$user->refresh();
|
2016-05-30 05:50:59 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
self::assertSame('Foo', $user->name);
|
|
|
|
self::assertSame('bar@baz.com', $user->email);
|
|
|
|
self::assertTrue(Hash::check('secret', $user->password));
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|
2018-08-22 19:46:36 +00:00
|
|
|
|
2021-05-21 17:14:00 +00:00
|
|
|
public function testUpdateProfileWithNewPassword(): void
|
2018-08-22 19:46:36 +00:00
|
|
|
{
|
2022-07-27 15:32:36 +00:00
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create(['password' => Hash::make('secret')]);
|
|
|
|
|
|
|
|
$token = $this->putAs('api/me', [
|
2018-08-22 19:46:36 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
2021-05-21 17:14:00 +00:00
|
|
|
'new_password' => 'new-secret',
|
|
|
|
'current_password' => 'secret',
|
2022-07-27 15:32:36 +00:00
|
|
|
], $user)
|
|
|
|
->headers
|
|
|
|
->get('Authorization');
|
|
|
|
|
|
|
|
$user->refresh();
|
2021-05-21 17:14:00 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
self::assertNotNull($token);
|
|
|
|
self::assertSame('Foo', $user->name);
|
|
|
|
self::assertSame('bar@baz.com', $user->email);
|
|
|
|
self::assertTrue(Hash::check('new-secret', $user->password));
|
2018-08-22 19:46:36 +00:00
|
|
|
}
|
2016-05-30 05:50:59 +00:00
|
|
|
}
|