v1.0.0
This commit is contained in:
46
src/tests/Feature/RoleMiddlewareTest.php
Normal file
46
src/tests/Feature/RoleMiddlewareTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
function userWithRole(string $role): User
|
||||
{
|
||||
return User::query()->create([
|
||||
'name' => ucfirst($role).' User',
|
||||
'email' => $role.'@example.com',
|
||||
'roles' => [$role],
|
||||
]);
|
||||
}
|
||||
|
||||
test('guests are redirected to login for every protected area', function (string $uri) {
|
||||
$this->get($uri)->assertRedirect('/login');
|
||||
})->with(['/client', '/operator', '/admin']);
|
||||
|
||||
test('each role can only access its own area', function () {
|
||||
$client = userWithRole('client');
|
||||
$operator = userWithRole('operator');
|
||||
$admin = userWithRole('admin');
|
||||
|
||||
$this->actingAs($client)->get('/client')->assertOk();
|
||||
$this->actingAs($client)->get('/operator')->assertForbidden();
|
||||
$this->actingAs($client)->get('/admin')->assertForbidden();
|
||||
|
||||
$this->actingAs($operator)->get('/operator')->assertOk();
|
||||
$this->actingAs($operator)->get('/client')->assertForbidden();
|
||||
$this->actingAs($operator)->get('/admin')->assertForbidden();
|
||||
|
||||
$this->actingAs($admin)->get('/admin')->assertOk();
|
||||
$this->actingAs($admin)->get('/client')->assertForbidden();
|
||||
$this->actingAs($admin)->get('/operator')->assertForbidden();
|
||||
});
|
||||
|
||||
test('a user with multiple roles can access every matching area', function () {
|
||||
$user = User::query()->create([
|
||||
'name' => 'Multi Role',
|
||||
'email' => 'multi@example.com',
|
||||
'roles' => ['operator', 'admin'],
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->get('/operator')->assertOk();
|
||||
$this->actingAs($user)->get('/admin')->assertOk();
|
||||
$this->actingAs($user)->get('/client')->assertForbidden();
|
||||
});
|
||||
Reference in New Issue
Block a user