91 lines
2.4 KiB
PHP
91 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Api\V1\Roles;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class RoleSwitcherControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_index_returns_roles(): void
|
|
{
|
|
$user = $this->seedUser();
|
|
$roleId = $this->seedRole();
|
|
|
|
DB::table('user_roles')->insert([
|
|
'user_id' => $user->id,
|
|
'role_id' => $roleId,
|
|
]);
|
|
|
|
Sanctum::actingAs($user);
|
|
$response = $this->getJson('/api/v1/role-switcher');
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('status', true);
|
|
$this->assertNotEmpty($response->json('data.roles'));
|
|
}
|
|
|
|
public function test_switch_sets_role(): void
|
|
{
|
|
$user = $this->seedUser();
|
|
$this->seedRole();
|
|
Sanctum::actingAs($user);
|
|
|
|
$response = $this->postJson('/api/v1/role-switcher/switch', [
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$response->assertJsonPath('status', true);
|
|
$response->assertJsonPath('data.role', 'admin');
|
|
}
|
|
|
|
public function test_requires_authentication(): void
|
|
{
|
|
$response = $this->getJson('/api/v1/role-switcher');
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
private function seedUser(): User
|
|
{
|
|
$userId = DB::table('users')->insertGetId([
|
|
'firstname' => 'Admin',
|
|
'lastname' => 'User',
|
|
'cellphone' => '9999999999',
|
|
'email' => 'switch@example.com',
|
|
'address_street' => '123 Street',
|
|
'city' => 'City',
|
|
'state' => 'ST',
|
|
'zip' => '12345',
|
|
'accept_school_policy' => 1,
|
|
'password' => bcrypt('password'),
|
|
'user_type' => 'primary',
|
|
'semester' => 'Fall',
|
|
'school_year' => '2025-2026',
|
|
'status' => 'Active',
|
|
]);
|
|
|
|
return User::query()->findOrFail($userId);
|
|
}
|
|
|
|
private function seedRole(): int
|
|
{
|
|
return DB::table('roles')->insertGetId([
|
|
'name' => 'admin',
|
|
'slug' => 'admin',
|
|
'description' => 'Admin',
|
|
'dashboard_route' => 'administrator/administratordashboard',
|
|
'priority' => 1,
|
|
'is_active' => 1,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
}
|