mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-15 11:42:15 +01:00
Fixed permissions; Added tests for permission store
This commit is contained in:
committed by
Constantin Graf
parent
75a3404f46
commit
267adf52ca
@@ -6,7 +6,6 @@ namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Jetstream\Features;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeleteAccountTest extends TestCase
|
||||
@@ -15,31 +14,32 @@ class DeleteAccountTest extends TestCase
|
||||
|
||||
public function test_user_accounts_can_be_deleted(): void
|
||||
{
|
||||
if (! Features::hasAccountDeletionFeatures()) {
|
||||
$this->markTestSkipped('Account deletion is not enabled.');
|
||||
}
|
||||
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$response = $this->delete('/user', [
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$response->assertStatus(302);
|
||||
$this->assertNull($user->fresh());
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_before_account_can_be_deleted(): void
|
||||
{
|
||||
if (! Features::hasAccountDeletionFeatures()) {
|
||||
$this->markTestSkipped('Account deletion is not enabled.');
|
||||
}
|
||||
|
||||
$this->actingAs($user = User::factory()->create());
|
||||
// Arrange
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
// Act
|
||||
$response = $this->delete('/user', [
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
// Assert
|
||||
$this->assertNotNull($user->fresh());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use App\Service\PermissionStore;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
@@ -20,6 +21,13 @@ abstract class TestCase extends BaseTestCase
|
||||
LogFake::bind();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
// Note: It is necessary to clear the permission cache after each test, since the "scoped singletons" are not reset between tests.
|
||||
app(PermissionStore::class)->clear();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
protected function assertEqualsIdsOfEloquentCollection(array $ids, Collection $models): void
|
||||
{
|
||||
$this->assertEqualsCanonicalizing($ids, $models->pluck('id')->toArray());
|
||||
|
||||
@@ -107,7 +107,8 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
{
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:invite-placeholder',
|
||||
], true);
|
||||
'invitations:create',
|
||||
]);
|
||||
$user = User::factory()->create([
|
||||
'is_placeholder' => true,
|
||||
]);
|
||||
@@ -242,6 +243,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:invite-placeholder',
|
||||
'invitations:create',
|
||||
]);
|
||||
$otherOrganization = Organization::factory()->create();
|
||||
$user = User::factory()->create([
|
||||
@@ -265,6 +267,7 @@ class MemberEndpointTest extends ApiEndpointTestAbstract
|
||||
// Arrange
|
||||
$data = $this->createUserWithPermission([
|
||||
'members:invite-placeholder',
|
||||
'invitations:create',
|
||||
]);
|
||||
Passport::actingAs($data->user);
|
||||
|
||||
|
||||
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