user profiles
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Role;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
// Create roles
|
||||
$this->adminRole = Role::create([
|
||||
'name' => 'Admin',
|
||||
'slug' => 'admin',
|
||||
'description' => 'Full access',
|
||||
]);
|
||||
|
||||
$this->moderatorRole = Role::create([
|
||||
'name' => 'Moderator',
|
||||
'slug' => 'moderator',
|
||||
'description' => 'Moderate content',
|
||||
]);
|
||||
|
||||
$this->userRole = Role::create([
|
||||
'name' => 'User',
|
||||
'slug' => 'user',
|
||||
'description' => 'Regular user',
|
||||
]);
|
||||
});
|
||||
|
||||
test('users can be assigned roles', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect($user->roles)->toHaveCount(1);
|
||||
expect($user->roles->first()->slug)->toBe('user');
|
||||
});
|
||||
|
||||
test('user can have multiple roles', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach([$this->userRole->id, $this->moderatorRole->id]);
|
||||
|
||||
expect($user->roles)->toHaveCount(2);
|
||||
});
|
||||
|
||||
test('hasRole method works correctly', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect($user->hasRole('user'))->toBeTrue();
|
||||
expect($user->hasRole('admin'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('hasAnyRole method works correctly', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->moderatorRole);
|
||||
|
||||
expect($user->hasAnyRole(['moderator', 'admin']))->toBeTrue();
|
||||
expect($user->hasAnyRole(['user', 'admin']))->toBeFalse();
|
||||
});
|
||||
|
||||
test('isAdmin method works correctly', function () {
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect($admin->isAdmin())->toBeTrue();
|
||||
expect($user->isAdmin())->toBeFalse();
|
||||
});
|
||||
|
||||
test('isModerator method works correctly', function () {
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect($moderator->isModerator())->toBeTrue();
|
||||
expect($user->isModerator())->toBeFalse();
|
||||
});
|
||||
|
||||
test('admin middleware blocks non-admin users', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->get('/admin/dashboard');
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('admin middleware allows admin users', function () {
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
$response = $this->actingAs($admin)->get('/admin/dashboard');
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
|
||||
test('moderator middleware blocks regular users', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->get('/moderator/dashboard');
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('moderator middleware allows moderators', function () {
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
$response = $this->actingAs($moderator)->get('/moderator/dashboard');
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
|
||||
test('moderator middleware allows admins', function () {
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
$response = $this->actingAs($admin)->get('/moderator/dashboard');
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
|
||||
test('guests cannot access protected routes', function () {
|
||||
$response = $this->get('/admin/dashboard');
|
||||
$response->assertStatus(302);
|
||||
|
||||
$response = $this->get('/moderator/dashboard');
|
||||
$response->assertStatus(302);
|
||||
});
|
||||
|
||||
test('create product gate allows users moderators and admins', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect(Gate::forUser($user)->allows('create-product'))->toBeTrue();
|
||||
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
expect(Gate::forUser($moderator)->allows('create-product'))->toBeTrue();
|
||||
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
expect(Gate::forUser($admin)->allows('create-product'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('approve product gate allows only moderators and admins', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect(Gate::forUser($user)->allows('approve-product'))->toBeFalse();
|
||||
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
expect(Gate::forUser($moderator)->allows('approve-product'))->toBeTrue();
|
||||
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
expect(Gate::forUser($admin)->allows('approve-product'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('manage users gate allows only admins', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
expect(Gate::forUser($user)->allows('manage-users'))->toBeFalse();
|
||||
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
expect(Gate::forUser($moderator)->allows('manage-users'))->toBeFalse();
|
||||
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
expect(Gate::forUser($admin)->allows('manage-users'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('admin has access to all gates via gate before', function () {
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
expect(Gate::forUser($admin)->allows('create-product'))->toBeTrue();
|
||||
expect(Gate::forUser($admin)->allows('approve-product'))->toBeTrue();
|
||||
expect(Gate::forUser($admin)->allows('manage-users'))->toBeTrue();
|
||||
expect(Gate::forUser($admin)->allows('manage-roles'))->toBeTrue();
|
||||
expect(Gate::forUser($admin)->allows('manage-settings'))->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user