feat(auth): implement AdminAppMeController and AdminAppMeResource; add routes and tests for user context retrieval
This commit is contained in:
95
tests/Feature/Auth/AdminAppMeControllerTest.php
Normal file
95
tests/Feature/Auth/AdminAppMeControllerTest.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Authorization\Models\Role;
|
||||
use App\Domains\Menu\Models\Menu;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AdminAppMeControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_authentication_is_required(): void
|
||||
{
|
||||
$this->getJson('/api/v1/adminapp/me')->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_a_customer_cannot_access_the_adminapp_context(): void
|
||||
{
|
||||
$customer = User::factory()->create([
|
||||
'rol_codigo' => $this->createRole(RoleCode::User)->codigo,
|
||||
'tenant_codigo' => null,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($customer);
|
||||
|
||||
$this->getJson('/api/v1/adminapp/me')->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_it_returns_the_user_tenant_and_authorized_admin_menus(): void
|
||||
{
|
||||
$adminAppRole = $this->createRole(RoleCode::AdminApp);
|
||||
$tenant = $this->createTenant('acme');
|
||||
$otherTenant = $this->createTenant('other');
|
||||
|
||||
$catalog = $this->createMenu('admin.catalog', 'Catálogo', '/admin/catalog');
|
||||
$staff = $this->createMenu('admin.staff', 'Staff', '/admin/staff');
|
||||
$storefront = $this->createMenu('index', 'Inicio', '/');
|
||||
|
||||
$adminAppRole->menus()->sync([$catalog->code, $storefront->code]);
|
||||
$tenant->menues()->sync([$catalog->code, $staff->code, $storefront->code]);
|
||||
$otherTenant->menues()->sync([$staff->code]);
|
||||
|
||||
$user = User::factory()->create([
|
||||
'nombre_apellido' => 'Admin Acme',
|
||||
'email' => 'admin@acme.test',
|
||||
'rol_codigo' => $adminAppRole->codigo,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
]);
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$this->getJson('/api/v1/adminapp/me')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.user.id', $user->id)
|
||||
->assertJsonPath('data.user.email', 'admin@acme.test')
|
||||
->assertJsonPath('data.tenant.codigo', 'acme')
|
||||
->assertJsonCount(1, 'data.tenant.menues')
|
||||
->assertJsonPath('data.tenant.menues.0.code', 'admin.catalog')
|
||||
->assertJsonPath('data.tenant.menues.0.label', 'Catálogo')
|
||||
->assertJsonPath('data.tenant.menues.0.route', '/admin/catalog')
|
||||
->assertJsonMissing(['code' => 'admin.staff'])
|
||||
->assertJsonMissing(['code' => 'index']);
|
||||
}
|
||||
|
||||
private function createRole(RoleCode $role): Role
|
||||
{
|
||||
return Role::query()->create([
|
||||
'codigo' => $role->value,
|
||||
'nombre' => $role->value,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.test",
|
||||
]);
|
||||
}
|
||||
|
||||
private function createMenu(string $code, string $label, string $route): Menu
|
||||
{
|
||||
return Menu::query()->create([
|
||||
'code' => $code,
|
||||
'label' => $label,
|
||||
'route' => $route,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user