koel/tests/Feature/ProfileTest.php

99 lines
2.6 KiB
PHP
Raw Normal View History

2016-05-30 05:50:59 +00:00
<?php
2017-02-14 06:53:02 +00:00
namespace Tests\Feature;
use Illuminate\Support\Facades\Hash;
2024-01-09 18:34:40 +00:00
use Tests\TestCase;
2016-05-30 05:50:59 +00:00
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
2024-03-19 22:48:12 +00:00
use function Tests\read_as_data_url;
use function Tests\test_path;
2024-01-11 12:41:33 +00:00
2017-08-05 16:56:11 +00:00
class ProfileTest extends TestCase
2016-05-30 05:50:59 +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', [
'name' => 'Foo',
'email' => 'bar@baz.com',
2022-07-27 15:32:36 +00:00
])
->assertUnprocessable();
}
2018-08-22 19:46:36 +00:00
public function testUpdateProfileWithoutNewPassword(): void
{
2024-03-19 22:48:12 +00:00
$user = create_user(['password' => Hash::make('secret')]);
2022-07-27 15:32:36 +00:00
2022-07-27 08:49:33 +00:00
$this->putAs('api/me', [
'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
public function testUpdateProfileWithNewPassword(): void
2018-08-22 19:46:36 +00:00
{
2024-03-19 22:48:12 +00:00
$user = create_user(['password' => Hash::make('secret')]);
2022-07-27 15:32:36 +00:00
$token = $this->putAs('api/me', [
2018-08-22 19:46:36 +00:00
'name' => 'Foo',
'email' => 'bar@baz.com',
'new_password' => 'new-secret',
'current_password' => 'secret',
2022-07-27 15:32:36 +00:00
], $user)
->headers
->get('Authorization');
$user->refresh();
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
}
2024-03-19 22:48:12 +00:00
public function testUpdateProfileWithAvatar(): void
{
$user = create_user(['password' => Hash::make('secret')]);
self::assertNull($user->getRawOriginal('avatar'));
$this->putAs('api/me', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'current_password' => 'secret',
'avatar' => read_as_data_url(test_path('blobs/cover.png')),
], $user)
->assertOk();
$user->refresh();
self::assertFileExists(user_avatar_path($user->getRawOriginal('avatar')));
}
public function testUpdateProfileRemovingAvatar(): void
{
$user = create_user([
'password' => Hash::make('secret'),
'email' => 'foo@bar.com',
'avatar' => 'foo.jpg',
]);
$this->putAs('api/me', [
'name' => 'Foo',
'email' => 'foo@bar.com',
'current_password' => 'secret',
], $user)
->assertOk();
$user->refresh();
self::assertNull($user->getRawOriginal('avatar'));
}
2016-05-30 05:50:59 +00:00
}