Skip to content

Commit

Permalink
remove db exists validation on request
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Sep 29, 2021
1 parent d86d451 commit 65d6e2c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}

0 comments on commit 65d6e2c

Please sign in to comment.