update rm deadcode
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\Admin\Employee;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EmployeeRoleGuardTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Permission::firstOrCreate(['name' => 'administrator.edit', 'guard_name' => 'admin']);
|
||||
Permission::firstOrCreate(['name' => 'administrator.destroy', 'guard_name' => 'admin']);
|
||||
|
||||
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
|
||||
$administrator = Role::firstOrCreate(['name' => 'Administrator', 'guard_name' => 'admin']);
|
||||
$sale = Role::firstOrCreate(['name' => 'Sale', 'guard_name' => 'admin']);
|
||||
|
||||
$administrator->givePermissionTo(['administrator.edit', 'administrator.destroy']);
|
||||
$sale->givePermissionTo(['administrator.edit', 'administrator.destroy']);
|
||||
}
|
||||
|
||||
private function makeEmployee(string $role): User
|
||||
{
|
||||
$user = User::factory()->create([
|
||||
'username' => 'user-' . uniqid(),
|
||||
'email' => uniqid() . '@example.com',
|
||||
]);
|
||||
$user->assignRole($role);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function payload(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'fullname' => 'Test User',
|
||||
'username' => 'test-' . uniqid(),
|
||||
'email' => uniqid() . '@example.com',
|
||||
'role' => 'Sale',
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function test_non_full_administrator_cannot_edit_a_full_administrator(): void
|
||||
{
|
||||
$fullAdmin = $this->makeEmployee('Full Administrator');
|
||||
$caller = $this->makeEmployee('Administrator');
|
||||
|
||||
$response = $this->actingAs($caller, 'admin')->putJson(
|
||||
"/api/v1/administrator/employees/{$fullAdmin->id}",
|
||||
$this->payload(['role' => 'Sale']),
|
||||
);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_non_full_administrator_cannot_delete_a_full_administrator(): void
|
||||
{
|
||||
$fullAdmin = $this->makeEmployee('Full Administrator');
|
||||
$caller = $this->makeEmployee('Administrator');
|
||||
|
||||
$response = $this->actingAs($caller, 'admin')->deleteJson(
|
||||
"/api/v1/administrator/employees/{$fullAdmin->id}",
|
||||
);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_cannot_change_own_role(): void
|
||||
{
|
||||
$caller = $this->makeEmployee('Sale');
|
||||
|
||||
$response = $this->actingAs($caller, 'admin')->putJson(
|
||||
"/api/v1/administrator/employees/{$caller->id}",
|
||||
$this->payload(['username' => $caller->username, 'email' => $caller->email, 'role' => 'Administrator']),
|
||||
);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_non_full_administrator_cannot_assign_administrator_role(): void
|
||||
{
|
||||
$caller = $this->makeEmployee('Administrator');
|
||||
$target = $this->makeEmployee('Sale');
|
||||
|
||||
$response = $this->actingAs($caller, 'admin')->putJson(
|
||||
"/api/v1/administrator/employees/{$target->id}",
|
||||
$this->payload(['username' => $target->username, 'email' => $target->email, 'role' => 'Administrator']),
|
||||
);
|
||||
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_full_administrator_can_assign_administrator_role(): void
|
||||
{
|
||||
$caller = $this->makeEmployee('Full Administrator');
|
||||
$target = $this->makeEmployee('Sale');
|
||||
|
||||
$response = $this->actingAs($caller, 'admin')->putJson(
|
||||
"/api/v1/administrator/employees/{$target->id}",
|
||||
$this->payload(['username' => $target->username, 'email' => $target->email, 'role' => 'Administrator']),
|
||||
);
|
||||
|
||||
$response->assertOk();
|
||||
$this->assertTrue($target->fresh()->hasRole('Administrator'));
|
||||
}
|
||||
}
|
||||
@@ -24,14 +24,14 @@ class LoginThrottleTest extends TestCase
|
||||
{
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$response = $this->postJson('/api/v1/administrator/login', [
|
||||
'email' => 'nobody@example.com',
|
||||
'username' => 'nobody',
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
$this->assertNotSame(429, $response->status());
|
||||
}
|
||||
|
||||
$response = $this->postJson('/api/v1/administrator/login', [
|
||||
'email' => 'nobody@example.com',
|
||||
'username' => 'nobody',
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
|
||||
@@ -62,9 +62,43 @@ class PermissionGatingTest extends TestCase
|
||||
'tickets index' => ['GET', '/api/v1/administrator/tickets', 'support.view'],
|
||||
'activity-logs index' => ['GET', '/api/v1/administrator/activity-logs', 'system-log.manager'],
|
||||
'email-logs index' => ['GET', '/api/v1/administrator/email-logs', 'system-log.manager'],
|
||||
'post-categories index' => ['GET', '/api/v1/administrator/post-categories', 'post.view'],
|
||||
'posts index' => ['GET', '/api/v1/administrator/posts', 'post.view'],
|
||||
'media index' => ['GET', '/api/v1/administrator/media', 'media.view'],
|
||||
'dashboard stats' => ['GET', '/api/v1/administrator/dashboard/stats', 'dashboard.view'],
|
||||
'queue jobs recent' => ['GET', '/api/v1/administrator/queue/jobs/recent', 'queue.manager'],
|
||||
];
|
||||
}
|
||||
|
||||
public function test_force_delete_post_requires_post_destroy_permission(): void
|
||||
{
|
||||
Permission::firstOrCreate(['name' => 'post.destroy', 'guard_name' => 'admin']);
|
||||
|
||||
$unauthorized = $this->makeAdminWithPermissions(['post.view']);
|
||||
$this->actingAs($unauthorized, 'admin')
|
||||
->deleteJson('/api/v1/administrator/posts/999/force-delete')
|
||||
->assertStatus(403);
|
||||
|
||||
$authorized = $this->makeAdminWithPermissions(['post.destroy']);
|
||||
$response = $this->actingAs($authorized, 'admin')
|
||||
->deleteJson('/api/v1/administrator/posts/999/force-delete');
|
||||
$this->assertNotSame(403, $response->status());
|
||||
}
|
||||
|
||||
public function test_dashboard_requires_dashboard_view_permission(): void
|
||||
{
|
||||
Permission::firstOrCreate(['name' => 'dashboard.view', 'guard_name' => 'admin']);
|
||||
|
||||
$unauthorized = $this->makeAdminWithPermissions(['post.view']);
|
||||
$this->actingAs($unauthorized, 'admin')
|
||||
->getJson('/api/v1/administrator/dashboard/stats')
|
||||
->assertStatus(403);
|
||||
|
||||
$authorized = $this->makeAdminWithPermissions(['dashboard.view']);
|
||||
$response = $this->actingAs($authorized, 'admin')->getJson('/api/v1/administrator/dashboard/stats');
|
||||
$this->assertNotSame(403, $response->status());
|
||||
}
|
||||
|
||||
public function test_full_administrator_bypasses_all_permission_gates(): void
|
||||
{
|
||||
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
|
||||
|
||||
Reference in New Issue
Block a user