From 65d6e2c454465102809c672a965a396a36aabe77 Mon Sep 17 00:00:00 2001 From: mohammad-alavi Date: Wed, 29 Sep 2021 09:16:05 +0330 Subject: [PATCH] remove db exists validation on request --- .../Requests/SyncPermissionsOnRoleRequest.php | 3 +- .../Functional/SyncPermissionsOnRoleTest.php | 54 +++++++++++++++---- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/app/Containers/AppSection/Authorization/UI/API/Requests/SyncPermissionsOnRoleRequest.php b/app/Containers/AppSection/Authorization/UI/API/Requests/SyncPermissionsOnRoleRequest.php index 5622f58c8..cea75fca4 100644 --- a/app/Containers/AppSection/Authorization/UI/API/Requests/SyncPermissionsOnRoleRequest.php +++ b/app/Containers/AppSection/Authorization/UI/API/Requests/SyncPermissionsOnRoleRequest.php @@ -34,8 +34,7 @@ public function rules(): array { return [ 'permissions_ids' => 'required', - 'permissions_ids.*' => 'exists:' . config('permission.table_names.permissions') . ',id', - 'role_id' => 'required|exists:' . config('permission.table_names.roles') . ',id', + 'role_id' => 'required', ]; } diff --git a/app/Containers/AppSection/Authorization/UI/API/Tests/Functional/SyncPermissionsOnRoleTest.php b/app/Containers/AppSection/Authorization/UI/API/Tests/Functional/SyncPermissionsOnRoleTest.php index 8db82b72b..3e41a4366 100644 --- a/app/Containers/AppSection/Authorization/UI/API/Tests/Functional/SyncPermissionsOnRoleTest.php +++ b/app/Containers/AppSection/Authorization/UI/API/Tests/Functional/SyncPermissionsOnRoleTest.php @@ -5,6 +5,8 @@ use App\Containers\AppSection\Authorization\Models\Permission; use App\Containers\AppSection\Authorization\Models\Role; use App\Containers\AppSection\Authorization\UI\API\Tests\ApiTestCase; +use Illuminate\Testing\Fluent\AssertableJson; +use Vinkla\Hashids\Facades\Hashids; /** * Class SyncPermissionsOnRoleTest. @@ -23,21 +25,55 @@ class SyncPermissionsOnRoleTest extends ApiTestCase public function testSyncDuplicatedPermissionsToRole(): void { - $permissionA = Permission::factory()->create(['display_name' => 'AAA']); - $permissionB = Permission::factory()->create(['display_name' => 'BBB']); - $roleA = Role::factory()->create(); - $roleA->givePermissionTo($permissionA); + $permissionA = Permission::factory()->create(); + $permissionB = Permission::factory()->create(); + $role = Role::factory()->create(); + $role->givePermissionTo($permissionA); $data = [ - 'role_id' => $roleA->getHashedKey(), + 'role_id' => $role->getHashedKey(), 'permissions_ids' => [$permissionA->getHashedKey(), $permissionB->getHashedKey()], ]; $response = $this->makeCall($data); $response->assertStatus(200); - $this->assertDatabaseHas(config('permission.table_names.role_has_permissions'), [ - 'permission_id' => $permissionA->id, - 'role_id' => $roleA->id, - ]); + $response->assertJson( + fn (AssertableJson $json) => + $json->has('data') + ->where('data.object', 'Role') + ->where('data.id', $role->getHashedKey()) + ->count('data.permissions.data', 2) + ->where('data.permissions.data.0.id', $permissionA->getHashedKey()) + ->where('data.permissions.data.1.id', $permissionB->getHashedKey()) + ->etc() + ); + } + + public function testSyncPermissionsOnNonExistingRole(): void + { + $permission = Permission::factory()->create(); + $invalidId = 7777; + $data = [ + 'role_id' => Hashids::encode($invalidId), + 'permissions_ids' => [$permission->getHashedKey()], + ]; + + $response = $this->makeCall($data); + + $response->assertStatus(404); + } + + public function testSyncNonExistingPermissionOnRole(): void + { + $role = Role::factory()->create(); + $invalidId = 7777; + $data = [ + 'role_id' => $role->getHashedKey(), + 'permissions_ids' => [Hashids::encode($invalidId)], + ]; + + $response = $this->makeCall($data); + + $response->assertStatus(404); } }