From fe8fb2c38bcf9e41094450fbaec3045493706f48 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 14 Aug 2023 15:19:01 +0100 Subject: [PATCH] fix: Tenant profile policy method --- .../src/Pages/Tenancy/EditTenantProfile.php | 2 +- tests/database/factories/TeamFactory.php | 20 +++++++ .../migrations/create_teams_table.php | 22 ++++++++ tests/src/Models/Team.php | 26 +++++++++ tests/src/Models/User.php | 15 +++++- .../Pages/Tenancy/EditTenantProfileTest.php | 53 +++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/database/factories/TeamFactory.php create mode 100644 tests/database/migrations/create_teams_table.php create mode 100644 tests/src/Models/Team.php create mode 100644 tests/src/Panels/Pages/Tenancy/EditTenantProfileTest.php diff --git a/packages/panels/src/Pages/Tenancy/EditTenantProfile.php b/packages/panels/src/Pages/Tenancy/EditTenantProfile.php index 136a7adbf5..1ff46579ae 100644 --- a/packages/panels/src/Pages/Tenancy/EditTenantProfile.php +++ b/packages/panels/src/Pages/Tenancy/EditTenantProfile.php @@ -212,7 +212,7 @@ public static function getSlug(): string public static function canView(Model $tenant): bool { try { - return authorize('edit', $tenant)->allowed(); + return authorize('update', $tenant)->allowed(); } catch (AuthorizationException $exception) { return $exception->toResponse()->allowed(); } diff --git a/tests/database/factories/TeamFactory.php b/tests/database/factories/TeamFactory.php new file mode 100644 index 0000000000..5b6ff202a9 --- /dev/null +++ b/tests/database/factories/TeamFactory.php @@ -0,0 +1,20 @@ + $this->faker->company(), + ]; + } +} diff --git a/tests/database/migrations/create_teams_table.php b/tests/database/migrations/create_teams_table.php new file mode 100644 index 0000000000..04f6e8409b --- /dev/null +++ b/tests/database/migrations/create_teams_table.php @@ -0,0 +1,22 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('teams'); + } +}; diff --git a/tests/src/Models/Team.php b/tests/src/Models/Team.php new file mode 100644 index 0000000000..50acfbd242 --- /dev/null +++ b/tests/src/Models/Team.php @@ -0,0 +1,26 @@ +create()); + + Gate::policy(Team::class, TeamPolicyWithAccess::class); + + livewire(EditTeamProfile::class) + ->assertSuccessful(); +}); + +it('denies the user access to the tenant profile page if the user is unauthorized', function () { + Filament::setTenant(Team::factory()->create()); + + Gate::policy(Team::class, TeamPolicyWithoutAccess::class); + + livewire(EditTeamProfile::class) + ->assertNotFound(); +}); + +class EditTeamProfile extends EditTenantProfile +{ + public static function getLabel(): string + { + return 'Edit team'; + } +} + +class TeamPolicyWithAccess +{ + public function update(User $user, Team $team): bool + { + return true; + } +} + +class TeamPolicyWithoutAccess +{ + public function update(User $user, Team $team): bool + { + return false; + } +}