koel/tests/Feature/UserTest.php

110 lines
2.8 KiB
PHP
Raw Normal View History

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;
use Mockery\MockInterface;
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
/** @var MockInterface */
private $hash;
2019-07-22 07:03:23 +00:00
public function setUp(): void
2018-08-22 19:40:04 +00:00
{
parent::setUp();
$this->hash = $this->mockIocDependency(Hasher::class);
}
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',
])->seeStatusCode(403);
}
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', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
], factory(User::class, 'admin')->create());
2015-12-13 04:42:28 +00:00
2018-08-22 19:40:04 +00:00
self::seeInDatabase('users', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'hashed',
]);
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 */
$user = factory(User::class)->create([
'name' => 'John',
'email' => 'john@doe.com',
'password' => 'nope',
]);
$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}", [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
], factory(User::class, 'admin')->create());
2015-12-13 04:42:28 +00:00
2018-08-22 19:40:04 +00:00
self::seeInDatabase('users', [
'id' => $user->id,
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'hashed',
]);
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
{
$user = factory(User::class)->create();
2015-12-15 10:32:41 +00:00
$admin = factory(User::class, 'admin')->create();
2016-09-26 06:30:00 +00:00
$this->deleteAsUser("api/user/{$user->id}", [], $admin)
2015-12-15 10:32:41 +00:00
->notSeeInDatabase('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
{
$admin = factory(User::class, '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)
2015-12-15 10:32:41 +00:00
->seeStatusCode(403)
->seeInDatabase('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
{
$user = factory(User::class)->create();
$this->assertNull($user->getPreference('foo'));
$user->setPreference('foo', 'bar');
$this->assertEquals('bar', $user->getPreference('foo'));
$user->deletePreference('foo');
$this->assertNull($user->getPreference('foo'));
}
2015-12-13 04:42:28 +00:00
}