Better tests for User Controller

This commit is contained in:
Phan An 2018-08-22 21:40:04 +02:00
parent 3efcc9a049
commit 61bc21c303
2 changed files with 68 additions and 23 deletions

View file

@ -6,13 +6,20 @@ use App\Http\Requests\API\UserStoreRequest;
use App\Http\Requests\API\UserUpdateRequest;
use App\Models\User;
use Exception;
use Hash;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Hashing\Hasher as Hash;
use Illuminate\Http\JsonResponse;
use RuntimeException;
class UserController extends Controller
{
private $hash;
public function __construct(Hash $hash)
{
$this->hash = $hash;
}
/**
* Create a new user.
*
@ -27,7 +34,7 @@ class UserController extends Controller
return response()->json(User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
'password' => $this->hash->make($request->password),
]));
}
@ -46,7 +53,7 @@ class UserController extends Controller
$data = $request->only('name', 'email');
if ($request->password) {
$data['password'] = Hash::make($request->password);
$data['password'] = $this->hash->make($request->password);
}
return response()->json($user->update($data));

View file

@ -3,34 +3,64 @@
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Contracts\Hashing\Hasher;
use Mockery\MockInterface;
class UserTest extends TestCase
{
/** @test */
public function admin_can_create_a_user()
/** @var MockInterface */
private $hash;
public function setUp()
{
parent::setUp();
$this->hash = $this->mockIocDependency(Hasher::class);
}
public function testNonAdminCannotCreateUser()
{
// Non-admins can't do shit
$this->postAsUser('api/user', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
])
->seeStatusCode(403);
])->seeStatusCode(403);
}
public function testAdminCreatesUser()
{
$this->hash
->shouldReceive('make')
->once()
->with('qux')
->andReturn('hashed');
// But admins can
$this->postAsUser('api/user', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'qux',
], factory(User::class, 'admin')->create());
$this->seeInDatabase('users', ['name' => 'Foo']);
self::seeInDatabase('users', [
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'hashed',
]);
}
/** @test */
public function admin_can_update_a_user()
public function testAdminUpdatesUser()
{
$user = factory(User::class)->create();
/** @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');
$this->putAsUser("api/user/{$user->id}", [
'name' => 'Foo',
@ -38,17 +68,26 @@ class UserTest extends TestCase
'password' => 'qux',
], factory(User::class, 'admin')->create());
$this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']);
self::seeInDatabase('users', [
'id' => $user->id,
'name' => 'Foo',
'email' => 'bar@baz.com',
'password' => 'hashed',
]);
}
/** @test */
public function admin_can_delete_a_user()
public function testAdminDeletesUser()
{
$user = factory(User::class)->create();
$admin = factory(User::class, 'admin')->create();
$this->deleteAsUser("api/user/{$user->id}", [], $admin)
->notSeeInDatabase('users', ['id' => $user->id]);
}
public function testSeppukuNotAllowed()
{
$admin = factory(User::class, 'admin')->create();
// A user can't delete himself
$this->deleteAsUser("api/user/{$admin->id}", [], $admin)
@ -56,8 +95,7 @@ class UserTest extends TestCase
->seeInDatabase('users', ['id' => $admin->id]);
}
/** @test */
public function user_can_update_their_preferences()
public function testUpdateUserProfile()
{
$user = factory(User::class)->create();
$this->assertNull($user->getPreference('foo'));