koel/tests/Feature/ProfileTest.php

63 lines
1.7 KiB
PHP
Raw Normal View History

2016-05-30 13:50:59 +08:00
<?php
2017-02-14 14:53:02 +08:00
namespace Tests\Feature;
2016-05-30 13:50:59 +08:00
use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
2016-05-30 13:50:59 +08:00
2017-08-05 17:56:11 +01:00
class ProfileTest extends TestCase
2016-05-30 13:50:59 +08:00
{
/** @var User */
private $user;
2018-08-22 21:46:36 +02:00
2019-07-22 09:03:23 +02:00
public function setUp(): void
2018-08-22 21:46:36 +02:00
{
parent::setUp();
2021-07-26 23:21:36 +02:00
// @phpstan-ignore-next-line
$this->user = User::factory()->create(['password' => Hash::make('secret')]);
2018-08-22 21:46:36 +02:00
}
public function testUpdateProfileRequiresCurrentPassword(): void
2016-05-30 13:50:59 +08:00
{
$this->putAsUser('api/me', [
'name' => 'Foo',
'email' => 'bar@baz.com',
], $this->user)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
}
2018-08-22 21:46:36 +02:00
public function testUpdateProfileWithoutNewPassword(): void
{
$this->putAsUser('api/me', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'current_password' => 'secret',
], $this->user);
2018-08-22 21:46:36 +02:00
$this->user->refresh();
2016-05-30 13:50:59 +08:00
self::assertSame('Foo', $this->user->name);
self::assertSame('bar@baz.com', $this->user->email);
self::assertTrue(Hash::check('secret', $this->user->password));
2016-05-30 13:50:59 +08:00
}
2018-08-22 21:46:36 +02:00
public function testUpdateProfileWithNewPassword(): void
2018-08-22 21:46:36 +02:00
{
$this->putAsUser('api/me', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'new_password' => 'new-secret',
'current_password' => 'secret',
], $this->user)
->assertJsonStructure(['token']);
2018-08-22 21:46:36 +02:00
$this->user->refresh();
self::assertSame('Foo', $this->user->name);
self::assertSame('bar@baz.com', $this->user->email);
self::assertTrue(Hash::check('new-secret', $this->user->password));
2018-08-22 21:46:36 +02:00
}
2016-05-30 13:50:59 +08:00
}