mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-13 10:42:16 +01:00
Fixed permissions; Added tests for permission store
This commit is contained in:
committed by
Constantin Graf
parent
75a3404f46
commit
267adf52ca
124
tests/Unit/Service/PermissionStoreTest.php
Normal file
124
tests/Unit/Service/PermissionStoreTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user