2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
2017-02-14 06:53:02 +00:00
|
|
|
namespace Tests\Feature;
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Models\User;
|
2018-08-22 19:40:04 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
class UserTest extends TestCase
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-08-22 19:40:04 +00:00
|
|
|
private $hash;
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2018-08-22 19:40:04 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->hash = self::mock(Hasher::class);
|
2018-08-22 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testNonAdminCannotCreateUser(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->postAsUser('api/user', [
|
2018-08-22 19:40:04 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'password' => 'qux',
|
2020-09-06 21:20:42 +00:00
|
|
|
'is_admin' => false,
|
2020-09-06 18:21:39 +00:00
|
|
|
])->assertStatus(403);
|
2018-08-22 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testAdminCreatesUser(): void
|
2018-08-22 19:40:04 +00:00
|
|
|
{
|
|
|
|
$this->hash
|
|
|
|
->shouldReceive('make')
|
|
|
|
->once()
|
|
|
|
->with('qux')
|
|
|
|
->andReturn('hashed');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->postAsUser('api/user', [
|
2020-01-08 14:21:29 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'password' => 'qux',
|
2020-09-06 21:20:42 +00:00
|
|
|
'is_admin' => true,
|
2020-11-14 16:57:25 +00:00
|
|
|
], User::factory()->admin()->create());
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('users', [
|
2018-08-22 19:40:04 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'password' => 'hashed',
|
2020-06-13 15:19:47 +00:00
|
|
|
'is_admin' => true,
|
2018-08-22 19:40:04 +00:00
|
|
|
]);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testAdminUpdatesUser(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2018-08-22 19:40:04 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create([
|
2018-08-22 19:40:04 +00:00
|
|
|
'name' => 'John',
|
|
|
|
'email' => 'john@doe.com',
|
|
|
|
'password' => 'nope',
|
2020-06-13 15:19:47 +00:00
|
|
|
'is_admin' => true,
|
2018-08-22 19:40:04 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->hash
|
|
|
|
->shouldReceive('make')
|
|
|
|
->once()
|
|
|
|
->with('qux')
|
|
|
|
->andReturn('hashed');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->putAsUser("api/user/{$user->id}", [
|
2020-01-08 14:21:29 +00:00
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'password' => 'qux',
|
2020-06-13 15:19:47 +00:00
|
|
|
'is_admin' => false,
|
2020-11-14 16:57:25 +00:00
|
|
|
], User::factory()->admin()->create());
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertDatabaseHas('users', [
|
2018-08-22 19:40:04 +00:00
|
|
|
'id' => $user->id,
|
|
|
|
'name' => 'Foo',
|
|
|
|
'email' => 'bar@baz.com',
|
|
|
|
'password' => 'hashed',
|
2020-06-13 15:19:47 +00:00
|
|
|
'is_admin' => false,
|
2018-08-22 19:40:04 +00:00
|
|
|
]);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testAdminDeletesUser(): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User $user */
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
|
|
|
$admin = User::factory()->admin()->create();
|
2015-12-15 10:32:41 +00:00
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->deleteAsUser("api/user/{$user->id}", [], $admin);
|
|
|
|
self::assertDatabaseMissing('users', ['id' => $user->id]);
|
2018-08-22 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testSeppukuNotAllowed(): void
|
2018-08-22 19:40:04 +00:00
|
|
|
{
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User $admin */
|
2020-11-14 16:57:25 +00:00
|
|
|
$admin = User::factory()->admin()->create();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-15 10:32:41 +00:00
|
|
|
// A user can't delete himself
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->deleteAsUser("api/user/{$admin->id}", [], $admin)
|
2020-09-06 18:21:39 +00:00
|
|
|
->assertStatus(403);
|
|
|
|
|
|
|
|
self::assertDatabaseHas('users', ['id' => $admin->id]);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2018-09-04 06:25:24 +00:00
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function testUpdateUserProfile(): void
|
2018-09-04 06:25:24 +00:00
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
$user = User::factory()->create();
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertNull($user->getPreference('foo'));
|
2018-09-04 06:25:24 +00:00
|
|
|
|
|
|
|
$user->setPreference('foo', 'bar');
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertEquals('bar', $user->getPreference('foo'));
|
2018-09-04 06:25:24 +00:00
|
|
|
|
|
|
|
$user->deletePreference('foo');
|
2020-09-06 18:21:39 +00:00
|
|
|
self::assertNull($user->getPreference('foo'));
|
2018-09-04 06:25:24 +00:00
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|