user profiles
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Role;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake('public');
|
||||
});
|
||||
|
||||
test('users can view a profile', function () {
|
||||
$user = User::factory()->create([
|
||||
'name' => 'John Doe',
|
||||
'bio' => 'Test bio',
|
||||
]);
|
||||
|
||||
$response = $this->get(route('profile.show', $user));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('John Doe');
|
||||
$response->assertSee('Test bio');
|
||||
});
|
||||
|
||||
test('authenticated users can view their edit profile page', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get(route('profile.edit'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Edit Profile');
|
||||
});
|
||||
|
||||
test('guests cannot view edit profile page', function () {
|
||||
$response = $this->get(route('profile.edit'));
|
||||
|
||||
$response->assertRedirect(route('login'));
|
||||
});
|
||||
|
||||
test('users can update their profile', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => 'Updated Name',
|
||||
'email' => $user->email,
|
||||
'bio' => 'Updated bio',
|
||||
'website' => 'https://example.com',
|
||||
'twitter' => 'twitter_handle',
|
||||
'github' => 'github_handle',
|
||||
'linkedin' => 'linkedin_handle',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('profile.show', $user));
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
$user->refresh();
|
||||
expect($user->name)->toBe('Updated Name');
|
||||
expect($user->bio)->toBe('Updated bio');
|
||||
expect($user->website)->toBe('https://example.com');
|
||||
expect($user->twitter)->toBe('twitter_handle');
|
||||
});
|
||||
|
||||
test('users can upload an avatar', function () {
|
||||
$user = User::factory()->create();
|
||||
$file = UploadedFile::fake()->image('avatar.jpg');
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar' => $file,
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('profile.show', $user));
|
||||
|
||||
$user->refresh();
|
||||
expect($user->avatar)->not->toBeNull();
|
||||
Storage::disk('public')->assertExists('avatars/' . $user->avatar);
|
||||
});
|
||||
|
||||
test('avatar must be an image', function () {
|
||||
$user = User::factory()->create();
|
||||
$file = UploadedFile::fake()->create('document.pdf');
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar' => $file,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('avatar');
|
||||
});
|
||||
|
||||
test('avatar cannot exceed 2MB', function () {
|
||||
$user = User::factory()->create();
|
||||
$file = UploadedFile::fake()->image('avatar.jpg')->size(3000);
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'avatar' => $file,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('avatar');
|
||||
});
|
||||
|
||||
test('users can delete their avatar', function () {
|
||||
Storage::fake('public');
|
||||
$user = User::factory()->create(['avatar' => 'test_avatar.jpg']);
|
||||
Storage::disk('public')->put('avatars/test_avatar.jpg', 'fake content');
|
||||
|
||||
$response = $this->actingAs($user)->delete(route('profile.avatar.delete'));
|
||||
|
||||
$response->assertRedirect(route('profile.edit'));
|
||||
$response->assertSessionHas('success');
|
||||
|
||||
$user->refresh();
|
||||
expect($user->avatar)->toBeNull();
|
||||
Storage::disk('public')->assertMissing('avatars/test_avatar.jpg');
|
||||
});
|
||||
|
||||
test('email must be unique when updating profile', function () {
|
||||
$existingUser = User::factory()->create(['email' => 'existing@example.com']);
|
||||
$user = User::factory()->create(['email' => 'user@example.com']);
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => $user->name,
|
||||
'email' => 'existing@example.com',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('email');
|
||||
});
|
||||
|
||||
test('website must be a valid url', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->put(route('profile.update'), [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
'website' => 'not-a-valid-url',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('website');
|
||||
});
|
||||
@@ -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();
|
||||
});
|
||||
+6
-1
@@ -6,5 +6,10 @@ use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
//
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->withoutVite();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user