85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
use App\Http\Resources\PermissionResource;
|
|
use App\Models\Permission;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Facades\Tests\Setup\UserFactory;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Tests\Feature\ApiTestCase;
|
|
|
|
test('unauthenticated', function () {
|
|
$role = Role::factory()->create();
|
|
|
|
$this->getJson("/api/roles/{$role->id}/permissions")
|
|
->assertStatus(401);
|
|
});
|
|
|
|
test('get settings without permission', function () {
|
|
$role = Role::factory()->create();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user, ApiTestCase::API_GUARD)
|
|
->getJson("/api/roles/{$role->id}/permissions")
|
|
->assertStatus(403);
|
|
});
|
|
|
|
test('get role permissions', function () {
|
|
$role = Role::factory()->create();
|
|
$permission = Permission::factory()->create();
|
|
|
|
$role->attachPermission($permission);
|
|
|
|
$this->actingAs(rolePermissionUser(), ApiTestCase::API_GUARD)
|
|
->getJson("/api/roles/{$role->id}/permissions")
|
|
->assertOk()
|
|
->assertJsonFragment(
|
|
PermissionResource::collection([$permission])->toArray(request())
|
|
);
|
|
});
|
|
|
|
test('update role permissions', function () {
|
|
$role = Role::factory()->create();
|
|
$permissions1 = Permission::factory()->times(2)->create();
|
|
$permissions2 = Permission::factory()->times(3)->create();
|
|
|
|
$role->attachPermissions($permissions1->toArray());
|
|
|
|
$this->actingAs(rolePermissionUser(), ApiTestCase::API_GUARD)
|
|
->putJson("/api/roles/{$role->id}/permissions", [
|
|
'permissions' => $permissions2->pluck('id')->toArray(),
|
|
])
|
|
->assertOk()
|
|
->assertJsonFragment(
|
|
(new PermissionResource($permissions2[0]))->toArray(
|
|
$this->createTestRequest(Request::createFromGlobals())
|
|
)
|
|
)
|
|
->assertJsonFragment(
|
|
(new PermissionResource($permissions2[1]))->toArray(
|
|
$this->createTestRequest(Request::createFromGlobals())
|
|
)
|
|
)
|
|
->assertJsonFragment(
|
|
(new PermissionResource($permissions2[2]))->toArray(
|
|
$this->createTestRequest(Request::createFromGlobals())
|
|
)
|
|
);
|
|
|
|
foreach ($permissions2 as $perm) {
|
|
$this->assertDatabaseHas('permission_role', [
|
|
'permission_id' => $perm->id,
|
|
'role_id' => $role->id,
|
|
]);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
function rolePermissionUser()
|
|
{
|
|
return UserFactory::user()->withPermissions('permissions.manage')->create();
|
|
}
|