mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Stores are shared by all users, but the stores API only returned the caller's
own stores. Stores with no creator, such as Amazon AU from the seed data, were
missing from every user's results.
- GET /api/stores, filter[domain], filter[domains] and GET /api/stores/{id}
return every store.
- The urls and products includes return only the caller's rows. The user
include returns the creator only to the creator.
- A new StorePolicy lets any user create and edit a store. Only an admin can
delete one, in the API (403) and in the Filament UI.
- user_id now records who created a store. The server sets it, and an edit
does not change it. Store::creating() fills it from the logged-in user, else
the oldest admin. Adding a URL in a job passes the user who added it.
- A migration sets existing NULL user_id values to the oldest admin.
74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Filament;
|
|
|
|
use App\Filament\Resources\StoreResource\Pages\EditStore;
|
|
use App\Filament\Resources\StoreResource\Pages\ListStores;
|
|
use App\Models\Store;
|
|
use App\Models\User;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Tables\Actions\DeleteBulkAction;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Stores are shared. Any user can view and edit any store. Only an admin can delete
|
|
* one, because a delete removes every user's URLs and prices for that store.
|
|
*/
|
|
class StorePolicyTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_any_user_can_view_and_edit_any_store(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$store = Store::factory()->create(['user_id' => User::factory()->create()->id]);
|
|
|
|
$this->assertTrue($user->can('viewAny', Store::class));
|
|
$this->assertTrue($user->can('view', $store));
|
|
$this->assertTrue($user->can('create', Store::class));
|
|
$this->assertTrue($user->can('update', $store));
|
|
}
|
|
|
|
public function test_only_an_admin_can_delete_a_store(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$admin = User::factory()->admin()->create();
|
|
$store = Store::factory()->create(['user_id' => $user->id]);
|
|
|
|
$this->assertFalse($user->can('delete', $store));
|
|
$this->assertFalse($user->can('deleteAny', Store::class));
|
|
$this->assertTrue($admin->can('delete', $store));
|
|
$this->assertTrue($admin->can('deleteAny', Store::class));
|
|
}
|
|
|
|
public function test_edit_page_hides_delete_from_a_non_admin(): void
|
|
{
|
|
$this->actingAs(User::factory()->create());
|
|
$store = Store::factory()->create();
|
|
|
|
Livewire::test(EditStore::class, ['record' => $store->getKey()])
|
|
->assertSuccessful()
|
|
->assertActionHidden(DeleteAction::class);
|
|
}
|
|
|
|
public function test_edit_page_shows_delete_to_an_admin(): void
|
|
{
|
|
$this->actingAs(User::factory()->admin()->create());
|
|
$store = Store::factory()->create();
|
|
|
|
Livewire::test(EditStore::class, ['record' => $store->getKey()])
|
|
->assertActionVisible(DeleteAction::class);
|
|
}
|
|
|
|
public function test_list_page_hides_bulk_delete_from_a_non_admin(): void
|
|
{
|
|
$this->actingAs(User::factory()->create());
|
|
Store::factory()->create();
|
|
|
|
Livewire::test(ListStores::class)
|
|
->assertTableBulkActionHidden(DeleteBulkAction::class);
|
|
}
|
|
}
|