feat: add admin seeder and is_admin factory state
Runs is_admin migration, adds AdminSeeder for the admin user, registers it in DatabaseSeeder, adds admin() factory state to UserFactory, and adds AdminAccessTest covering both forbidden and ok cases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -57,4 +57,12 @@ class UserFactory extends Factory
|
|||||||
'two_factor_confirmed_at' => now(),
|
'two_factor_confirmed_at' => now(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate that the user is an admin.
|
||||||
|
*/
|
||||||
|
public function admin(): static
|
||||||
|
{
|
||||||
|
return $this->state(['is_admin' => true]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
database/seeders/AdminSeeder.php
Normal file
22
database/seeders/AdminSeeder.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class AdminSeeder extends Seeder
|
||||||
|
{
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
User::updateOrCreate(
|
||||||
|
['email' => 'uovidiu@sent.com'],
|
||||||
|
[
|
||||||
|
'name' => 'Ovidiu U',
|
||||||
|
'password' => Hash::make('changeme'),
|
||||||
|
'is_admin' => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// User::factory(10)->create();
|
$this->call(AdminSeeder::class);
|
||||||
|
|
||||||
User::factory()->create([
|
User::factory()->create([
|
||||||
'name' => 'Test User',
|
'name' => 'Test User',
|
||||||
|
|||||||
20
tests/Feature/Admin/AdminAccessTest.php
Normal file
20
tests/Feature/Admin/AdminAccessTest.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
uses(RefreshDatabase::class);
|
||||||
|
|
||||||
|
it('denies non-admin users access to admin panel', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$this->get('/admin')->assertForbidden();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows admin users to access admin panel', function () {
|
||||||
|
$user = User::factory()->admin()->create();
|
||||||
|
$this->actingAs($user);
|
||||||
|
|
||||||
|
$this->get('/admin')->assertOk();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user