Fixed permissions; Added tests for permission store

This commit is contained in:
Constantin Graf
2024-04-24 15:07:39 +02:00
committed by Constantin Graf
parent 75a3404f46
commit 267adf52ca
6 changed files with 170 additions and 25 deletions

View File

@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service;
use App\Models\Organization;
use App\Models\User;
use App\Service\PermissionStore;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Jetstream\Jetstream;
use Tests\TestCase;
class PermissionStoreTest extends TestCase
{
use RefreshDatabase;
public function test_has_method_returns_false_when_user_is_not_authenticated(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$organization->users()->attach($user, ['role' => 'employee']);
$permissionStore = new PermissionStore();
// Act
$result = $permissionStore->has($organization, 'permission');
// Assert
$this->assertFalse($result);
}
public function test_has_method_returns_false_when_user_does_not_belong_to_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$permissionStore = new PermissionStore();
$this->actingAs($user);
// Act
$result = $permissionStore->has($organization, 'permission');
// Assert
$this->assertFalse($result);
}
public function test_has_method_returns_false_when_user_does_not_have_permission(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$organization->users()->attach($user, ['role' => 'employee']);
$permissionStore = new PermissionStore();
$this->actingAs($user);
// Act
$result = $permissionStore->has($organization, 'permission');
// Assert
$this->assertFalse($result);
}
public function test_has_method_returns_true_when_user_has_permission(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$organization->users()->attach($user, ['role' => 'employee']);
$permissionStore = new PermissionStore();
$this->actingAs($user);
// Act
$result = $permissionStore->has($organization, 'time-entries:view:own');
// Assert
$this->assertTrue($result);
}
public function test_get_permissions_method_returns_empty_array_when_user_is_not_authenticated(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$organization->users()->attach($user, ['role' => 'employee']);
$permissionStore = new PermissionStore();
// Act
$result = $permissionStore->getPermissions($organization);
// Assert
$this->assertEmpty($result);
}
public function test_get_permissions_method_returns_empty_array_when_user_does_not_belong_to_organization(): void
{
$organization = Organization::factory()->create();
$user = User::factory()->create();
$permissionStore = new PermissionStore();
$this->actingAs($user);
// Act
$result = $permissionStore->getPermissions($organization);
// Assert
$this->assertEmpty($result);
}
public function test_get_permissions_method_returns_permissions_when_user_belongs_to_organization(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create();
$organization->users()->attach($user, ['role' => 'employee']);
$permissionStore = new PermissionStore();
$this->actingAs($user);
// Act
$result = $permissionStore->getPermissions($organization);
// Assert
$this->assertSame(Jetstream::findRole('employee')->permissions, $result);
}
}